【正文】
is: )。 scanf ( %f, amp。d )。 printf ( The number of years is: )。 scanf ( %d, amp。n )。 for ( i = 1, p = c。 i = n。 i++ ) p = p * ( 1 – d )。 printf ( The scrap value at the end of useful life of the item is %.2f\n, p )。 } 6/2/2022 Chapter 7 Arrays An election is contested by 5 candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the votes cast for each candidate using an array variable count. In case, a number read is outside the range 1 to 5, the ballot should be considered as a 39。spoilt ballot39。 and the program should also count the number of spoilt ballots. 6/2/2022 Chapter 7 Arrays main ( ) { int count, candidate[6]={0}, spoilt = 0。 printf ( Please input the votes (1~5) and press 39。039。 to end the voting.\n )。 scanf ( %d, amp。count )。 while ( count ) { if ( count =1 amp。amp。 count = 5) candidate[count] ++。 else spoilt ++。 scanf ( %d, amp。count )。 } printf ( The result of voting:\n )。 for ( count = 1。 count = 5。 count ++ ) printf(Candidate%3d ballots: %d \n,count,candidate[count])。 printf ( Spoilt ballots: %d\n, spoilt )。 } 6/2/2022 Chapter 7 Arrays The following set of numbers is popularly known as Pascal39。s triangle. If we denote rows by i and columns by j, then any element (except the boundary elements) in the triangle is given by pi,j = pi1,j1 + pi1,j Write a program to calculate the elements of the Pascal triangle for 10 rows and print the results. ???????????????151010511464113311211116/2/2022 Chapter 7 Arrays define N 10 main ( ) { int p[N][N] = {0}, i, j。 for ( i = 0。 i N。 i ++ ) for ( j = 0。 j = i。 j++ ) if ( j == 0 || i == j ) p[i][j] = 1。 else p[i][j] = p[i1][j1] + p[i1][j]。 for ( i = 0。 i N。 i ++ ) { for ( j = 0。 j = i。 j++ ) printf ( %5d, p[i][j] )。 printf ( \n )。 } } 6/2/2022 Chapter 7 Arrays The annual examination results of 100 students are tabulated as follows: Write a program to read the data and determine the following: (a) Total marks obtained by each student. (b) The highest marks in each subject and the Roll No. of the student who secured it. (c) The student who obtained the highest total marks. Roll No. Subject 1 Subject 2 Subject 3 6/2/2022 Chapter 7 Arrays define N 100 main ( ) { float s[N][4] = {0}, m[N] = {0}, max。 int i, j, k。 printf(Please input the roll number and marks of %d students.\n, N)。 for ( i = 0。 i N。 i ++ ) { printf(Roll No. : )。 scanf(%f, amp。s[i][0])。 printf(The mark of Subjcet 1 : )。 scanf(%f, amp。s[i][1])。 m[i] = s[i][1]。 printf(The mark of Subjcet 2 : )。 scanf(%f, amp。s[i][2])。 m[i] += s[i][2]。 printf(The mark of Subjcet 3 : )。 scanf(%f, amp。s[i][3])。 m[i] += s[i][3]。} for ( i = 0。 i N。 i ++ ) printf( Total marks of student % is : %.2f\n, s[i][0], m[i] )。 for ( j = 1。 j = 3。 j ++ ) { for ( i = 0, max = s[0][j], k = 0。 i N。 i ++ ) if ( s[i][j] max ) { max = s[i][j]。 k = i。 } printf(The highest mark of Subject %d is : %.2f. The Roll No. is : %\n, j, max, s[k][0] )。 } for ( i = 0, max = m[0], k = 0。 i N。 i ++ ) if ( m[i] max ) { max = m[i]。 k = i。 } printf ( The student % obtained the highest total marks.\n, s[k][0] )。 } 6/2/2022 Chapter 7 Arrays Given are two onedimensional arrays A and B which are sorted in ascending order. Write a program to merge them into a single sorted array C that contains every item from arrays A and B, in ascending order. 6/2/2022 Chapter 7 Arrays define M 5 define N 6 main ( ) { int a[M], b[N], c[M+N], i, j, k。 printf(Input %d numbers of array a in ascending order:\n, M)。 for ( i = 0。 i M。 i ++ ) scanf ( %d, amp。a[i] )。 printf(Input %d numbers of array b in ascending order:\n, N)。 for ( j = 0。 j N。 j++ ) scanf ( %d, amp。b[j] )。 for ( k = i = j = 0。 k M + N。 k++ ) { if ( b[j] a[i] amp。amp。 j N || i = M ) { c[k] = b[j]。 j++。 continue。 } else { c[k] = a[i]。 i++。 continue。 } } printf(The merged numbers are: )。 for ( k = 0。 k M + N。 k ++ ) printf( %6d, c[k] )。 } 6/2/2022 Chapter 7 Arrays Two matrices that have the same number of rows and columns can be multiplied to produce a third matrix. Consider the following two matrices. The product of A and B is a third matrix C of size nxn where each element of C is given by the following equation. Write a program that will read the values of elements of A and B and produce the product matrix C. ??????????????????????????nnnnnnnnnnnnbbbbbbbbbBaaaaaaaaaA212222111211212222111211??????????????? nkkjikij baC16/2/2022 Chapter 7 Arrays define N 10 main ( ) { int a[N][N], b[N][N], c[N][N], i, j, k。 printf(Please input matrix A of %d by %d:\n, N, N)。 for ( i = 0。 i N。 i ++ ) for ( j = 0。 j N。 j++ ) scanf ( %d, amp。a[i][j] )。 printf(Please input matrix B of %d by %d:\n, N, N)。 for ( i = 0。 i N。 i ++ ) for ( j = 0。 j N。 j++ ) scanf ( %d, amp。b[i][j] )。 printf ( The product of matrix A and matrix B is : \n )。 for ( i = 0。 i N。 i ++ ) { for ( j = 0。 j N。 j++ ) { for ( k = 0, c[i][j] = 0。 k N。 k ++ ) c[i][j] += a[i][k] * b[k][j]。 printf ( %6d, c[i][j] )。 } printf ( \n )。 } } 6/2/2022 Chapter 7 Arrays Write a program that fills a fivebyfive matrix as follows. ? Upper left triangle with +1s ? Lower right triangle with 1s ? Right to left diagonal with zeros Display the co