Arrays
1. Which of the following array declarations are valid.
1. int [] arr = { 1, 2, 3, 4 };
Ans: Invalid. [] should be placed after arr.
2. int arr[] = { ‘1’, ‘2’, ‘3’, ‘4’ };
Ans: Valid. char literals are encoded as int. Array size is determined from number of values in initialization.
3. float price = { 2.2, 3.4, 5.5 };
Ans: Invalid. price is not declared as an array – missing [].
4. double perimeter[7] = { 0 };
Ans: Valid. Array of 7 double values, all initialized to 0.
5. char keyword[5] = { ‘A’ };
Ans: Valid. Array of 5 char values, all initialized to ‘A’.
2. Write a program to find highest value in an array of 10 integers.
#include<stdio.h> int main (int argc, char * argv[]) { int arr [] = { 1, 2, 3, 4, 5, 6, 7, 10, 8, 9 }; /* If we initialize max to 0, program will fail if * all elements of array were negative. */ int max = arr[0]; for(int i=1; i<10; i++) { if(arr[i] > max) { max = arr[i]; } } printf("Max value in array is %d\n", max); return 0; }
3. Write a program to compute the average of 10 float numbers in an array.
#include<stdio.h> int main (int argc, char * argv[]) { float arr [] = { 1.2, 2.1, 3.4, 9.4, 10.5, 6.6, 0.7, 10, 8.8, 1.9 }; float average = 0.0; for(int i=0; i<10; i++) { average += arr[i]; } average /= 10; printf("Average of array is %f\n", average); return 0; }
4. What will be the output of following program.
#include <stdio.h> int main (int argc, char * argv[]) { int arr[] = { 0,1,2,3,4,5,6,7,8,9 }; int i=0; int num_elements = 10; for(i=9; i>=0; i--) { arr[i] = arr[num_elements - i - 1] + arr[i]; } for(i=0; i<num_elements; i++) { printf("arr[%d] = %d\n", i, arr[i]); } return 0; }
Ans:
arr[0] = 9
arr[1] = 10
arr[2] = 11
arr[3] = 12
arr[4] = 13
arr[5] = 9
arr[6] = 9
arr[7] = 9
arr[8] = 9
arr[9] = 9
5. Enhance the above program to add two functions find_max and find_min to find the highest and lowest scores respectively.
01: #include <stdio.h> 02: float compute_average(int marks[], int size); 03: int main (int argc, char * argv[]) { 04: int marks[50] = { 0 }; 05: int i=0; 06: float average = 0.0; 07: do { 08: printf ("Enter marks for student %d", i+1); 09: scanf("%d",&marks[i]); 10: i++; 11: }while(i<50); 12: average = compute_average(marks, 50); 13: printf("Average score is %f\n", average); 14: return 0; 15: } 16: float compute_average(int marks[], int size) { 17: int i=0, sum=0; 18: for(i=0; i<size; i++) { 19: sum = sum + marks[i]; 20: } 21: return ((1.0*sum)/size); 22: }
int find_max(int marks[], int size) { int max = marks[0]; for(int i=1; i<size; i++) { if(marks[i] > max) { max = marks[i]; } } return max; } int find_min(int marks[], int size){ int min = marks[0]; for(int i=1; i<size; i++) { if(marks[i] < min) { min = marks[i]; } } return min; }
6. Write a program to search a value in an array of integers.
int find_val(int arr[], int size, int val){ for(int i=0; i<size; i++) { if(arr[i] == val) { return i; } } return -1; }
7. Write a program that inputs two arrays of 10 integers each and saves sum of corresponding elements of these arrays in a third array and prints them.
#include <stdio.h> #define ARRAY_SIZE 10 void read_arr(int [], int); void add_arr(int [], int [], int [], int); int main (int argc, char * argv[]) { int arr1[ARRAY_SIZE] = { 0 }; int arr2[ARRAY_SIZE] = { 0 }; int result[ARRAY_SIZE] = { 0 }; read_arr(arr1, ARRAY_SIZE); read_arr(arr2, ARRAY_SIZE); add_arr(arr1, arr2, result, ARRAY_SIZE); for(int i=0; i<ARRAY_SIZE; i++) { printf("%d + %d = %d\n", arr1[i], arr2[i], result[i]); } return 0; } void read_arr(int arr[], int size) { for(int i=0; i<size; i++) { printf("Enter element %d ", i+1); scanf("%d", &arr[i]); } } void add_arr(int arr1[], int arr2[], int result[], int size) { for(int i=0; i<size; i++) { result[i] = arr1[i] + arr2[i]; } }
8. Trace control flow of above example.
01: #include<stdio.h> 02: int main (int argc, char * argv[]) { 03: int marks[5][3] = { 0 }; 04: int student=0, subject=0, sum=0; 05: for(student=0; student<5; student++) { 06: for(subject=0; subject<3; subject++) { 07: printf("Enter marks for student %d subject %d: ",student,subject); 08: scanf("%d",&marks[student][subject]); 09: } 10: } 11: for(student=0; student<5; student++) { 12: sum = 0; 13: for(subject=0; subject<3; subject++) { 14: sum = sum + marks[student][subject]; 15: } 16: printf("Student %d scored total %d\n", student, sum); 17: } 18: return 0; 19: }
- After initialization, control reaches line 05.
- Control expression 0<5 is true. Control enters for loop, line 06.
- Another for loop. Control expression 0<3 evaluates to true. Control enters nested loop, line 07.
- Message Enter marks for student 0 subject 0: is printed to the console. Control advances to line 08 and input is read.
- End of nested loop is reached. Tail statement is executed. Value of subject is incremented to 1. Control jumps to line 06.
- Control expression 1<3 evaluates to true. Control enters nested loop, line 07.
- Message Enter marks for student 0 subject 1: is printed to the console. Control advances to line 08 and input is read.
- End of nested loop is reached. Tail statement is executed. Value of subject is incremented to 2. Control jumps to line 06.
- Control expression 2<3 evaluates to true. Control enters nested loop, line 07.
- Message Enter marks for student 0 subject 2: is printed to the console. Control advances to line 08 and input is read.
- End of nested loop is reached. Tail statement is executed. Value of subject is incremented to 3. Control jumps to line 06.
- Control expression 3<3 evaluates to false. Control jumps out of nested loop, line 10.
- End of outer loop is reached. Tail statement is executed. Value of student is incremented to 1. Control jumps to line 05.
- Steps 2-13 are repeated for all values student from 1 to 4.
- When value of student is incremented to 5, control jumps out of outer loop, line 11.
- Value of student is set to 0.
- Control expression 0<5 is true. Control enters for loop, line 12.
- Value of sum is set to 0. Note that since we need to compute total for each student, we set sum to 0 before we start adding marks for that student.
- Control advances to line 13. Value of subject is set to 0.
- Control expression 0<3 is true. Control enters for loop, line 14.
- Value of marks[0][0] is added to sum.
- End of nested loop is reached. Control statement is executed and value of subject is incremented to 1.
- Control expression 1<3 is true. Control etners for loop, line 14.
- Value of marks[0][1] is added to sum.
- End of nested loop is reached. Control statement is executed and value of subject is incremented to 2.
- Control expression 2<3 is true. Control etners for loop, line 14.
- Value of marks[0][2] is added to sum.
- End of nested loop is reached. Control statement is executed and value of subject is incremented to 3.
- Control expression 3<3 is false. Control jumps out of nested for loop, line 16.
- Message student 0 scored total is printed with value of sum.
- Control advances to line 17, end of outer for loop.
- Tail statement is executed. Value of student is incremented to 1.
- Steps 20-31 are executed for all values of student from 1-4.
- When value of student is incremented to 5, control jumps out of outer for loop, line 18.
- Program terminates.
9. Enhance the above example to print average marks of each student in addition to total marks.
#include<stdio.h> int main (int argc, char * argv[]) { int marks[5][3] = { 0 }; int student=0, subject=0, sum=0; for(student=0; student<5; student++) { for(subject=0; subject<3; subject++) { printf("Enter marks for student %d subject %d: ",student, subject); scanf("%d",&marks[student][subject]); } } for(student=0; student<5; student++) { sum = 0; for(subject=0; subject<3; subject++) { sum = sum + marks[student][subject]; } printf("Student %d scored total %d and average %f\n", student, sum, sum/3.0); } return 0; }
10. Enhance the program further to print class average marks in each subject.
#include<stdio.h> int main (int argc, char * argv[]) { int marks[5][3] = { 0 }; int student=0, subject=0, sum=0; for(student=0; student<5; student++) { for(subject=0; subject<3; subject++) { printf("Enter marks for student %d subject %d: ",student,subject); scanf("%d",&marks[student][subject]); } } for(student=0; student<5; student++) { sum = 0; for(subject=0; subject<3; subject++) { sum = sum + marks[student][subject]; } printf("Student %d scored total %d and average %f\n", student, sum, sum/3.0); } for(subject=0; subject<3; subject++) { sum = 0; for(student=0; student<5; student++) { sum = sum + marks[student][subject]; } printf("Average score for subject %d is %f\n", subject, sum/5.0); } return 0; }