Arrays Homework

For this assignment you will write a program that asks the user for an array size, and then asks the user to enter that many integers. Your program will then print out the sum and average of the values in the array. Write your program in the file arrays.c.

Fill the array by repeatedly using scanf() in a loop. Use a second loop to calculate the sum. The average will be the sum divided by the array size, but remember that you need to cast either the sum or the size to a double in the division. As a reminder, to cast the variable sum to a double you would write (double)sum. To print a double, use %lf for the conversion specification.

Note that you could write this program with a single loop and avoid using an array altogether. However, the point of this assignment is for you to practice declaring an array and using loops to access the elements of the array, so you will not get full credit if you do not use an array.

Here’s what an example run should look like:

Enter the size of the array: 4
Enter the values to put in the array: 1 4 5 7
Sum: 17
Average: 4.250000

The text of your prompts can be different, but the last two lines must follow this format exactly for the tests to pass.

Grading

Your grade will be based on the following: