Stuck

Hey, I'm a beginner and I'm stuck on this one assignment. I've included the instructions and what I have so far but don't worry about the whole thing. The sentence in bold is what I don't know how to do. I'm also having trouble understanding the underlined portion. I know my code is probably inefficient but if I could get any help on either part it would be appreciate. Thanks!


Part A

Write a program that reads a specified number of floating point numbers from the user and
prints their average. The first number input by the user will be an integer that specifies how
many numbers are to be averaged. The rest of the input will be the floating point numbers
to be averaged. Your output should be accurate to three decimal places. For example, if the
input is:
8 1.39 5.5 9.6885 3.198 23.58684 17 -6.58 16.35
your output should be:
average = 8.767.
You must use a for loop to solve this problem.

Part B

Add the following two features to the above problem:
1. the user is able to enter an arbitrary number of data sets, and you need to average
each data set. Each data set starts with the number of observations, entered as
floating point numbers, and then the observations. When you encounter a data set
that starts with the number 0, you will exit the program.
You will need to add an
outer while loop (i.e., a while loop that surrounds your for loop) to your program.
2. the size of the data set must be between 1 and 10 inclusive. You should continually
prompt the user for a valid data size until the user enters either 0, meaning that
you should exit the program, or an integer between 1 and 10. Use a do while to
implement this prompt (you may need to put this prompt in two different places in
your program).
For example, if the input is:
8 1.39 5.5 9.6885 3.198 23.58684 17 -6.58 16.35
1 17.85
3 10 9 4
0
then your output should be
average = 8.767
average = 17.850
average = 7.667
Since each average will print as soon as the dataset for that average has been entered, your
output will not look this nice, because your output will be interspersed with your input.

Part C

You will add two more features to your program in this extra credit part:
1. In addition to printing your output, you should now graph your output as well by
printing an appropriate number of asterisks (‘*’). Since you cannot print a fractional
asterisk, you will round your average to the nearest integer. You can do this by
adding 0.5 to your average and then assigning your number to an integer variable.
C++ will automatically truncate the decimal part of the average when you do so. For
example, if your average is 7.667, then adding 0.5 to 7.667 will yield 8.167, which
will get truncated to 8.
2. At the end of your program, print the smallest and largest of the averages for your
data sets
, and the average of your averages, all to three decimal places. The average
of your averages should be the sum of the averages divided by the number of data
sets. When you are trying to compute the smallest and largest averages, you will run
into a problem of how to initialize the variables that keep track of the smallest and
largest averages. Since you do not have a bound of how large or small the averages
can be, you cannot try assigning really large or really small numbers to these
variables. My suggestion is to declare a boolean variable that keeps track of whether
this is the first average your program has computed. If it is true, then assign your
computed average to both of your tracking variables. If it is false, then check to see
whether this computed average is a new minimum or maximum average and take
the appropriate action.

For the input shown in problem 2, your output should now be:
8.767 *********
17.850 ******************
7.667 ********
smallest average = 7.667
largest average = 17.850
average of averages = 11.428


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

int main()
{
	int NumOfSets, NumOfNums, i, j, a, b;
	double Num, Total, BigTotal, Avg, AvgAvg;

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(3);

	do {
		cout << "Enter number of data sets between 1 and 10: ";
		cin >> NumOfSets;
	} while((NumOfSets < 1) || (NumOfSets > 10));



	for(i = 1; i <= NumOfSets; i++) {
		cout << endl << "How many numbers in this set? ";
		cin >> NumOfNums;
		cout << "Enter numbers: ";

		for(j = 1; j <= NumOfNums; j++) {
			cin >> Num;
			Total += Num;
		}

		Avg = Total / NumOfNums;
		BigTotal += Avg;
		cout << endl << "Average for this set is: " << Avg << endl;

		for (a = 1; a <= (Avg + .5); a++) {
			cout << "*";
		}
	}
	AvgAvg = BigTotal / NumOfSets;
	cout << endl << "Average of data sets is: " << AvgAvg << endl;
	for (b = 1; b <= (AvgAvg + .5); b++) {
		cout << "*";
	}
return 0;
}
std::vectors. They're awesome.

1
2
3
4
5
#include <vector> //Lets you use vectors, which are like resizable arrays.
vector<type> name; //Initializes a resizable array of type... type. Notice we don't specify a size.
name.push_back(value); //Add a value to the end of the resizable array, thus expanding it.
name.at(position); //Like the [] operator, except safer.
name.size(); //Get the number of elements in the vector. 


Do those snippets help?

-Albatross
Last edited on
Bolded: The instructor want you to make a while loop that averages while your input is not 0, so an example:
1
2
3
4
5
while(varible != 0)
{
do for statements
      average
}


to find your smallest and largest data sets you would use a for loop, setting the current element to a temporary variable and using an if statement to compare if it is greater than or less than the variable high and low(high starts at 0, low starts at a high number(32783 if you're using an int and want to be safe), if greater then set it to variable high, replacing the last value in high, same goes for low.
Topic archived. No new replies allowed.