Lab 5: Integer arrays

Goals

Demonstrate proficiency in reading values into an array and then processing the values that were read in.

Background

Arrays

The array is a mechanism for creating a collection of varibles that occupy adjacent locations in computer memory.

To create an array of 20 integers the statement:

int my_array[20];

may be used.

A subscript is used to access a specific value within an array. To set element 13 of my_array to 99 the statement:

my_array[13] = 99;

may be used.

Valid subscripts are integer constants, variable, or expressions having values in the range 0,1,2, ... size-1 where size is the number of elements in the array.

Assignment:

Program p14.c on page 87 of the notes should be used as a starting point for this assignment. Write a program called lab5.c that creates an array of twenty integers and then reads an unknown number of integer values into the array. (The number of values is guaranteed to be less than or equal to 20.)

The program should print three integer values:

the number of values read;

the average of the values; and

the largest input value which is less than or equal to the average.

Step 1:

Begin by writing a program that reads an unknown number of values into the array and prints the number of values that were read.

When and only when you and your TA are convinced that this much is correct...

Step 2:

Add a second while loop that will compute and print the average of the values that were read.

When and only when you and your TA are convinced that this much is correct...

Step 3:

Add a third while loop that will find the largest value in the input that is less than or equal to the average.

Turn In Work

Show your TA that you completed the assignment. Then turn in your lab5.c program using the command:

sendlab.101.section_number 5 lab5.c

2

Mike Westall

2007-09-03T18:26:51