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:

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 "std_lib_facilities.h"

int main()
{
	cout<<"What is your desired grade?\n";
	double desired;
	cin>>desired;
	
	cout<<"What were your latest scores?\n";
	
	vector<double>grades;
	double temp;
	while(cin>>temp)
		grades.push_back(temp);
	
	cout<<grades.size()<<" values have been given.\n";
	
	for (double i=0; i<grades.size(); ++i)
	
	double grade_size;
	grade_size>>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:

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
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	cout << "What were your latest scores?\n";
	vector <double> 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.
Some comments...

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.

1
2
3
4
5
6
7
8
9
10
11
12
std::vector<int> foo;

for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it)
    std::cout << *it;


or

std::vector<int> 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.