Hello ladies and gents, in Accelerated C++ I have an exercise wich goes as follows:

Write a program that will keep track of grades for several students at once. The programcould keep two vectors in sync. The first should hold the student's names, and the second the final grades that can be computed as input is read. For now, you should assume a fixed number of homework grades. We'll see in 4.1.3/56 how to handle a variable number of grades intermixed with student names.

What Ive got so far is this:

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

int main()
{
	vector<string> names;
	string name;
	vector<double> grades;
	double grade;
	char ch = 'n';

	while (ch != 'Q')
	{
		cout << "First students name: " << endl;
		while (cin >> name)
		{
			names.push_back(name);
			cout << "Enter the three grades.\n";
			for (int i = 0; i < 3; ++i)
			{
				cin >> grade;
				grades.push_back(grade);
			}
			cin.clear();
			cout << "Ad another student or press Ctr 'z'." << endl;
		}

		short count = names.size();
		
		cout << setw(10) <<"Names:" << setw(10) << "Result 1:" << setw(10) << "Result 2:" 
			 << setw(10) << "Result 3:" << endl;
		for (int i = 0; i < count; ++i)
		{
			cout << setw(10) << names[i];
			for(int j = 0; j < amount; ++j)
			{
				cout << setw(10) << grades[j];
			}
			cout << endl;
		}

		cout << "Press Q to quit.\n";

		cin.clear();
		
		cin >> ch;
		ch = toupper(ch);
	}

	return 0;
}

Problem with this code is that the same grades are used for each student, I somehow have to get to the grades from the second, third, ... student and think that I need to link the vector for the names with the vector for grades, but, this is the problem. HOW do I do that.

I could write the first student with it's grades and then output them, but, I don't think that's the idea of this exercise.

Thanks for any help.

Recommended Answers

All 7 Replies

if you read the book correctly you'll find out that the grade you're working with here is a single double per student, which is calculated from the total series of grades before being stored in the vector.

Keeping 2 vectors in synch with each value in one relating to exactly one value in the other shouldn't be a problem.

if you read the book correctly you'll find out that the grade you're working with here is a single double per student, which is calculated from the total series of grades before being stored in the vector.

Ah, I see, thanks. Think I thought it was more complicated then it really was.

Keeping 2 vectors in synch with each value in one relating to exactly one value in the other shouldn't be a problem.

Is this a decent solution:

while (ch != 'Q')
	{
		cout << "First students name: " << endl;
		while (cin >> name)
		{
			names.push_back(name);
			double totGrade = 0.0;

			cout << "Enter the grades.\n";
			for (int i = 0; i < 3; ++i)
			{
				cin >> grade;
				totGrade += grade;
			}
			grades.push_back(totGrade/3);
			
			cin.clear();
			cout << "Ad another student or press Ctr 'z'." << endl;
		}

		short count = names.size();
		
		for (int i = 0; i < count; ++i)
		{
			cout << "The median grade for " << names[i] << " equals " << grades[i] << ".\n";
		}

		cout << "Press Q to quit.\n";

		cin.clear();
		
		cin >> ch;
		ch = toupper(ch);
	}

Thanks for the help.

Adding another question to this, exercise 3-2 ask the following:

Write a program to compute and print the quartiles (that is, the quarters of the numbers with the largest values, the next highest quarter, and so on) of a set of integers.

Am I correct that I have to do the following:
Enter certain numbers like 13, 25, 39 and then calculate what the values are for 1/4, 1/2 and 3/4 for each integer?

If this is the case, then what is the relation towards using vectors for this???

Thanks.

With regard to maintaining arrays, set it up so when you do something to index x in array1 you also do the something to index x in array2.

To find quartiles, I'd sort the array. Determine the total number of items in the array. Divide the total number by 4. Use that quotient to help find the index of the values in the appropriate quartile. Say for example the array held 8 items sorted in ascending order from lowest at index 0 to largest at index 7. Then 8/4 is two meaning each quartile would hold 2 items. So the lowest quartile would be indexes 0 and 1, the largest quartile would be indexes 6 and 7, etc.

Thanks Lerner,

I made the exercise with the quartiles, it works, but was wondering wether you could tell me what I could improve in it.

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> myNumbers;
	int number;

	while (cin >> number)
		myNumbers.push_back(number);
	cin.clear();

	sort(myNumbers.begin(), myNumbers.end());

	int qrt = myNumbers.size()/4;
	int quarter = qrt;
	int j = 0;
	int count = myNumbers.size()/qrt;

	for (int i = 0; i < count; ++i)
	{
		cout << "Quartiles are [" << i << "]:" << endl;
		while  (j < qrt)
		{
			cout << myNumbers[j] << " ";
			++j;
		}
		qrt += quarter;

		cout << endl;
	}

	cout << endl;
	cout << "Press enter to exit." << endl;

	cin.get();

	return 0;
}

Also, regarding to your remark on those arrays, is that made because of a mistake I made in the first exercise I talked about in this thread?

Also, I have downloaded the VC++ 2005 EE IDE and when debugging a program, I allways get a message like this:

Debugging information for 'UsingVectors.exe' cannot be found or does not match. Binary was not built with debug information.

Do you want to continue debugging?

Yes No

But then I also have the opportunity to not show this dialog again.

Could you tell me what is happening and how I can prevent this message from appearing?


Thanks for your help.

To stop your input loop you rely on the input stream to go into a fail state, which is fine. You even clear the input stream afterward, which is great. But, I believe that the data in the input stream buffer which caused the input stream to fail is still in the input stream buffer. I think you would be better off clearing the buffer as well as clearing the stream by calling the ignore() method of input streams using a large number as the parameter for ignore() or a more sophisticated parameter such as the infamous---std::numeric_limits< std::streamsize > ::max()----which requires you to the add limits header file to your program. I don't think you NEED to use such a sophisticated approach at this time in your career, a large number like 8999 or whatever will do 99+% of the time. If you become aware of the input buffer and how it works with the input stream and that you should probably clear the buffer as well as the stream when the stream fails, then I think you've learned a lot at this time.

There is going to be four quartiles, or something is wrong. Therefore I don't think you need the variable count. If you are going to labile the quartiles by number, you use the variable i for this, then I think you should probably use a range of 1-4, not 0-3 which is what i should be in your code.

You know what input to give your program, but other end users may not. Telling them what you expect them to do when, and how to stop the input loop would be helpful.

All of these are nit picking concerns. It looks as though you've learned the lesson that was trying to be taught in the current exercise.

With regard to my prior comment on sychronized arrays, it was just a comment on how to think about and handle synchronized arrays. Say the project involved using an array of 1000 competitors names and a synchronized array of the final scores they have earned during a tournament. Your job is to review the names and output the competitor's name who had the highest score and what it was. Assume the names and scores are already in the arrays and that each name and associated score have the same index in their respective array at the beginning of the exercise, that is, the competitor at index x has the score at index x. To accomplish your task you might think about sorting the array of scores from lowest to highest or highest to lowest to determine the winner rather than doing a sequential, linear search of the array of scores. The problem with sorting the array of scores is that if you don't do all the index switching with the name array at the same time you switch values in the score array you won't keep the names associated with their respective scores. So my comment was intended to be sure you understood what the concept of synchronization was in the context of the problem, that is, the arrays are synchronized by the index values of the elements within the arrays.

As to your last problem, that is compiler specific. In my version of VC++ there is an option to build (aka compile and link) the source code using debug mode or release mode. The difference is that in the debug mode there is a bunch of stuff added by the IDE to the source code you wrote to aid the debugger in it's job whereas the release mode doesn't have all the debug information in the executable file. I suspect that the same option exists in your version and that you are building in release mode, which doesn't have doesn't have the necessary information for the debugger to use, and hence the error message.

Thanks for the extended info Lerner, that really made it much clearer :!:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.