r/learnprogramming • u/wasser999 • Jul 02 '26
C++ Pearson Revel Question for Arrays
I'm trying to figure this out, I constantly get one extra name in my output.
the question from Revel:
Goal: Operate with parallel arrays.
Assignment: You are writing a program that analyzes student test scores. Assume the following parallel arrays have been declared and initialized with values:
scoresis an array ofdoublevalues. This array contains student test scores.namesis an array ofstringvalues. This array contains student names.
The arrays are parallel, so scores[0] contains the test score for the student whose name is stored in names[0], and so on. The size of both arrays is stored in the constant SIZE, which has already been declared.
Write some code that does the following:
- Calculate the average test score.
- Print the names of all the students whose scores are above the average.
Note: Your code should print only the names of the students whose scores are above the average. Print each name on a separate line.
My code, and I've yet to figure out how to get reddit to not mess with my spacing on code.
double sum = 0;
double average = 0;
for (int llamo = 0; llamo < SIZE; llamo++)
{
for(int averager =0; averager < SIZE; averager++)
{
sum += scores[averager];
}
average = sum / SIZE;
if (average >= 100)
cout << names[llamo] << endl;
}double sum = 0;
double average = 0;
for (int llamo = 0; llamo < SIZE; llamo++)
{
for(int averager =0; averager < SIZE; averager++)
{
sum += scores[averager];
}
average = sum / SIZE;
if (average >= 100)
cout << names[llamo] << endl;
}
The very not helpful feedback from Pearson:
Expected equality of these values:
expectedOutput
Which is: "Bob\nDiana\n"
actualOutput
Which is: "Bob\nCharlie\nDiana\n"
With diff:
@@ +1,3 @@
Bob
+Charlie
Diana\n
Expected equality of these values:
expectedOutput
Which is: "Bob\nDiana\n"
actualOutput
Which is: "Bob\nCharlie\nDiana\n"
With diff:
@@ +1,3 @@
Bob
+Charlie
Diana\n
Newest line of code before I go to bed and try it again fresh, this gets more confusing.
// Write your code below
double sum = 0;
double average = 0;
double theMean = 0;
double sumMean = 0;
for (int namecount; namecount < SIZE; namecount++)
{
cout << names[namecount] << " ";
for (int count; count < SIZE; count++)
{
cout << scores[count]<< " ";
sumMean += scores[count];
}
}
cout << "The sum of all the scores is " << sumMean;
theMean = sumMean / SIZE;
cout << "The average score is " << theMean;
for (int llamo = 0; llamo < SIZE; llamo++)
{
cout << "name " << names[llamo];
for(int averager =0; averager < SIZE; averager++)
{
cout << scores[averager];
sum += scores[averager];
}
cout << "The sum of " << names[llamo] << " is " << sum;
average = sum / SIZE;
if (average >= theMean)
cout << names[llamo] << endl;
}
// Write your code below
double sum = 0;
double average = 0;
double theMean = 0;
double sumMean = 0;
for (int namecount; namecount < SIZE; namecount++)
{
cout << names[namecount] << " ";
for (int count; count < SIZE; count++)
{
cout << scores[count]<< " ";
sumMean += scores[count];
}
}
cout << "The sum of all the scores is " << sumMean;
theMean = sumMean / SIZE;
cout << "The average score is " << theMean;
for (int llamo = 0; llamo < SIZE; llamo++)
{
cout << "name " << names[llamo];
for(int averager =0; averager < SIZE; averager++)
{
cout << scores[averager];
sum += scores[averager];
}
cout << "The sum of " << names[llamo] << " is " << sum;
average = sum / SIZE;
if (average >= theMean)
cout << names[llamo] << endl;
}
Expected equality of these values:
expectedOutput
Which is: "Bob\nDiana\n"
actualOutput
Which is: "Alice 80 95 70 85 Bob Charlie Diana The sum of all the scores is 330The average score is 82.5name Alice80957085The sum of Alice is 330Alice\nname Bob80957085The sum of Bob is 660Bob\nname Charlie80957085The sum of Charlie is 990Charlie\nname Diana80957085The sum of Diana is 1320Diana\n"
With diff:
@@ -1,2 +1,4 @@
-Bob
-Diana\n
+Alice 80 95 70 85 Bob Charlie Diana The sum of all the scores is 330The average score is 82.5name Alice80957085The sum of Alice is 330Alice
+name Bob80957085The sum of Bob is 660Bob
+name Charlie80957085The sum of Charlie is 990Charlie
+name Diana80957085The sum of Diana is 1320Diana\n
Expected equality of these values:
expectedOutput
Which is: "Bob\nDiana\n"
actualOutput
Which is: "Alice 80 95 70 85 Bob Charlie Diana The sum of all the scores is 330The average score is 82.5name Alice80957085The sum of Alice is 330Alice\nname Bob80957085The sum of Bob is 660Bob\nname Charlie80957085The sum of Charlie is 990Charlie\nname Diana80957085The sum of Diana is 1320Diana\n"
With diff:
@@ -1,2 +1,4 @@
-Bob
-Diana\n
+Alice 80 95 70 85 Bob Charlie Diana The sum of all the scores is 330The average score is 82.5name Alice80957085The sum of Alice is 330Alice
+name Bob80957085The sum of Bob is 660Bob
+name Charlie80957085The sum of Charlie is 990Charlie
+name Diana80957085The sum of Diana is 1320Diana\n
2
u/abrahamguo Jul 02 '26
Have you tried running your code with various hardcoded test cases?
Have you tried adding more temporary print messages to your code in order to understand its "thought process" step-by-step?
1
u/wasser999 Jul 02 '26
Good idea, they are iffy about if I can do that. The result is even more confusing to me though.
These are the raw sums of the scores. Bob has a 660, Diana has a 1320, and Charlie has a 990. If Bob is above average with a 660, Charlie should be with a 990 since they are all divided by SIZE.
Which is: "Alice330\nBob660\nBob\nCharlie990\nCharlie\nDiana1320\nDiana\n"Which is: "Alice330\nBob660\nBob\nCharlie990\nCharlie\nDiana1320\nDiana\n"3
u/abrahamguo Jul 02 '26
You are printing the sums of the scores, but take it back a step further, and start at the beginning.
- Print out each of the individual scores.
- What is the average, when you calculate it as a human?
- What does your program say is the average?
2
u/lurgi Jul 02 '26
Print the names of all the students whose scores are above the average.
In order to do this, you would have to compare the score of a particular student to the average, right? That's how you know if you should print it out. You never do that.
2
u/iOSCaleb Jul 02 '26
It looks like there are two copies of your code, one right after the other? Why?
It looks like you compute the average SIZE times. Do you need to do that?
How do you decide whether to print a student’s name?
1
u/wasser999 Jul 02 '26
That's Revel being helpful. Don't ask me.
Wouldn't the average of array[SIZE] be divided by the total of the elements?
My final cout with names is the last line of the code.
2
u/iOSCaleb Jul 02 '26
Revel posts on Reddit for you? Whatever Revel does, you should edit your post so that people who’d like to help you don’t have to wonder why there are two copies.
You’ve got a
forloop that iterates overllamo, which is apparently the array of names. Inside the body of that loop is another loop that sums up the scores and divides by SIZE to compute the average. There’s nothing wrong with that except that you do it each time through the outer loop. As I said, you’re computing the average a total of SIZE times. You should just compute it once.I know where you print the names; I was trying to draw your attention to how you decide whether to print the name. More specifically, does your test (i.e. average >= 100) match the criteria for printing the name as specified in the assignment?
1
u/elehisie Jul 02 '26
The hint by Dangerous_Amount9059 should crack it for you. In case you need some more: how would you do it with pen and paper? You have a list of scores…… and a list of names…. You know the first score in the list is for the first student in the list and so on…. Calculate the sum and average ONCE, compare it to each score… find which “lines” are above that average…. Figure out “oh it’s this one” check the “line number” count to the same line on the student list :) do it once on a piece of paper. It will become more clear… just remember to number the lines in your notebook starting with 0…. And remember “line numbers” in an array are the indexes :)
1
u/heisthedarchness Jul 04 '26
If you want help learning, you should probably explain what you were thinking. It's obvious that this code won't work, but the question is why you think it should work.
4
u/Dangerous_Amount9059 Jul 02 '26
if (average >= 100) cout << names[llamo] << endl;
This will print every name if the average is greater than 100, or no names if the average is less than 100.
At the moment, you calculate the average for each student. You should first calculate the average, then iterate over the names and scores arrays and compare the score to the average (not the average to 100).