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;
}
The way I understand it is that your teacher wants you to have an arbitrary number of sets, what you should be getting is not number of sets but the amount of floating point numbers that follows (see the example at the end of Part B).

The assignment seems pretty clear about what is asked of you, so it might benefit you to read through it again.
For the data set that starts with zero, you could use a string and stringstream, then use the compare function, but I don't think you want that.

For the underlined part, you'll have to create a variable to keep track of how many times you've gone through the loop. The first time, it puts the average as the highest and lowest and adds to a variable to keep the average total. If you go through the loop more than once, then you start comparing highest and lowest. At the end, you divide the variable holding the average total by the variable keeping track of how many times you've looped to get the average of averages.
But I need to know the number of sets because I have to find the average of averages (Part C).
If you count how many times you execute the loop, then you will be able to find the average of averages by dividing the total averages by how many times the loop has executed.

If you go through the loop three times, you get three averages. You total the three averages and divide them by 3 (the number of times you went through the loop) and you'll get the average of the averages.
Ahhh I gotcha. But what I really wanted to know was how to exit the program when a data set started with 0.
And I'm sure the stringstream thing would work but I need to do it using things we've already learned unfortunately
You could make the input a string. Then you would use stringstream to convert it into a number after making sure that the first character is not a 0.

1
2
3
4
5
6
7
8
9
#include <sstream>

getline(cin, input);

while(input.substr(0,1) != "0")
{
  stringstream(input) >> number;
  do calculations;
}
Yeah that looks good it just has a lot of stuff we haven't learned so he would know I didn't write it.

I started over and I have this so far (through part B1).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
	int NumOfNums, i;
	double Num, Total, Avg;
	
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(3);

	while(NumOfNums != 0){
		cout << "Enter size of data set followed by data: ";
		cin >> NumOfNums;
		for(i = 1; i <= NumOfNums; i++){
			cin >> Num;
			Total += Num;
		}
		Avg = Total / NumOfNums;
		cout << "Average: " << Avg << endl;
	}
	return 0;
}


This sort of works for the zero thing but I don't think that's really right.
You don't initialize NumofNums to anything, so when you reach line 13, NumOfNums contains junk, which could be anything, including 0.

You don't initialize Total to anything so when you reach line 18 your adding junk+Num.

You should prefer to declare variables close to their first use.
If you know arrays, then you could make the number a string and copy its to an array. Then you could go back and manually convert each character of the array into a number. It's a lot of work though. Why can't you just say you went to a website?
Last edited on
Topic archived. No new replies allowed.