CSC 270
Homework 6
Assigned Nov. 6, due Nov. 13
In this assignment, you'll basically re-do homework 2
and homework 3 in C. Most of the code you wrote
for those assignments should still work; it'll need only minor
modifications to become legal, working C code.
Write a C program that reads in an int, then that
many double numbers into an array,
prints their sum and average (appropriately labelled), and prints out how
many of them are positive. It will then sort the array (using
bubble-sort or some other sorting algorithm of your choice) and print
out the sorted array.
Each of the following tasks should be done by a function to which the
array (and its size) is passed:
- read the doubles into the array
- compute the sum
- count how many are positive
- sort the array
- print the array
The int at the beginning should be no smaller than 0, nor
larger than 100; if it is, your program should print an appropriate
error message rather than asking for the doubles and printing
the results.
For example, if the input were
5 3.7 5.2 -4.7 -62 14.7
the output might be
Sum: -43.1
Average: -8.62
3 of your 5 numbers are positive.
After sorting, the numbers are...
-62 -4.7 3.7 5 5.2 14.7
Also turn in sample runs with well-chosen test cases.
Points to remember:
- Variables cannot be declared in the middle of a function; all the
variable declarations for a function must come before any of
the executable statements.
- There is no iostream, no cout and no cin;
instead, we #include <stdio.h> and use the standard I/O functions
printf and scanf. Note in particular that
scanf expects pointers to all of its arguments (except
character strings, which are already pointers). See the textbook, or
log onto any Unix machine and type "man 3 printf" or "man 3 scanf" to
learn more about these functions.
- There is no const declaration; instead, C programmers use
the preprocessor directive
#define VARNAME value
which creates a compile-time constant named VARNAME (you can
name it anything you want, but the convention is to use all upper-case
letters) which stands for the specified value.
Note also that the #define directive is not a
statement, so it does not end in a semicolon. It should
be on a line by itself.
Technically, it can appear anywhere before VARNAME is used, but the
convention is to put such directives near the beginning of the file,
before any variable or function declarations.
- There are no reference parameters. If a C programmer wants to pass
something that can be changed by the function, (s)he'll pass a
pointer to the object (e.g. the scanf
function mentioned above).
- The default type for floating-point numbers is float, not
double. You can still work with doubles, but
some built-in functions need to be specifically told that you're
doing so.
- There are no classes or methods, but I don't think any of you used
classes or methods for HW2 or HW3 anyway.
- There are probably other things I've forgotten; I'll try to add them
here as I think of them....
Last modified:
Sat Nov 4 14:46:25 EST 2000
Stephen Bloch / sbloch@adelphi.edu