CSC 270
Homework 4 in C++

Assigned Oct. 7, due Oct. 28

  1. Write a program that reads in three double numbers, prints their sum and average (appropriately labelled), and prints out how many of them are positive.  For example, if the input were
    3.7 5.2 -4.7
    the output might be
    Sum: 4.2
    Average: 1.4
    2 of your 3 numbers are positive.

    I recommend doing this by writing a sum function, an average function, and a countPositive function, each taking in three doubles. You may also want to write a read function and a printResults function to do all the I/O.

    Also write a test function that calls sum, average, and countPositive on various well-chosen test cases, compares the answers with the right answers, and prints either a message for each failed test (showing the inputs, the right answer, and the actual answer), or "All tests passed".

  2. Write a program that reads in an int, then that many double numbers into an array, prints their sum and average (appropriately labelled), prints out how many of them are positive, and prints out how many of them are larger than the average. The task of finding the sum should be done by a function named sum to which the array is passed. The counting tasks should be done by one or more functions that take in an array as a parameter. Note that in C and C++, you'll need to pass in the size of the array, as well as the array itself; this isn't necessary in Java. 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.

    Again, please write a test function that calls the other functions on hard-coded inputs, compares the answers with the correct answers, and prints an appropriate message.

    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.
    4 of your 5 numbers are larger than the average.

    Note: Since we haven't talked about pointers and dynamic memory allocation yet, declare a fixed-size array as large as it might possibly need to be.


Last modified: 
Stephen Bloch / bloch@adelphi.edu