CSC 270
Homework 4 in C++

  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.

    In any case, turn in sample runs with well-chosen test cases.

  2. Write a 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. The task of finding the sum should be done by a function named sum to which the array is passed. The task of counting how many are positive should be done by a function named countPos to which the array is passed. 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 1000000; 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.

    Also turn in sample runs with well-chosen test cases.

    Note: For a first attempt, declare a fixed-size array as large as it might possibly need to be. Once you've got that working, change the program so it allocates only as much memory as it needs, using a pointer rather than an array declaration.


Last modified: 
Stephen Bloch / bloch@adelphi.edu