CIS071
Lab08 - Simple Statistics with Arrays DUE:
10AM, Mar 25, 2006
Objectives:
Arrays,
subscripts; computing statistics from arrays.
Task:
You are given code for
program lab8_jumpstart (see below). This code allows user to enter up to 20 nonnegative
integers, calculates sum of all entered numbers, average of all entered
numbers, and finds minimum entered number.
Task 0:
Study the provided code and
try to understand how and why it works. Pay special attention to finding of the
smallest element of the array.
Task 1:
Add a piece of code to lab8_jumstart that finds the maximum element of the array (let us
call it array_max). Hint: It should look quite similar to
the code that finds the smallest element.
Task 2:
Calculate the average of all
elements of the array excluding the smallest and larges element (let us call is
array_avg_middle). Hint: It
involves some basic math. Important:
Pay attention to the required size of the array to be able to calculate this;
how large should array_size
be to be able to calculate array_avg2? Please, modify
your program to account for this.
Task 3:
Add a piece of code that
calculates the second smallest element of the array (let us call it array_min2). This is the most challenging task of todays lab.
Deliverables:Submit your program; show a few sample runs of the program.
Sample Program
(click here for lab8_jumpstart.c
file it will allow you cleaner copy-paste; however, char will probably convert to char ., so you will have to take care of that):
/* Program Title: lab8_jumpstart
*
Author: Slobodan Vucetic
*
Date: 10/19/2006
*
*
Requirements:
* Ask a user to enter
up to 20 nonnegative integers.
* If user enters a
negative number, stop the entering process.
* After user enters
20 numbers, stop the entering process.
* Calculate sum of
all entered numbers
* Calculate average
of all entered numbers
* Find the smallest
entered number
*
*
Test Plan:
* Test by entering
several different combinations of integers.
* Check if entering a
negative number stops the entering process
* Check if the
entering process is stopped after user enters 20 numbers
* Check if the
results are correct
*/
/* Preprocessor directives */
#include<stdio.h>
/* Main function */
int main(void)
{
int scores[20];
int I, flag;
int array_size,
array_sum, array_avg, array_min;
/* A procedure for populating the array */
i = 0;
flag = 0;
do {
printf(\n Enter an integer >= 0 (if integer < 0, stop)>
);
scanf(%d,&scores[i]);
printf(... you entered %d, scores[i]);
if (scores[i] > 0)
i = i+1;
else
flag = 1;
} while(i < 20
&& flag == 0);
array_size = i;
printf(\n\n***Size of the array is %d, array_size);
/* A procedure that sums all entered integers */
array_sum = 0;
for (i=0; i
< array_size; i = i+1)
array_sum = array_sum
+ scores[i];
printf(\n***
Sum of array elements is %d, array_sum);
/* Calculate the average of the entered
integers */
array_avg = array_sum/array_size;
printf(\n***Average
of the array elements is %d, array_size);
/* Find the smallest entered integer */
array_min = scores[0];
for (i=1; i
< array_size; i++) {
if (scores[i] < array_min)
array_min = scores[i];
}
printf(\n***The
smallest element of the array is %d \n\n, array_min);
return(0);
}