Module III – Question Bank
1. What is an array?
An array is derived data type in c programming language which can store similar type of
data in continuous memory location. Data may be primitive type (int, char, float, double…),
address of union, structure, pointer, function or another array.
Example of array declaration:
int arr[5];
char arr[5];
float arr[5];
long double arr[5];
char * arr[5];
int (arr[])();
double ** arr[5];
Array is useful when:
(a) We have to store large number of data of similar type. If we have large number of similar
kind of variable then it is very difficult to remember name of all variables and write the
program. For example:
//PROCESS ONE
#include<stdio.h>
int main(){
int ax=1;
int b=2;
int cg=5;
int dff=7;
int am=8;
int raja=0;
int rani=11;
int xxx=5;
int yyy=90;
int p;
int q;
int r;
int avg;
avg=(ax+b+cg+dff+am+raja+rani+xxx+yyy+p+q+r)/12;
printf("%d",avg);
return 0;
}
If we will use array then above program can be written as:
//PROCESS TWO
#include<stdio.h>
int main(){
int arr[]={1,2,5,7,8,0,11,5,50};
int i,avg;
for(int i=0;i<12;i++){
avg=avg+arr[i];
}
printf("%d",avg/12);
return 0; }
2. What all are the advantages of using the array?
1. An array provides singe name .So it easy to remember the name of all element of an array.
2. Array name gives base address of an array .So with the help increment operator we can
visit one by one all the element of an array.
3. Array has many application data structure.
3. What all are the Different type of array in c?
(a) Array of integer
An array which can hold integer data type is known as array of integer.
(b) Array of character
An array which can hold character data type is known as array of character.
(c) Array of union
An array which can hold address of union data type is known as union of integer.
For example:
(1) What will be output when you will execute the following program?
#include<stdio.h>
union A{
char p;
float const * const q;
};
int main(){
union A arr[10];
printf("%d",sizeof arr);
return 0;
}
Output: 20
(2) What will be output when you will execute the following program?
#include<stdio.h>
union A{
char character;
int ascii;
};
int main(){
union A arr[2]={{65},{'a'}};
printf("%c %c",arr[0],arr[1]);
return 0;
}
Output: A a
(d) Array of structure
An array which can hold address of structure data type is known as array of structure. For
example:
(1) What will be output when you will execute the following program?
#include<stdio.h>
typedef struct stu{
char * name;
int roll;
}s;
int main(){
s arr[2]={{"raja",10},{"rani",11}};
printf("%s %d",arr[0]);
return 0;
}
Output: raja 10
(2) What will be output when you will execute the following program?
#include<stdio.h>
struct A{
int p;
float q;
long double *r;
};
int main(){
struct A arr[10];
printf("%d",sizeof arr);
return 0;
}
Output: 80
(e) Array of string
An array which can hold integer data type is known as array of integer.
(f) Array of array
An array which can hold address of another array is known as array of array.
4. Write a program to calculate the standard deviation of an array of
√ ( )
− n 2
1
SD= ∑ x −xi −
n values. n i =1 , where x is the mean of the values.?
#include<stdio.h>
main( )
float val[10], f, sum=0.0, sq;
int i, n, dif;
printf(“Enter the limit\n”);
scanf(“%d”,&n);
printf(“Enter the values\n”);
for(i=0;i<n;i++)
scanf(“%f”,&val[i]);
for(i=0;i<n;i++)
sum+=val[i];
p=sum/n;
for(i=0;i<n;i++)
dif=(p-val[i]) * (p-val[i]);
sum+=dif;
sq=sqrt(sum/n);
5. Write a program to accept a square matrix and print the trace and
norm of the matrix.
Hint: Trace of the matrix is the sum of main diagonal elements and
Norm is the square root of the sum of squares of all the elements of
the matrix.
#include<stdio.h>
main( )
int a[10][10], n, i, j, r, c;
float t=0,sum=0,normal;
printf(“Enter the order of matrix\n”);
scanf(“%d %d”, &r, &c);
printf(“Enter the elements\n”);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf(“%d”,&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(i==j)
t+=a[i][j];
sum+=pow(a[i][j],2);
normal=sqrt(sum);
printf(“Trace of the matrix is %f\n”, t);
printf(“Normal of the matrix is %f\n”, normal);