Author: Glynis Hamel Date: 3/15/96 Version: 1.0 Project ID: Documentation Sample CS Class: CS1005 Programming Language: C OS/Hardware dependencies: None Problem Description: Program inputs a list of numbers, calculates the average of the numbers, and displays those numbers from the list that are greater than or equal to the average. Overall Design: System structure N/A Data representation Input values stored in a one-dimensional array. All references to size of array made through constant macro SIZE. Algorithms Average =(sum of numbers) / (number of numbers) For-loop steps through array to compare each number to average. Program Assumptions and Restrictions: Number of values input is hard-coded. Program does not check for overflow or underflow, or for round-off errors. Program does not recover from erroneous (non-numeric) input. Interfaces: User User prompted for each input value separately. File/D-B None Program/Module N/A Implementation Details: Data Array that holds numbers is type float Variables float numbers[SIZE], float average Algorithm Sum of numbers is calculated after (rather than within,) the input loop, to separate the input and computation functions. How to build the program: gcc sample.prog.c Program Source: #include <stdio.h> #define SIZE 5 /* Program will input a list of numbers, calculate the average of the numbers, and output those numbers from the list that are greater than or equal to the average */ main () { float numbers [SIZE]; /* array to hold list of numbers */ int i; /* index into array, loop control */ float average; printf ("\nThis program will calculate the average of a list of numbers,\n"); printf ("then display those numbers from the list that are greater than\n"); printf ("or equal to the average.\n\n"); /* prompt for and accept list of numbers */ printf ("Enter %d numbers now:\n\n", SIZE); for (i=0; i < SIZE; i++) { printf ("Enter value #%d: ", i+1); scanf ("%f", &numbers[i]); printf ("\n"); } /* sum numbers, then divide by SIZE to get average */ for (i=0, average=0.0; i < SIZE; i++) average += numbers[i]; average = average / SIZE; printf ("\nThe average of the numbers is %f\n\n", average); /* compare each number to the average. Print those greater than or equal to the average */ printf ("These numbers are greater than or equal to the average...\n\n"); for (i=0; i < SIZE; i++) if (numbers[i] >= average) printf ("%f\n", numbers[i]); printf ("\nProgram terminating...\n"); } Additional Files: None. Results: Script started on Fri Mar 15 16:24:39 1996 sequoia.WPI.EDU: /users/csfaculty/ghamel/doc.standard% a.out This program will calculate the average of a list of numbers, then display those numbers from the list that are greater than or equal to the average. Enter 5 numbers now: Enter value #1: 12.7 Enter value #2: 15 Enter value #3: 5.67 Enter value #4: 9.3 Enter value #5: 13 The average of the numbers is 11.134001 These numbers are greater than or equal to the average... 12.700000 15.000000 13.000000 Program terminating... sequoia.WPI.EDU: /users/csfaculty/ghamel/doc.standard% exit exit script done on Fri Mar 15 16:24:57 1996 Test Procedures: Value of constant SIZE was changed and program recompiled to check for errors in usage of SIZE. Program was run with various data sets including all zero's, mixture of positive and negative numbers, very large and very small numbers. Test Data: Data used in sample execution (above) shows example of round-off error. Performance Evaluation: N/A Time/Space User Interface References: None. |
webmaster@cs.wpi.edu / Jul 23, 1996