Arrays[ ] Problem Set
1.) Write C statements to accomplish each of the following:
2.) Write C statements that perform each of the following single subscripted array operations:
3.) Find the error(s) in each of the following statements:
a.) char str[5]; scanf("%s", str); /* user types hello */ b.) int a[3]; printf("$d %d %d\n", a[1], a[2], a[3]); c.) #define SIZE 4 main() { float f[SIZE] = { 1.1, 10.01, 100.01, 1000.111, 109.54 }; int b[SIZE - 5]; int array_one[3.0]; d.) double d[2][10]; d[1,9] = 2.345;
4.) What does the following program do?
#include#define SIZE 10 int whatIsThis(Int [ ], int); main() { int total, a[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; total = whatIsThis(a, SIZE); printf("Total of array elemnt values is %d\n", total); return 0; } int whatIsThis( int b[ ], int size ) { if (size = = 1) { return b[0]; } else { return b[ size - 1 ] + whatIsThis(b, size - 1); } }
5.) Write a function that computes and prints out the sums from the elements of an array of integers. Each element of the array contributes to one of the two sums, depending on whether the element itself is even or odd.
Your function definition should look something like this:
void print_sum ( int array_one[ ] , int size ) ;
6.) Write a program that keeps sales data for ten years by month. The two dimensional array should have a month index of size 12. Given this data, compute by sorted order the months of the year for which sales are best. ( sum January's for every year, sum February's for every year ....... etc ....... , then tell which months were best)
7.) What does the following program do?
/* Array example problem */ #include <stdio.h> #define SIZE 3 main() { int a[SIZE] = {29, 27, 23}; int i, pass, hold; printf ("Data items in original order \n"); for (i = 0; i <= 2 ; i++) { printf("%4d", a[i]); } for (pass = 1; pass <= 2; pass++) { for (i=0 ; i <= 1 ; i++) { /* two compares */ if (a[i] a[i + 1]) { hold = a[i]; a[i] = a[i + 1]; a[i+1] = hold; } } } printf ("\nData items in new order \n"); for (i = 0; i <= 2 ; i++) { printf("%4d", a[i]); } }
8.) Write a function that sums the even indexed elements and the odd indexed elements of an array of doubles. The function should print these sums out to the screen. Each element of the array contributes to one of the two sums, depending on whether the index of the element is even or odd.
The function definition should look something like this:
void print_sum ( double array_one[ ] , int size ) ;
9.) Write a program that finds the maximum and minimum elements of a two-dimensional array.
Do all of this within main( ).