Question: Vectors continued...

So I know this code looks atrocious... but I'll try to present my MAIN problem as specifically as I can.

Here's the code:

 ``123456789101112131415161718192021222324`` ``````#include "std_lib_facilities.h" int main() { cout<<"What is your desired grade?\n"; double desired; cin>>desired; cout<<"What were your latest scores?\n"; vectorgrades; double temp; while(cin>>temp) grades.push_back(temp); cout<>grades.size(); cout<<"You need a "<<(grade_size*desired)-(grades[1]+grades[2]); //COMPLETELY LOST }``````

I'm trying to create a program that allows my roommates to determine the grades they need to make their "desired" grade for the class.

The math isn't my issue, it's the coding.

I suppose that most of my issues begin at lines 18-23.
You can make out what I'm trying to do. It's beyond my knowledge (as I am a beginner).

1) Can I push grades.size() into an int object?
2) How can I go about calling specific values in the vector "grades"?

Thank you for your time and help.

-Incline

This was a fun project.

There is an issue with your math.
And you'll need one function from the <numeric> header.

Also, your I/O could use a little help, since you are requiring it to terminate to get grades. I'll give that to you for free:

 ``1234567891011121314151617181920212223242526272829`` ``````#include #include #include #include #include using namespace std; int main() { cout << "What were your latest scores?\n"; vector grades; while (true) { cout << "Exam " << (grades.size() + 1) << "> "; string s; getline( cin, s ); if (s.empty()) break; istringstream ss( s ); double temp; ss >> temp; if (!ss) { cout << "Er, try again or press ENTER to stop entering grades.\n"; continue; } grades.push_back( temp ); } ``````

For the remainder, some math:

The current grade is the average of all the scores, which is (sum of scores)/(number of scores).

Get the sum of scores using `double sum = accumulate( grades.begin(), grades.end(), 0 );`
So the average is: `double current_grade = sum / grades.size();`
Slick, huh?

Once you know that, tell the user what his current grade is before asking more questions.

Now you need to know two things:
- how many exams remain?
- what grade does the user want?

Now for the math:

desired = (sum + x) / (grades.size() + exams_remaining)

Solve for x.

Then the minimum grade required for each remaining exam is (x / exams_remaining).

Hope this helps.

Declare variables at the very top of functions, methods, and code blocks. Don't use doubles in a for loop because they hold decimal places which you clearly aren't using in your increment statement. If you need to loop a ridiculous amount of times, use a long, or long long.

You can access a vector in two ways. Using the subscript operator or using an iterator.

 ``123456789101112`` ``````std::vector foo; for (std::vector::iterator it = foo.begin(); it != foo.end(); ++it) std::cout << *it; or std::vector foo; for (int i = 0; i < foo.end(); ++i) std::cout << foo[i];``````

Last edited on
 Declare variables at the very top of functions

What a terrible idea. Variables should always be created as locally as possible.
@Duoas

Thanks for your very detailed response. I appreciate the time you put into it.

Why did you use a "while" loop to set the ball rolling? It strikes my curiosity.
Er, the same reason you used a while loop: you are getting input for a list of things.

 `Declare variables at the very top of functions`

What a terrible idea. Variables should always be created as locally as possible.

I wouldn't say that's a terrible idea.

Variables should be declared as locally as possible, yes, but except for throw-away variables (like the stringstream and temp variables in my input loop above) the local context typically is the function.

I don't much care for the 'declare a variable off the cuff as you go' kind of programming. By keeping variable declarations as close together at the top of the local context, you make it easier to maintain your code. It forces you to think about the algorithm instead of cobbling stuff together.
Topic archived. No new replies allowed.