PROGRAM 4
Objective:
Case Study: Prepare case study on C,C++,JAVA for readability(evaluation
criteria).
CASE STUDY: READABILITY
INTRODUCTION
A programming language is a formal language comprising a set of instructions that
produce various kinds of output. Programming languages are used in computer
programming to implement algorithms. All languages have elements in common, which
can be exploited to make a comparison between them. You’ll want comparison criteria,
to use with respect to these elements in common. Each of these criteria is determined and
influenced by a certain number of language qualities, determined and implemented
during the design of the language.
The following are four language evaluation criteria
Readability:- The ease with which programs can be read and understood.
Writability:- The ease with which a language can be used to create a program
Reliability:- Conformance to specifications (i.e., performs to its specifications)
Cost:- the ultimate total cost.
The case study is about readability so let's read in detail about readability.
READABILTY
The ease with which programs can be read and understood is called readability. The following
describe characteristics that contribute to the readability of a PL
a) Simplicity- The language that has large no. of basic components is more difficult is more
difficult to learn than one with a small no of basic components. The language should not have
multiplicity of commands. For eg. I = I + 1 ; I + = 1 ;I + + ; + + I . The language should not have
operator overloading in which a single operator symbol has more than one meaning
b) Orthogonal – It means that a relatively small number of primitive constructs can be
combined in number of ways to build the program. Orthogonal language is independent of the
context of its appearance in the program.
c) Control Statements- A program that can be read from top to bottom is much easier to
understand than a program that requires the reader to jump from one statement to some other
non adjacent statement.
d) Data Types and Structures – The presence of adequate facilities for defining data types and
data structures in a language is another significant aid to readability. There should be provision
for data types, for record type of data types(representing an array of employee records)e)Syntax
considerations – Syntax is the form of elements of language. There are 3 types of syntactic
design choices that affect readability. Different forms of identifiers, special keywords(reserve
words),Form & meaning – constructs that are similar in appearance but different meaning is
not readable
1. OVERALL SIMPLICITY
a) Multiplicity:
C C++ java
count = count + 1 count = count + 1 count = count + 1
count += 1 count += 1 count += 1
count++ count++ count++
++count ++count ++count
2.
b) operator overloading:
C C++ java
In arrays overloading is In Arrays, pointers, objects Java can support overloading in
Possible. etc overloading can be done. Sting catenation also(including.
other factors).
3.orthogonality:
An array can contain any data type except void
Parameters are passed by value but array are passed by reference
C++
class can contain variables and functions (well … that’s obvious)
functions can contain classes ad variables (local classes inside function? C++
can, but have some limitation with templates: this is an example of non perfect
orthogonality)
variables can contain classes and functions. (That’s harder to see ... but a variable
created as instance an anonymous strut inf fact can)
java:
The accessibility specifies public/private are completely orthogonal to the static specifier.
A notorious non-orthogonality are the semantics of variables:
Int x;
Myclass y;
4.exception handling:
C:
Do not support exception handling;
C++
Supports by try and catch method
Java:
Done by try and catch method
5. syntax design(For output statement)
C:
printf, (, "Hello,World" ); and
scanf("%d",&n);
printf("sum= %d",sum);
C++:
Cout<<” hello world”;
cin>>n;
cout<<"sum="<<sum;
Java
System.out.primt(“Hello world);
num1 = sc.nextDouble();
system.out.println("sum="+sum);
QUE1
Program(code):
#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n < 1) {
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
for (i = 0; i < n; ++i) {
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
Ques2 . WAP for matrix addition of 2 D
QUE 2.
array with time complexcity.
ANS2-
#include <stdio.h>
int main()
{
int rows, cols;
//Initialize matrix a
int a[][3] = {
{1, 0, 1},
{4, 5, 6},
{1, 2, 3}
};
//Initialize matrix b
int b[][3] = {
{1, 1, 1},
{2, 3, 1},
{1, 5, 1}
};
//Calculates number of rows and columns present in given mat
rix
rows = (sizeof(a)/sizeof(a[0]));
cols = (sizeof(a)/sizeof(a[0][0]))/rows;
//Array sum will hold the result
int sum[rows][cols];
//Performs addition of matrices a and b. Store the result in ma
trix sum
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Addition of two matrices: \n");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}