Ok....I'm stuck again...I'm wondering if there is a way to sort the following code, so that I can pick out the highest value. I don't believe that I can make it into a list, since they are in parallel arrays. I'm hoping not to have to do a long set of sentences, but rather a quicker way.....Any ideas? Several people have told me that it would be much much easier if my code were in a 2D array.....how would I do that?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

double CalculateAvg (ifstream& students, string studentid[60], string fname[60], string lname[60],
					int Q1[60], int Q2[60], int Q3[60], int MidTerm[60], int Q4[60], 
					int Q5[60], int Q6[60], int Final[60], int i);

void main()
{
	ifstream students;
	ofstream out;

	string fname[60];
	string studentid[60];
	string lname[60];
	string id[60];
	int Q1[60];
	int Q2[60];
	int Q3[60];
	int MidTerm[60];
	int Q4[60];
	int Q5[60];
	int Q6[60];
	int Final[60];

	string file;

	int i;

	cout << "Enter name of file" << endl;
	cin >> file; file += ".txt";

	students.open(file.c_str());
	if(students)
	{
		out.open("CompleteReport.txt");
		out << "Student Grade Report:" << endl;
		cout << fixed << setprecision(1);
		out << fixed << setprecision(1);
		cout << endl 
			<< "StudentID    LastName    FirstName   Q1     Q2     Q3     Mid    Q4      Q5     Q6     Final    Avg.    Grade" 
			<< endl;
		cout << "--------------------------------------------------------------------------------------------"
			<<endl;
		out << "StudentID    LastName    FirstName   Q1     Q2     Q3     Mid    Q4      Q5     Q6     Final    Avg.    Grade" 
			<< endl;
		out << "--------------------------------------------------------------------------------------------"
			<<endl;

		for (i = 0; i < 6; ++i )
		{
			if (students >> studentid[i] >> fname[i] >> lname[i] >> Q1[i] >> Q2[i] >> Q3[i] >> MidTerm[i] 
					>> Q4[i] >> Q5[i] >> Q6[i] >> Final[i])
			{
				cout << left << setw(13) << studentid[i] << setw(12) << fname[i] << setw(12) << lname[i] << setw(7)
					<< Q1[i] << setw(7) << Q2[i] << setw(7) << Q3[i] << setw(7) << MidTerm[i] << setw(8)
					<< Q4[i] << setw(7) << Q5[i] << setw(8) << Q6[i] << setw(8) << Final[i] << 
					CalculateAvg (students, id, fname, lname, Q1, Q2, Q3, MidTerm, Q4, Q5, Q6, Final, i);
				out << left << setw(13) << studentid[i] << setw(12) << fname[i] << setw(8) << lname[i] << setw(7)
					<< Q1[i] << setw(7) << Q2[i] << setw(7) << Q3[i] << setw(7) << MidTerm[i] << setw(8)
					<< Q4[i] << setw(7) << Q5[i] << setw(8) << Q6[i] << setw(8) << Final[i] <<
					CalculateAvg (students, id, fname, lname, Q1, Q2, Q3, MidTerm, Q4, Q5, Q6, Final, i);

				cout << endl;
			}
			else
			{
				break;
			}
		}
		students.close();
	}
}



double CalculateAvg (ifstream& students, string studentid[60], string fname[60], string lname[60], 
					 int Q1[60], int Q2[60], int Q3[60], int MidTerm[60], int Q4[60], int Q5[60], 
					 int Q6[60], int Final[60],int i)
{	
	double average1, average2, average3, average4, average5, average6;
	int list[] = {Q1[i], Q2[i], Q3[i], MidTerm[i], Q4[i], Q5[i], Q6[i], Final[i]};
	int i;

	average1 = (Q2[i] + Q3[i] + (MidTerm[i] * 2) + Q4[i] + Q5[i] + Q6[i] + Final[i]) / 8;
	average2 = (Q1[i] + Q3[i] + (MidTerm[i] * 2) + Q4[i] + Q5[i] + Q6[i] + Final[i]) / 8;
	average3 = (Q1[i] + Q2[i] + (MidTerm[i] * 2) + Q3[i] + Q5[i] + Q6[i] + Final[i]) / 8;
	average4 = (Q1[i] + Q2[i] + (MidTerm[i] * 2) + Q3[i] + Q5[i] + Q6[i] + Final[i]) / 8;
	average5 = (Q1[i] + Q2[i] + (MidTerm[i] * 2) + Q3[i] + Q4[i] + Q6[i] + Final[i]) / 8;
	average6 = (Q1[i] + Q2[i] + (MidTerm[i] * 2) + Q3[i] + Q4[i] + Q5[i] + Final[i]) / 8;



	if (Q1[i] <= Q2[i] && Q3[i] && Q4[i] && Q5[i] && Q6[i])
			return average1;

	else if (Q2[i] <= Q1[i] && Q3[i] && Q4[i] && Q5[i] && Q6[i])
			return average2;

	else if (Q3[i] <= Q1[i] && Q2[i] && Q4[i] && Q5[i] && Q6[i])
			return average3;

	else if (Q4[i] <= Q1[i] && Q2[i] && Q3[i] && Q5[i] && Q6[i])
			return average4;

	else if (Q5[i] <= Q1[i] && Q2[i] && Q3[i] && Q4[i] && Q6[i])
			return average5;

	else if (Q6[i] <= Q1[i] && Q2[i] && Q3[i] && Q4[i] && Q5[i])
			return average6;
}

Recommended Answers

All 10 Replies

Before anything else: I told you this was wrong... if (Q1[i] <= Q2[i] && Q3[i] && Q4[i] && Q5[i] && Q6[i]) should be Q1 <=Q2 && Q1<=Q3 && Q1 <=Q4 && Q1 <=Q5 && Q1<=Q6 I know what you have compiles but that doesn't mean it works the way you want it to.

Also, I didn't notice it before but don't do void main, main should always return an int (pop a return 0; at the end of your code).

Keep the names and identifiers in their own arrays still (it wouldn't take too much work to get them into a string array string identifiers[60][3]; . You can make a scores array that is int scores[60][8]; . Those declarations just have [rows][columns] after the name. You'll have to restructure your input method slightly, by taking in your names, then setting up a small for loop to take in the scores for person1.
The tricky part is that your sorting will not include the midterm scores (right? if the midterm is lowest you wouldn't drop it), so you may want to store those next to the finals so that you can sort a contiguous block within your array.
Anyway, that gives you a head start.

Before anything else: I told you this was wrong... if (Q1[i] <= Q2[i] && Q3[i] && Q4[i] && Q5[i] && Q6[i]) should be Q1 <=Q2 && Q1<=Q3 && Q1 <=Q4 && Q1 <=Q5 && Q1<=Q6 I know what you have compiles but that doesn't mean it works the way you want it to.

Also, I didn't notice it before but don't do void main, main should always return an int (pop a return 0; at the end of your code).

Ok.....I'm on it....I can do the first part, but our professor likes to have the main program be a void main()

Would it make sense to duplicate the same process for finding the average, but instead look for the largest value that way, instead of having to convert over to a 2D array? Like this...

double CalculateAvg(...)
{
     int largest1, largest 2, largest3, largest4, largest5, largest6;

     if (Q1[i] > Q2[i] && Q1[i] > Q3[i].....)
         return largest1;
     if (Q2[i] > Q1[i] && Q2[i] > Q3[i].....)
         return largest2;

    etc......
}

and somehow mesh it with the CalculateAvg averages that were previously found?

Ok.....I'm on it....I can do the first part, but our professor likes to have the main program be a void main()

Then we can't hold it against you personally :) (and honor thy faculty members) but every time he encourages it smile to yourself and know that you secretly know better.

Ok.....I'm on it....I can do the first part, but our professor likes to have the main program be a void main()

Would it make sense to duplicate the same process for finding the average, but instead look for the largest value that way, instead of having to convert over to a 2D array? Like this...

double CalculateAvg(...)
{
     int largest1, largest 2, largest3, largest4, largest5, largest6;

     if (Q1[i] > Q2[i] && Q1[i] > Q3[i].....)
         return largest1;
     if (Q2[i] > Q1[i] && Q2[i] > Q3[i].....)
         return largest2;

    etc......
}

and somehow mesh it with the CalculateAvg averages that were previously found?

LOL! I won't....and believe me, your not the first person to tell me that either!

Ok.....I'm on it....I can do the first part, but our professor likes to have the main program be a void main()

Would it make sense to duplicate the same process for finding the average, but instead look for the largest value that way, instead of having to convert over to a 2D array? Like this...

and somehow mesh it with the CalculateAvg averages that were previously found?

Smallest, you mean? or are you dropping the largest? you could take whichever value you want to drop, multiply the average by N to get the total back again and subtract out your dropped value and divide again.

I think as long as the CalculateAverage function is working for you, stick with it. Sometimes those mile and a half long if statements are unavoidable and with the amount of rework required to get it all situated into a 2D array it might not be worth it.

Smallest, you mean? or are you dropping the largest? you could take whichever value you want to drop, multiply the average by N to get the total back again and subtract out your dropped value and divide again.

I think as long as the CalculateAverage function is working for you, stick with it. Sometimes those mile and a half long if statements are unavoidable and with the amount of rework required to get it all situated into a 2D array it might not be worth it.

Well, in our previous forum discussion, you taught me how to weed out the smallest. Now I have to find the largest value per student, double it, and add it to the average, so I was thinking something like this:

largest1 = (Q1[i] * 2);
largest2 = (Q2[i] * 2);
largest3 = (Q3[i] * 2);
largest4 = (Q4[i] * 2);
largest5 = (Q5[i] * 2);
largest6 = (Q6[i] * 2);


if (Q1[i] > Q2[i] && Q1[i] > Q3[i] && Q1[i] > Q4[i] && Q1[i] > Q5[i] && Q1[i] > Q6[i])
	return largest1;
else if (Q2[i] <= Q1[i] && Q2[i] <= Q3[i] && Q2[i] <= Q4[i] && Q2[i] <= Q5[i] && Q2[i] <= Q6[i])
    return largest2;
else if (Q3[i] <= Q2[i] && Q3[i] <= Q1[i] && Q3[i] <= Q4[i] && Q3[i] <= Q5[i] && Q3[i] <= Q6[i])
	return largest3;
else if (Q4[i] <= Q1[i] && Q4[i] <= Q2[i] && Q4[i] <= Q3[i] && Q4[i] <= Q5[i] && Q4[i] <= Q6[i])
	return largest4;
else if (Q5[i] <= Q1[i] && Q5[i] <= Q2[i] && Q5[i] <= Q3[i] && Q5[i] <= Q4[i] && Q5[i] <= Q6[i])
	return largest5;
else if (Q6[i] <= Q1[i] && Q6[i] <= Q2[i] && Q6[i] <= Q3[i] && Q6[i] <= Q4[i] && Q6[i] <= Q5[i])
	return largest6;

placed within the CalculateAvg function and possibly intertwined with the average1, average2, etc.

That seems reasonable

That seems reasonable

I'm just not sure how the code would work out though.....it seems like I would have to pass the "FindLargest" function through each Q" " 5 times.... since,

Q1 < all numbers , Q2 could be larger OR smaller than 3, 4, 5, and 6.


that's where I'm not sure how to include the "FindLargest" function...

Any suggestions?

I'm just not sure how the code would work out though.....it seems like I would have to pass the "FindLargest" function through each Q" " 5 times.... since,

Q1 < all numbers , Q2 could be larger OR smaller than 3, 4, 5, and 6.

it's actually turning out to be quite a huge, but successfully compiled mess of code, but is not returning the correct values....


that's where I'm not sure how to include the "FindLargest" function...

Any suggestions?

Can anyone think of an easier way to do this?

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.