## Practice Problems

### Arrays

Notice when you get compile errors (when gcc gives you an error)
vs. errors that crash the program (run-time errors)
vs. logical errors (compile and run fine but problem with your code)
1. Write a function that takes an int array and returns the sum of all the elements.
2. Write a function that takes an int array and returns the average of all the elements.
3. Write a function that takes an int array and a key to search for, and returns the index of where the key value is in the array or a -1 if it is not in the array.
4. Write a function that takes an int array and sorts the list in to order.
5. Declare a character based array called letters of ten elements
6. Assign the character value 'Z' to the fourth element of the letters array
7. Use a for loop to total the contents of an integer array called numbers which has five elements. Store the result in an integer called total.
8. Use a printf statement to print out the third element of an integer array called totals
9. Use a scanf statement to read a string of characters into the array words.
10. Use a printf statement to print out the contents of the character array called words
11. Write a for loop which will read five integers (use scanf) and deposit them into the integer based array named scores.
12. ```    (a)  What happens if you declare an array with
#define N 1000
int a[N];

(b)  What happens if you declare an array with
#define N 1000
int a[N*N];
```
13. What is in a[8] after the following code is executed?
```          for (i = 0; i < 10; i++)
a[i] = 9 - i;
for (i = 0; i < 10; i++)
a[i] = a[a[i]];
```
14. The following C program is supposed to read in a sequence of integers between 0 and 99 and return a value that occurs most frequently (mode).
Identify 7 errors.
```          void main(void) {
int num, i, maxi, a[99];
while (scanf("%2d", num) != EOF)
a[num]++;
for (i = 0; i <= 99; i++);
if (a[i] > a[maxi])
maxi = i;
printf("mode = %d\n", maxi);
}
```
15. How many elements does the array int a[5] contain? How do you access the first element? The last?

The array has 5 elements. The first is a[0]; the last is a[4].

16. What's wrong with the scrap of code in the question?
```
int a[5];
for(i = 1; i <= 5; i = i + 1)
a[i] = 0;
```

The array is of size 5, but the loop is from 1 to 5, so an attempt will be made to access the nonexistent element a[5]. A correct loop over this array would run from 0 to 4.

17. Write the statement that would declare a 10-element integer array and initialize all its elements to 1.
18. Given the following array, write code to initialize all the array elements to 88:
int eightyeight[88];
19. Write a program to print a histogram based on an int array of values.
The first number is the number that appears in the list.
The second number is how many times that number appears in the list.
Then there is a star for each time a number appears.
For example, if the number 2 exists twice in the list, then two stars appear.
If the number 3 appears 5 times, then 5 stars appear.
```2: 2    **
3: 5    *****
4: 4    ****
5: 10   **********
6: 15   ***************
7: 28   ****************************
8: 12   ************
9: 9    *********
10: 7   *******
11: 5   *****
12: 3   ***
```