ICT102 Practical 3 - Nick Circosta Online

advertisement
ICT102 Introduction to Computer Science
Lab Practice 3
Nicholas Circosta (30491537)
13/03/06
 C, most other programming languages, has many libraries of
modules or functions that a programmer can use in their
programs. You have already been introduced to the printf(),
scanf() and pow() functions, which are part of the “Standard C
Library”.
 This week's programming exercise will be dealing with
mathematical computations. Some of these will be based on
the built-in arithmetic operators such as + and / while others
utilise the sqrt() function from the Standard C Library:
double sqrt(double x);
This defines a function called sqrt which takes a double number
(a double is a fractional type like a float but is able to store
twice as many decimal places) and finds the square root of this
number and returns the result.
You can use this function as follows:
double result;
result=sqrt(25.0);
result would now contain the value 5.0, which is the value
returned from the function sqrt().
Note that when working with double numbers you need to use
“%lf” (“long float”) as a format specifier for scanf() rather than
“%f” as you can with floats. You can use printf with either format
specifier.
-1-
ICT102 Introduction to Computer Science
1. (Problem Solving/Design Exercise)
Create an algorithm for a program that reads in two double
numbers and uses those numbers to calculate and display the
arithmetic mean, geometric mean, and harmonic mean. (hint: the
arithmetic mean is the sum of the two numbers divided by two, the
geometric mean is the square root of their product, and the
harmonic mean is the 2 divided by the sum of their reciprocals
(1/x)). Now find the largest of the three values and identify which
of the means it is.
Use the space below or a separate piece of paper.
-2-
ICT102 Introduction to Computer Science
Print “Please Enter your first number”
Store -> Number1
Print “Please enter your second number
Store -> number2
arthmean = (number1+number2) / 2
If number1 or number2 < 0
negInput = 1
Else
geomean = Sqrt(number1*number2)
harmean = 2 / ((1/number1) + (1/number2))
If arthmean > geomean && arthmean > harmean
Print “Arithmetic mean is the biggest at” & arthmean
Else
if harmean > geomean && harmean > arthmean
Print “Harmean mean is the biggest at” & harmean
Else
if geomean > harmean && geomean > arthmean
Print “geomean mean is the biggest at” & geomean
else
Print “They are all the same value” & geomean
2. Test Data and Desk Checking
-3-
ICT102 Introduction to Computer Science
Draw up a table with separate columns for the two double
numbers being read in, the results of each mean calculation and
also which mean result is the largest. For example:
n1
n2
1
1
1
2
50
100
-5
Arithmetic
Mean
1
Harmonic
Mean
Geometric
Mean
Largest
1
1
geometric
1.3
1.42
arithmetic
75
66.6
70.71
arithmetic
-5
-5
-5
N
arithmetic
-5
-4
-4.5
-4.44
N
arithmetic
5
-5
N
N
N
N
1.5
Now identify suitable test values for n1 and n2, the two numbers
being read in. These values should test different possible
scenarios and should test what are sometimes called “boundary
conditions” which means situations where the result is likely to
change. For example, if the program gives correct results for the
values (10, 20) then it seems reasonable to assume it will give
correct results for (15, 25) so it is probably unnecessary to test
both of these sets. However, you should test other possible
combinations. For example, large positive numbers, negative
numbers, whole numbers and fractional numbers (both with a
whole part and without, e.g., 0.5 vs 1.5). You may also want to test
various combinations of these although you can probably safely
assume that the order of the values is not important. Note that the
table given above probably does not have sufficient rows for
covering an adequate range of test data and should be expanded
as required!
3. (Programming Exercise)

Implement the algorithm into a C program. Although you
have a complete algorithm, you may find the easiest way to
approach implementing this in a program is to take an
incremental approach. For example, you could implement the
first part of the program which reads the values and calculates
the means. After verifying that this is working correctly using
-4-
ICT102 Introduction to Computer Science
your desk check data you can then extend your program to
report which of the three means is the largest.

You will need to include the math header file by having
the line #include <math.h> somewhere at the top of your
program.
What to Submit
1. Your algorithm, either legibly handwritten or typed (This
week).
2. Your desk check data and results.
3. Your C program (next week).
Arithmetic Operators
Operator
Use
Description
+
var1 + var2
Adds var1 and var2
-
var1 - var2
Subtracts var2 from var1
*
var1 * var2
Multiplies var1 by var2
/
var1 / var2
Divides var1 by var2
var1 % var2
Computes the remainder of dividing
var1 by var2
%
When combining operators remember that precedence becomes important.
The best thing to do is to think carefully about the order in which you want the
operations to occur and use parentheses (ordinary brackets) to clearly indicate this.
This has the advantage of meaning that you never have to worry about all the
different precedence rules and also that your intention in terms of how the code
should behave is clearly documented.
-5-
Download