# Help With an Assignment

#### Commodore

##### Deity
I am taking a Programming Essentials course and I need a little help with this week's assignment:

Design a grade average program that will produce the numerical grade average of test scores input by a user.

Your program design should contain the following:
You must use an Array as your data structure to store the input grades
You must use a Looping structure to initialize the elements of your array to clear out system garbage.
The user may input up to 5 test scores. Hint: This does not mean each user will input 5 scores. 3 scores may be entered for calculation.
You must use a Looping structure to traverse the elements of your array to produce your calculation

Now, I have the array data structure and loop just fine, but I am having problems with the rest of it. There are two things I can't seem to figure out:

1. How to get my program to accept anywhere between 1 and 5 test scores instead of requiring the user to input 5 test scores.

2. How to average the scores based on the number of scores input, instead of treating it like 5 scores were input every single time.

Our class uses RAPTOR flowcharts to program, by the way.

I would have posted this in the Computer forum, but hardly anyone posts there anymore and time is a factor so I need a quick response. I am also not asking you guys to do this for me, I just need a nudge in the right direction.

#### Kennigit

##### proud 2 boxer
Either cap it at 5 and have user choose to exit at last test score entry or prompt for N <= 5 scores and exit UI at Nth input

Average for any N is simple once you have your data in an array

#### Commodore

##### Deity
Either cap it at 5 and have user choose to exit at last test score entry or prompt for N <= 5 scores and exit UI at Nth input

Average for any N is simple once you have your data in an array

Okay I rolled back to the point where the program only asks for input for the array. Here is the C++ for what I have so far:

#include <iostream>
#include <string>

using namespace std;
int main()
{
string raptor_prompt_variable_zzyz;
?? count;

count =1;
while (!(count>5))
{
cout << raptor_prompt_variable_zzyz << endl;
count =count+1;
}

return 0;
}

I already have it capped at 5. Should I add another loop that keeps asking the user if they want to input another test score before the loop that prompts them to enter another score executes?

#### nc-1701

##### bombombedum
After 10 seconds of thinking about it here's what I would do. Assuming all your test scores are already in an array A.

Initialize:
n:=0
sum:=0

for i while A>0 #put a zero at the end of your array if this gives problems.
sum:=sum+A
i++
n++
end loop.

return(sum/n).

#### Kennigit

##### proud 2 boxer
explicitly I was thinking like

max_tests = foo
cout << "Number test scores: " << endl
cin << "%d"

if(%d > 5)
#throw statement or cout >> "error message: more than \$foo tests entered" [foo = 5]
# initilize array size %d here

#do for or while loop here of cin statements as you have
#maybe cout << "Confirm test entries " << \$array.start : array.end << endl at end to avoid ID10T errors on user's data entry

apologies for not knowing any syntax offhand, I actually just recently [as in <1 week] read up some on c++ for fun but never programmed it myself (e.g. I dunno how to explicitly require %d user entry) and am not a programmer in general

good c++ practice may be to used std::bar() rather than using namespace std when using bar() from std library though. I wouldn't know but that's what my intro guides tell me!

edit:

\n I think is more common than endl

#### Kennigit

##### proud 2 boxer
oh, also remember 0-indexing in c/c++

#### Commodore

##### Deity
Thanks guys, this was just the nudge I needed. I have completed the program and it seems to be working as intended after a dozen or so test runs.

I don't know why this one was giving me so much trouble. I am completely new to programming, but all the other assignments up until this one were a breeze.

#### Tigranes

##### Armenian
We need an official homework thread!

#### warpus

##### In pork I trust
If you can use a dynamic array, you could just check the length while you calculate the average. If the length is 2, add up 2 values and divide by 2. If the length is 3, add up 3 values and divide by 3. But since they want you to clear out the array data of "garbage" using looping, I'm assuming you can't do this.

I would also write 2 functions, one to calculate the average, and one to ask for input. Just to make everything look cleaner. I think all your other questions have been answered. Take my post as a "would be nice to have" type post.

#### Zelig

##### Beep Boop
Bored at work. Bugger manual memory management and looping. C# and Linq:

Code:
``````using System;
using System.Linq;

public class Program
{
private static void Main(string[] args)
{
if (args == null)
{
Console.WriteLine("No arguments specified.");
}
else if (args.Length < 3)
{
Console.WriteLine("Too few arguments specified.");
}
else if (args.Length > 5)
{
Console.WriteLine("Too many arguments specified.");
}
else
{
double average = Array.ConvertAll(args, double.Parse).Average();
Console.WriteLine("Average score: " + average);
}
}
}``````

#### warpus

##### In pork I trust
Hey, that's pretty slick. Now that I think about it functions would be overkill. I just always got bonus marks for adding functions, teachers seem to love them.

#### Tahuti

##### Writing Deity
Hey, that's pretty slick. Now that I think about it functions would be overkill. I just always got bonus marks for adding functions, teachers seem to love them.

Adding functions makes the code significantly more readable and maintainable. Any added processing/memory load because of new methods is negligible at best, unless you have a knack for making methods that load up new variables, which is unfortunately my achilles heel when programming.

Replies
126
Views
6K
Replies
163
Views
5K
Replies
4
Views
519
Replies
4
Views
399
Replies
119
Views
12K