UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

CMSC 201
Programming Project One


Out: Tuesday 9/22/98
Due: Midnight Wednesday 10/7/98

The Objective

The objective of this assignment is to get you started writing programs in C in the UNIX environment. This project will give you practice using loops, and mixing data types.

The Background

There are three common means that can be associated with a group of values. You should already be familiar with the arithmetic mean, a.k.a. the average. The arithmetic mean is calculated by dividing the sum of all the values by the number of values.

arithMean = (val1 + val2 + val3 + . . . + valn) / n

where each vali is an individual value.

The second mean we'll work with is the geometric mean. It is calculated by taking the nth root of the product of all the values. The geometric mean is useful as a measure of central tendency, because it is less affected by extreme values then is the arithmetic mean. Here is the formula for the geometric mean.

geomMean = (val1 * val2 * val3 * . . . * valn)1/n

The last mean is known as the harmonic mean. It is defined as the reciprocal of the arithmetic mean of the reciprocals. The harmonic mean is given by the formula

harmonMean = 1 / ((1/val1 + 1/val2 + 1/val3 + . . . + 1/valn) / n )

The Task

You are to write a program that calculates each of the three means for any number of integer values the user decides to enter. You should continue to get integer values from the user until a value of 0 is entered. Your program should not accept negative values from the user and should print a suitable error message and ask for another value.

Since the calculation of the geometric mean involves the taking of the nth root of the product of the values, you will need to use the pow() function found in the math library. In order to do this, you will need to #include <math.h> so that the prototype for the pow() function can be seen before a call is made to it within your program. The math libraries are not found in the same location as the other standard libraries, so you must also tell the compiler to link in the math library. This is done by compiling using the -lm option. Here is an example:

lassie% cc proj1.c -lm

The pow() function has the following prototype:

double pow(double x, double y);

where the value returned is x y.

Sample Run

retriever[102] cc means.c -lm retriever[103] a.out This program will find the arithmetic, geometric and harmonic means of the values you enter. Please enter positive integers only and 0 to end. Enter a positive integer, 0 to end :1 Enter a positive integer, 0 to end :2 Enter a positive integer, 0 to end :3 Enter a positive integer, 0 to end :10 Enter a positive integer, 0 to end :0 arithMean = 4.000000 geomMean = 2.783158 harmonMean = 2.068966 retriever[104]

Submitting the Program

To submit a file called proj1.c you would type the following at the unix prompt:

submit cs201 proj1 proj1.c

Since I did not restrict the naming of your file, you may have named your file something else. The last item in the command should be the filename of the file that you wrote for your project. So if you called your file means.c, the command would be:

submit cs201 proj1 means.c

CSEE | 201 | 201 F'98 | lectures | news | help

Tuesday, 06-Oct-1998 15:32:18 EDT