Chapter_12
Chapter_12
2. The array name is a symbolic reference to the address to the first byte of the array. When we use the array
name, we are actually referring to the first byte of the array.
6. Arrays do not have to be a simple list of keys and values—each location in the array can hold another array.
7. a. When an array is initialized with fewer initializers as compared to its size then the un-assigned elements
are filled with zeros.
b. If we have more initializers than the declared size of the array, then a compile time error will be
generated.
10. We know that storing an integer value requires 2 bytes, therefore here, word size is 2 bytes
Arr[35] = 1000 + 2(35 – 0)
= 1000 + 2(35) = 1070
= 2000 + 2 {5(7) + 4}
= 2000 + 2 (39)
= 2000 + 78 = 2078
12. int A[] = {9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99};
and VAL = 90, the algorithm will proceed in the following manner.
A[5] is less than VAL, therefore, we will now search for the value in the later half of the array. So, we
change the values of BEG and MID.
A[8] is less than VAL, therefore, we will now search for the value in the later half of the array. So, again we
change the values of BEG and MID.
int A[] = {9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99};
and VAL = 17, the algorithm will proceed in the following manner.
A[5] is greater than VAL, therefore, we will now search for the value in the first half of the array. So, we
change the values of BEG and MID.
A[4] is greater than VAL, therefore, we will now search for the value in the first half of the array. So, again
we change the values of BEG and MID.
A[1] is greater than VAL, therefore, we will now search for the value in the first half of the array. So, again
we change the values of BEG and MID.
Now, BEG = END and still VAL != A[MID]. This means that the value is not present in the array
13. Binary search is more efficient but it can be used only with a sorted list. For an unsorted list, linear search
is the only option.
1. #include<stdio.h>
void main()
{ int array1[10], i, n, j, flag=0, pos;
printf(“\n Enter the size of the array : “);
scanf(“%d”, &n);
for(i=0;i<n;i++)
{ printf(“ array[%d] = “, i);
scanf(“%d”, &array1[i]);
}
for(i=0;i<n;i++)
{ for(j= i+1;j<n;j++)
{ if(array1[i] == array1[j] && i!=j)
{ flag=1;
printf(“\n Duplicate number %d found at location %d and %d”, array1[i], i, j);
pos = i;
}
}
}
if (flag ==1)
{ for(i= pos; i<n;i++)
array1[i] = arr [i+1];
n--;
}
if(flag=0)
printf(“\n No Duplicate”);
}
2. Scan each element of both the arrays. If the size of the arrays and elements of the two arrays are exactly
same, then the two arrays are equal. For example, check arr1[i] = arr2[i]
6. for(i=0;i<n;i++)
{ term = pow(array[i],2);
sum += term;
}
8. for(i=0;i<n;i++)
{ for(j=i+1;j<=n;j++)
{ even = chk_even(array1[i]);
if(even==0)
{ odd = chk_even(array1[j]);
if(odd==1)
{ temp = array1[i];
array1[i] = array1[j];
array1[j] = temp;
}
}
}
}
9. Input the array elements and apply simple formula of adding the values, finding mean, etc
10. for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
sum += array[i][j];
}
mean = (float) sum / (n*n);
11. for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ if(i==j)
sum += array[i][j];
}
}
12. for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ if(array1[i][j]!=0)
count++;
}
}
16. for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ even = chk_even(array1[i][j]);
if(even==0)
{ odd_arr[k] = array1[i][j];
k++;
}
else
{ even_arr[m]=array1[i][j];
m++;
}
}
}
18. for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ for(k=0;k<3;k++)
transposed_arr[i][j][k] = arr[k][j][i];
}
}
22. for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ if(i<j)
sum += array[i][j];
}
}
23. for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ if(i>j)
sum += array[i][j];
}
}
24. #include<stdio.h>
void main()
{ int array1[3][3]={1,2,3,4,0,0,0,0,0};
int i,j. flag=0;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ if(i>j && array1[i][j]==0)
flag = 1;
}
}
if(flag==1)
printf("\n Upper Triangular Matrix);
else
printf("\n Not An Upper Triangular Matrix);
}
25. int isLowerTriangular(int a[][], int n)
{ for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ if(i>j && a[i][j]==0)
flag = 1;
}
}
if(flag==1)
return 1;
else return 0;
}
26. int isSymmetric(int a[][], int n)
{ for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ if(i!=j && a[i][j]== a[j][i])
flag = 1;
}
}
if(flag==1)
return 1;
else return 0;
}
27. #include<stdio.h>
void mul(int a[2][2], int n);
void main()
{ int array1[2][2]={1,2,3,4};
int array2[2][2]={2,3,4,5};
int i,j;
mul(array1,2);
mul(array2,3);
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
array1[i][j] += array2[i][j];
}
for(i=0;i<2;i++)
{ printf("\n");
for(j=0;j<2;j++)
printf("%d", array1[i][j]);
}
}
void mul(int a[2][2], int n)
{ int i,j;
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
array1[i][j] *= n;
}
}
34. a. if(a[i]%3 == 0)
count_divisible_by_3++;
b. for(i=0,j=0;i<n;i++,j++)
{ if(j==10)
{ printf(“\n”);
j=0;
}
printf(“%d “, arr[i]);
}
c. for(i=0,j=0;i<n;i++)
{ if(arr[i]%2 == 0)
{ if(j==10)
{ printf(“\n”);
j=0;
}
printf(“%d “, arr[i]);
j++;
}
}
d. if(a[i]%2 != 0)
count_odds++;
e. Same as discussed earlier
f. Same as discussed earlier
38. for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
if(i==j)
array1[i][j] = 0;
}
40. #include<stdio.h>
void main()
{
int i,j, marks[5][3], total_marks = 0, count=0;
float avg_marks = 0.0;
for(i=0;i<5;i++)
{ printf("\n Enter the marks of %dth student : ",i);
for(j=0;j<3;j++)
scanf("%d", &marks[i][j]);
}
for(j=0;j<3;j++)
{ total_marks=0;
for(i=0;i<5;i++)
total_marks += marks[i][j];
avg_marks = (float)total_marks/5;
printf("\n\n Total Marks and Average of %dth subject = %d %f",j, total_marks, avg_marks);
}
for(i=0;i<5;i++)
{ total_marks = 0;
for(j=0;j<3;j++)
total_marks += marks[i][j];
avg_marks = (float)total_marks/3;
printf("\n\n Total Marks and Average of %dth student = %d %f",i, total_marks, avg_marks);
if(avg_marks<50.00)
count++;
}
printf("\n Number of students securing below 50 = %d", count);
for(i=0;i<5;i++)
{ printf("\n Marks obtained by %dth student = ",i);
for(j=0;j<3;j++)
printf("%d ", marks[i][j]);
}
}
1. 1 -1 1 -1 1 -1 1 -1 1 -1
2. 0011
3. 0210210210
a. int marks(10);
use square brackets
b. int marks[10, 5];
commas are not allowed, there must be two separate square brackets
c. int marks[10],[5];
commas are not allowed between square brackets
d. int marks[10];
correct
e. int marks[ ];
size of the array is not specified
f. int marks[10] [5];
no space is allowed between square brackets
g. int marks[9+1][6-1];
correct