Exercise 06: Arrays

For this assignment you will write two programs, both of which begin by asking the user for an array size, then asking the user to enter that many integers, and then placing the values the user enters into an array.

Filling the Array

Since the user is choosing the size of the array, you will have to wait to declare the array until you know what the size is going to be. So both programs should start out with the following steps:

  1. Prompt the user to enter an array size
  2. Read the size using scanf()
  3. Declare the array using the size provided by the user
  4. Use a for() loop to set each element of the array using scanf()

See the example file arrays.c in the GitHub Example Repository.

array_abs.c

In the file array_abs.c, write a program that prints out the absolute values of the integers that the user entered. To get the absolute value of an int you can either use the abs() function (which requires including stdlib.h) or you can multiply the value by -1 if it is negative.

Here is what an example run should look like:

Enter the size of the array: 4
Enter the integers to put in the array: -3 5 -10 2
Here are the absolute values of the integers you entered:
3 5 10 2

array_sum_avg.c

In the file array_sum_avg.c, write a program that computes the sum and average of the integers in an array of values filled with user input. 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 otherwise the result will be an integer. To cast the variable sum to a double you would write (double)sum.

Use two loops in this program. One loop to fill up the array and another to calculate the sum. Note that you could write this program with a single loop and avoid using an array altogether. However, the point of this exercise 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 and two loops.

Here is 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

Local Testing

You can run tests for each of the programs using the following commands respectively:

make gh-test-array-sum-avg
make gh-test-array-abs

Submission

When you are confident your programs function correctly submit your work by saving your code and then using git to add the C files, commit, and pushing your changes. You can add all the files on one line if you want, like this:

git add array_abs.c array_sum_avg.c
git commit -m "This is an example commit message"
git push

You can also add them one at a time (with four individual git add commands).

Once you have pushed, check GitHub to see the results of testing your code. If any of your programs do not pass the tests, you may make changes and submit again. To submit again, simply add the file(s) that you changed, commit, and push again.

Grading

For full credit all the tests must pass

Your grade for this exercise will be out out of 10 points: