0% found this document useful (0 votes)
13 views7 pages

Chapter_12

The document contains answers to review questions and programming exercises related to arrays and their manipulation in C programming. It covers topics such as array initialization, searching algorithms like binary search, and various programming tasks involving arrays. Additionally, it addresses common errors in array declarations and initializations.

Uploaded by

Rizawn Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Chapter_12

The document contains answers to review questions and programming exercises related to arrays and their manipulation in C programming. It covers topics such as array initialization, searching algorithms like binary search, and various programming tasks involving arrays. Additionally, it addresses common errors in array declarations and initializations.

Uploaded by

Rizawn Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Reema Thareja Computer Fundamentals and Programming in C, 2e

Answers to Review Questions

1. Refer Section 12.1.

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.

3. Refer Figure 12.2.

4. Refer Figure 12.26.

5. Refer Sections 12.6 and 12.9.

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.

8. Refer Section 12.11.

9. Refer Section 12.11.

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

11. Addrss(Marks[8,5]) = 2000 + 2{ 5(8 – 1) + (5 – 1) }

= 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.

BEG = 0, END = 10, MID = (0 + 10)/2 = 5

Now, VAL = 90 and A[MID] = A[5] = 54

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.

© Oxford University Press 2016. All rights reserved.


Reema Thareja Computer Fundamentals and Programming in C, 2e

Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2 =16/2 = 8

Now, VAL = 90 and A[MID] = A[8] = 81

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.

Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 = 9

Now VAL = 90 and A[MID] = 90.

int A[] = {9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99};

and VAL = 17, the algorithm will proceed in the following manner.

BEG = 0, END = 10, MID = (0 + 10)/2 = 5

Now, VAL = 17 and A[MID] = A[5] = 54

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.

Now, END = MID - 1 = 4, BEG = 0, MID = (0 + 4)/2 = 4/2 = 2

Now, VAL = 17 and A[MID] = A[4] = 36

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.

Now, BEG = 0 and END = MID - 1 = 3, MID = (0 + 3)/2 = 1

Now VAL = 17 and A[MID] = 18.

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 = 0 and END = MID - 1 = 0, MID = (0 + 0)/2 = 0

Now VAL = 17 and A[MID] = 9.

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.

© Oxford University Press 2016. All rights reserved.


Reema Thareja Computer Fundamentals and Programming in C, 2e

Answers to Programming Exercises

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]

3. // Read the array elements in array1


for(i=0;i<n;i++)
{ for(j= i+1;j<n;j++)
{if(array1[i] + array1[j] == 50)
printf("\n %d + %d = 10", array1[i], array[j]);
}
}

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;
}

© Oxford University Press 2016. All rights reserved.


Reema Thareja Computer Fundamentals and Programming in C, 2e

}
}
}

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++)

© Oxford University Press 2016. All rights reserved.


Reema Thareja Computer Fundamentals and Programming in C, 2e

{ 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++)

© Oxford University Press 2016. All rights reserved.


Reema Thareja Computer Fundamentals and Programming in C, 2e

{ 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++)

© Oxford University Press 2016. All rights reserved.


Reema Thareja Computer Fundamentals and Programming in C, 2e

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]);
}
}

Find the Output of the Following Codes

1. 1 -1 1 -1 1 -1 1 -1 1 -1
2. 0011
3. 0210210210

Identify Errors, if any, in the Following Declaration Statements.

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

Identify Errors, if any, in the Following Initialization Statements.

a. int marks[ ] = {0,0,0,0};


correct
b. int marks[2][3] = {10,20,30,40};
correct
c. int marks[2,3] = {10, 20,30},{40,50,60}};
error- comma not allowed
d. int marks[10]={0};
no error

© Oxford University Press 2016. All rights reserved.

You might also like