Question Bank For Arrays
Question Bank For Arrays
Question Bank For Arrays
Question No 1:
Write a program to create an array of n elements statically in which update the ith
element and print the array.
#include<stdio.h>
int main()
{
int arr[5]={10,20,30,40,50},i;
// printing the array before update
printf("Array elements before update:\n");
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
arr[2]=345;
// printing the array after update
printf("\nArray elements after update:\n");
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
Question No 2:
Write a program that calculates the total point earned by a football team
over a series of games and calculate the average points scored by the team.
For Example:
input =
5
30 20 40 10 50
output =
Sum=150
Average=30
#include<stdio.h>
int main()
{
int n, sum = 0, temp, avg,i;
scanf("%d", &n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=sum/n;
printf("Sum = %d\n",sum);
printf("average = %d\n",avg);
return 0;
}
Question No 3:
Write a program to find maximum and minimum of array elements.
Note: Array elements are of integer type
#include<stdio.h>
int main()
{
int max=0,min=0;i,n;
printf ("Enter no. of elements:");
scanf("%d",&n);
int a[n];
printf ("Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<max)
min=a[i];
}
printf("Maximum element is = %d\n",max);
printf("Minimum element is = %d\n",min);
return 0;
}
Question No 4:
Write a program to find second maximum of array elements.
#include<stdio.h>
int main()
{
int n,i,array[n],max=0,secondmax=0;
if(array[i]>max){
secondmax = max;
max = array[i];
}
return 0;
}
Question No 5:
a. What is the difference between array and a pointer? Give examples for declaring
single, two dimensional and multi-dimensional arrays.
Ans:
Array: Arrays are generally used for storing the same types of data items or values that are
same in the nature. Although arrays cannot be used to store the data items or values that are
not same in the nature, it is also considered a significant disadvantage of the arrays.
Syntax: int mark[5] = {19, 10, 8, 17, 9};
Pointers: Pointers are generally used for storing the memory address of the other variable
instead of storing the actual value of the variable. However, we can also use the pointer to
access the whole array by using the pointer arithmetic. In addition, it also even makes the
accessing array process much faster.
int *p;
Working It generally stores the address of another Array usually stores the value of the
variable of the same data type as the variable of the same datatype.
pointer variable's datatype.
Storage Pointers are specially designed to store the A normal array stores values of
address of variables. variables, and pointer array stores the
address of variables.
Capacity Usually, arrays can store the number of A pointer variable can store the address
elements the same size as the size of the of only one variable at a time.
array variable.
type name[size1][size2]...[sizeN];
For example, the following declaration creates a three-dimensional
integer array −
intthreedim[5][10][4];
Two-dimensional Arrays
The simplest form of multidimensional array is the two-dimensional array.
A two-dimensional array is, in essence, a list of one-dimensional arrays.
To declare a two-dimensional integer array of size [x][y], you would write
something as follows −
typearrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A
two-dimensional array can be considered as a table which will have x number of rows and y
number of columns. A two-dimensional array a, which contains three rows and
four columns can be shown as follows −
Thus, every element in the array a is identified by an element name of the form a[ i ][ j ],
where 'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each
element in 'a'.
Initializing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.
int a[3][4]={
{0,1,2,3},/* initializers for row indexed by 0 */
{4,5,6,7},/* initializers for row indexed by 1 */
{8,9,10,11}/* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional. The
following initialization is equivalent to the previous example −
}
printf("2D array elements are:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
Question No 6:
Write a program to perform addition of two matrices.
#include<stdio.h>
int main()
{
int i,j,r,c;
printf ("Enter no. of rows:");
scanf("%d",&r);
printf ("\nEnter the columns:");
scanf("%d",&c);
int a[r][c], b[r][c], c[r][c];
printf(“Enter the elements of the first matrix:\n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf(“Enter the elements of the second matrix:\n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
printf("Addition of 2 matrices:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
Question No 7:
Write a program to perform multiplication of two matrices.
#include<stdio.h>
int main()
{
int i,j,k,r1,c1,r2,c2;
printf ("Enter the rows of 1st matrix:");
scanf("%d",&r1);
printf ("\nEnter the columns of 1st matrix::");
scanf("%d",&c1);
printf ("Enter the rows of 2nd matrix:");
scanf("%d",&r2);
printf ("\nEnter the columns of 2nd matrix:");
scanf("%d",&c2);
int a[r1][c1], b[r2][c2], c[r1][c2];
printf("Enter the elements of the first matrix:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of the second matrix:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("Multiplication of 2 matrices:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Question No 8:
Write a program to perform trace of two matrices.
#include<stdio.h>
int main()
{
int i,j,r,c,trace;
printf ("Enter no. of rows:");
scanf("%d",&r);
printf ("\nEnter the columns:");
scanf("%d",&c);
int a[r][c];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
for(i=0;i<r;i++)
{
trace=trace+a[i][i];
}
printf("Trace of a matrix is: %d",trace);
}
Question No 9:
Write a program to perform transpose of two matrices.
#include<stdio.h>
int main()
{
int a[20][20],b[20][20];
int i,j,r,c;
printf ("Enter no. of rows:");
scanf("%d",&r);
printf ("\nEnter the columns:");
scanf("%d",&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=a[j][i];
}
}
printf("Transpose of a matrix is:\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
return 0;
}
Question No 10:
Write a program to find the row wise and column wise greatest element
note - first and second line contains the size of the matrix
input format:
3
3
123
654
791
output =
row wise greatest element
3
6
9
column wise greatest element
7
9
4
#include<stdio.h>
int main()
{
int x,y,i,j,l,k;
scanf("%d",&x);
scanf("%d",&y);
int a[10][10];
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("row wise greatest element\n");
for(i=0;i<x;i++)
{
l=a[i][0];
for(j=1;j<y;j++)
{
if(l<a[i][j])
{
l=a[i][j];
}
}
printf("%d\n",l);
}
printf("column wise greatest element\n");
for(j=0;j<y;j++)
{
l=a[0][j];
for(i=1;i<x;i++)
{
if(l<a[i][j])
{
l=a[i][j];
}
}
printf("%d\n",l);
}
return 0;
}
Question No 11:
Swetha as a Subject teacher conducted a test of MCQ's to the students.
After the test completed, she came to know that some of the students copied the
answers from other students and both obtained the same marks.
For example, If a student 'A' obtained 7 marks, all the students who copied from
'A', also obtained '7' marks.
Now the students with the same marks are disqualified in the test.
Help Swetha to find marks of the Topper in the class from the Qualified students.
Return -1 if no such student exists.
Input Format:
-------------
Line-1: An Integer N represents number of students.
Line-2: N space separated integers represent marks of N students marks[].
Output Format:
--------------
Print an integer.
Constraints:
1 <= N <= 2000
0 <= marks[i] <= 1000
Sample Input-1:
---------------
6
11 12 3 12 11 10
Sample Output-1:
----------------
10
Explanation:
------------
The marks with 12 and 11 are copied. so the topper is with marks 10.
Sample Input-2:
---------------
6
10 15 15 10 20 20
Sample Output-2:
----------------
-1
Explanation:
------------
Every one will get disqualified.
#include<stdio.h>
int main()
{
int n,i,j,c=1,max=-1;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]<0 || a[i]>1000)
break;
for(j=0;j<n;j++)
{
if(a[i]==a[j])
{
c++;
}
}
if(c==1 && a[i]>max)
{
max=a[i];
}
c=0;
}
printf("%d",max);
Question No 12:
Write a C program to compute mean, variance, Standard Deviation.
#include<stdio.h>
#include<math.h>
void main()
{
float x[10], mean, variance,dev;
float sum1=0, sum2=0;
int n,i;
printf("Enter the number of integers:");
scanf("%d", &n);
printf("\nEnter the integers:");
for(i=0;i<=n-1;i++)
{
scanf("%f", &x[i]);
}
//to find mean
for(i=0;i<=n-1;i++)
{
sum1 = sum1 + x[i];
}
mean = sum1 /n;
printf("\nmean = %f", mean);
Question No 13:
Discuss the applications of arrays.
Ans:
● Arrays are used to Store List of values
In c programming language, single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arrays are used to store a row of values. In single
dimensional array data is stored in linear form.
● Arrays are used to Perform Matrix Operations
We use two dimensional arrays to create matrix. We can perform various operations on
matrices using two dimensional arrays.
● Arrays are used to implement Search Algorithms
We use single dimensional arrays to implement search algorihtmslike ...
1. Linear Search
2. Binary Search
● Arrays are used to implement Sorting Algorithms
We use single dimensional arrays to implement sorting algorihtmslike ...
1. Insertion Sort
2. Bubble Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort, etc.,
● Arrays are used to implement Datastructures
We use single dimensional arrays to implement datastructures like...
1. Stack Using Arrays
2. Queue Using Arrays
● Arrays are also used to implement CPU Scheduling Algorithms