0

Hey lsu420luv,

Sorry for the reply like 8 hours later. You see, I live in Singapore and the reply you gave was at 4am SGT. Was asleep by then.

From what I saw in your previous post, I think you still do not comprehend quite a number of the C++ concepts... Here's what I did together with an explanation:

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

// Const Fields
const int MAXMEMBERS = 20;
const int MAXJUDGES = 5;

// Globals
int pianoPlayer[MAXMEMBERS];
double score[MAXMEMBERS][MAXJUDGES];
double weightFactor[MAXMEMBERS];
int profLevel[MAXMEMBERS];
int judgeNumber[MAXMEMBERS][MAXJUDGES];
double average[MAXMEMBERS];
int weightedScore[MAXMEMBERS];
int category;  //category of music the students are playing
int judgeCount[MAXMEMBERS];
int numPlayers;

// Methods
int ReadScores(ifstream& fin);
void PrintReport(ofstream& fout);
void ProcessScores();

Ok, I've separated the globals, constants and methods. You should do that too. It makes reading them a lot easier.

Notice that I have also reduced your PrintReport method that it only takes in an ofstream as its parameter. Remember what I told you about global variables? Since all the variables required by PrintReport are global (i.e. declared outside of any method), you can access them without passing them as parameters into the method.

int main()
{
    ifstream fin("PianoREV.data");
    ofstream fout("Report.out");

    if (!fin)
    {
        cout << "Error: Input File";
        return 1;
    }
    if (!fout)
    {
        cout << "Error: Output File";
        return 1;
    }

    numPlayers = ReadScores(fin);
    ProcessScores();
    PrintReport(fout);

    fin.close();
    fout.close();
}

Look at how I've reduced your file I/O code. You can simply open an ifstream or ofstream by calling their constructors; in other words, just by doing this:

ifstream fin("PianoREV.data");
ofstream fout("Report.out");

After that, you can check if the open succeeded simply by:

if (!fin)
{
    cout << "Error: Input File";
    return 1;
}
if (!fout)
{
    cout << "Error: Output File";
    return 1;
}

Basically, putting a stream as the condition of an if statement tells you if it is still valid. Doing this:

if (!fin)

Is (sort of) the same as:

if (fin.fail())

It's just much shorter. Why "sort of"? I will explain this later.
These are also present in the ReadScores method, the one that you've been using (underlined):

int ReadScores (ifstream& fin)
{
    int i= 0;
    int j= 0;
    fin >> category;
    for (i = 0; fin && i < MAXMEMBERS; ++i)
    {
        if (!(fin >> pianoPlayer[i]))
            break;
        if (!(fin >> profLevel[i]))
            break;
        if (!(fin >> weightFactor[i]))
            break;

        // Process the judges
        for (int j = 0; fin; ++j)
        {
            if (j >= MAXJUDGES) // If we have too many judges
            {
                // If the number of judges overflow the array size,
                // we have to clear off the remaining judge entries
                // so that we can continue reading from the next
                // player entry
                int k;
                if (!(fin >> k) || k == -1)
                {
                    judgeCount[i] = MAXJUDGES; // Set judgeCount to limit
                    break;
                }
                else
                    continue;
            }
            if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
            {
                judgeCount[i] = j;
                break;
            }
            if (!(fin >> score[i][j]))
            {
                judgeCount[i] = j;
                break;
            }
        }
    }
    return i;
}

If the input (>>) and output (<<) operators makes it look confusing, think of them as methods, and that these methods return the stream you call them with. What does this mean? Whenever you do something like:

cout << "Bob" << 123 << "Tom" << endl;

First of all, the stream you work with now is cout.
You use the output operator (<<) on cout.
And with the output operator, you tell cout to print "Bob", 123, "Tom" and endl (I'll explain endl later) Understood so far?

What I mean when that these operators are like methods and the return the stream you call them with, means that I call a method something like this happens when I use << (C++ pros, don't flame me with this, it's just an abstraction for explanation):

ostream& << (ostream& s)
{
    // Some logic ...

    return s;
}

Don't get it? Basically, this:

cout << "Bob" << 123 << "Tom" << endl;

Is exactly the same as:

cout << "Bob"; cout << 123; cout << "Tom"; cout << endl;

What does all these mean to you? Basically (look carefully at where the exclamation mark goes):

    if (!(fin >> pianoPlayer[i]))
        break;

Is exactly the same as:

    fin >> pianoPlayer[i];
    if (!fin)
        break;

Which is also (sort of) the same as:

    fin >> pianoPlayer[i];
    if (fin.fail())
        break;

Why "sort of the same"? Because (look, no exclamation mark):

if (fin)

Is actually doing this:

if (fin.good())

(And of course, if (!fin) the same as if (!fin.good()), the NOT operator (!) just changes true to false and vice versa).
It's sort of the same, because fin.good() is not just the opposite of fin.fail()! It's also the opposite of fin.eof() and fin.bad(). What are these?

  • fin.eof() returns true if the End-Of-File (EOF) has been reached.
  • fin.fail() returns true if some I/O error occurred.
  • fin.bad() returns true if some fatal I/O error occurred.

So, by using if (!fin.good()), or simply if (!fin) checks for all these possible errors.

Now, what is endl? endl is the C++ representation of a newline. You should not use "\n" because this is quite restricted as different OSes use different line terminators. *nix will use "\n" but Windows "\r\n". In order to make your code compile without being OS specific, you should use endl.

I've used endl in your PrintReport method too (underlined):

void PrintReport (ofstream& fout)
{
    for (int i = 0; i < numPlayers; ++i)
    {
        fout << "Piano Player: " << pianoPlayer[i] << endl;
        for (int j = 0; j < judgeCount[i]; ++j)
        {
            fout << "Judge Number: " << judgeNumber[i][j] << "   "
                << "Score: " << score[i][j] << endl;
        }
        fout << "Average Score: " << average[i] << endl
            << "Weighted Score: " << weightedScore[i] << endl << endl;
    }
}

You were asking why your PrintReport method print every player and judge? Look at the changes (underlined):

void PrintReport (ofstream& fout)
{
    for (int i = 0; i < numPlayers; ++i)
    {
        fout << "Piano Player: " << pianoPlayer[i] << endl;
        for (int j = 0; j < judgeCount[i]; ++j)
        {
            fout << "Judge Number: " << judgeNumber[i][j] << "   "
                << "Score: " << score[i][j] << endl;
        }
        fout << "Average Score: " << average[i] << endl
            << "Weighted Score: " << weightedScore[i] << endl << endl;
    }
}

You used MAXMEMBERS and MAXJUDGES earlier, and that's why you print to MAXMEMBERS and MAXJUDGES! It's that simple a logic error. Since you said you wanted a judgeCount array, I've included it in too. Notice that this is also reflected in the ReadScores method; ReadScores will determine how many judges are there per student (underlined):

int ReadScores (ifstream& fin)
{
    int i= 0;
    int j= 0;
    fin >> category;
    for (i = 0; fin && i < MAXMEMBERS; ++i)
    {
        if (!(fin >> pianoPlayer[i]))
            break;
        if (!(fin >> profLevel[i]))
            break;
        if (!(fin >> weightFactor[i]))
            break;

        // Process the judges
        for (int j = 0; fin; ++j)
        {
            if (j >= MAXJUDGES) // If we have too many judges
            {
                // If the number of judges overflow the array size,
                // we have to clear off the remaining judge entries
                // so that we can continue reading from the next
                // player entry
                int k;
                if (!(fin >> k) || k == -1)
                {
                    judgeCount[i] = MAXJUDGES; // Set judgeCount to limit
                    break;
                }
                else
                    continue;
            }
            if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
            {
                judgeCount[i] = j;
                break;
            }
            if (!(fin >> score[i][j]))
            {
                judgeCount[i] = j;
                break;
            }
        }
    }
    return i;
}

In case that you do not know what break and continue is, break terminates the innermost loop and continues immediately on the code outside the loop. continue forces the loop to reiterate and evaluate the condition immediately, not executing the remaining code before a normal iteration.

As for ProcessScores, I've also changed it to use judgeCount. Other than that, nothing else is changed. I was unable to find any fault with the logic.

void ProcessScores ()
{
    int j;
    double totalScore;
    for (int i = 0; i < numPlayers; ++i)
    {
        for (j = 0, totalScore = 0; j < judgeCount[i]; ++j)
        {
            totalScore += score[i][j];
        }
        if (j != 0)
            average[i] = totalScore / j;
        weightedScore[i] = average[i] * weightFactor[i];
    }
}

Putting everything together, here is the full source code:

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

// Const Fields
const int MAXMEMBERS = 20;
const int MAXJUDGES = 5;

// Globals
int pianoPlayer[MAXMEMBERS];
double score[MAXMEMBERS][MAXJUDGES];
double weightFactor[MAXMEMBERS];
int profLevel[MAXMEMBERS];
int judgeNumber[MAXMEMBERS][MAXJUDGES];
double average[MAXMEMBERS];
int weightedScore[MAXMEMBERS];
int category;  //category of music the students are playing
int judgeCount[MAXMEMBERS];
int numPlayers;

// Methods
int ReadScores(ifstream& fin);
void PrintReport(ofstream& fout);
void ProcessScores();

int main()
{
    ifstream fin("PianoREV.data");
    ofstream fout("Report.out");

    if (!fin)
    {
        cout << "Error: Input File";
        return 1;
    }
    if (!fout)
    {
        cout << "Error: Output File";
        return 1;
    }

    numPlayers = ReadScores(fin);
    ProcessScores();
    PrintReport(fout);

    fin.close();
    fout.close();
}

int ReadScores (ifstream& fin)
{
    int i= 0;
    int j= 0;
    fin >> category;
    for (i = 0; fin && i < MAXMEMBERS; ++i)
    {
        if (!(fin >> pianoPlayer[i]))
            break;
        if (!(fin >> profLevel[i]))
            break;
        if (!(fin >> weightFactor[i]))
            break;

        // Process the judges
        for (int j = 0; fin; ++j)
        {
            if (j >= MAXJUDGES) // If we have too many judges
            {
                // If the number of judges overflow the array size,
                // we have to clear off the remaining judge entries
                // so that we can continue reading from the next
                // player entry
                int k;
                if (!(fin >> k) || k == -1)
                {
                    judgeCount[i] = MAXJUDGES; // Set judgeCount to limit
                    break;
                }
                else
                    continue;
            }
            if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
            {
                judgeCount[i] = j;
                break;
            }
            if (!(fin >> score[i][j]))
            {
                judgeCount[i] = j;
                break;
            }
        }
    }
    return i;
}

void ProcessScores ()
{
    int j;
    double totalScore;
    for (int i = 0; i < numPlayers; ++i)
    {
        for (j = 0, totalScore = 0; j < judgeCount[i]; ++j)
        {
            totalScore += score[i][j];
        }
        if (j != 0)
            average[i] = totalScore / j;
        weightedScore[i] = average[i] * weightFactor[i];
    }
}

void PrintReport (ofstream& fout)
{
    for (int i = 0; i < numPlayers; ++i)
    {
        fout << "Piano Player: " << pianoPlayer[i] << endl;
        for (int j = 0; j < judgeCount[i]; ++j)
        {
            fout << "Judge Number: " << judgeNumber[i][j] << "   "
                << "Score: " << score[i][j] << endl;
        }
        fout << "Average Score: " << average[i] << endl
            << "Weighted Score: " << weightedScore[i] << endl << endl;
    }
}

Ok, I admit, all the help I gave you thus far are all done without the use of a compiler. I wasn't able to get my C++ compiler up but I can give you the guarantee that the code works. It should. Unless I make some careless mistake, which I believe I didn't.

I would recommend two books if you intend to go into C++:

The first is an extremely good guide and reference (though it states only reference). I would dare to give you my fullest support that this book is the definite reference to C++ (and some C too). It even goes through some more advanced topics like the Standard Library and RTTI. The same author has written many other good books on C++, C and Java as well.
The second is also a must-have for every C++ programmer. The Standard Library is the equivalent of the Java API for C++ (though not as extensive), and no other book gives a tutorial and reference on it as worth as Mr. Josuttis'.

It's sometimes hard to find a good reference on the Internet (which is surprising), so I had to rely on these books for C++ help. In fact, these two books propelled me into programming C++.
You can also check out How Not to Program in C++ by Steve Oualline. It's a rather light-hearted good read on the common mistakes people make with C++.

I believe all's done and you can submit your work. It's been nice working with ya on this, and if there's anything else that needs to be done, don't hesitate to post it. I've sent the PM. Thanks again!

Edited by deceptikon: Fixed formatting

0

Finally got my C++ compiler working. I've compiled the code and fixed some small compilation warnings:

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

// Const Fields
const int MAXMEMBERS = 20;
const int MAXJUDGES = 5;

// Globals
int pianoPlayer[MAXMEMBERS];
double score[MAXMEMBERS][MAXJUDGES];
double weightFactor[MAXMEMBERS];
int profLevel[MAXMEMBERS];
int judgeNumber[MAXMEMBERS][MAXJUDGES];
double average[MAXMEMBERS];
double weightedScore[MAXMEMBERS];
int category;  //category of music the students are playing
int judgeCount[MAXMEMBERS];
int numPlayers;

// Methods
int ReadScores(ifstream& fin);
void PrintReport(ofstream& fout);
void ProcessScores();

int main()
{
	ifstream fin("PianoREV.data");
	ofstream fout("Report.out");

	if (!fin)
	{
		cout << "Error: Input File";
		return 1;
	}
	if (!fout)
	{
		cout << "Error: Output File";
		return 1;
	}

	numPlayers = ReadScores(fin);
	ProcessScores();
	PrintReport(fout);

	fin.close();
	fout.close();
}

int ReadScores (ifstream& fin)
{
	int i= 0;
	fin >> category;
	for (i = 0; fin && i < MAXMEMBERS; ++i)
	{
		if (!(fin >> pianoPlayer[i]))
			break;
		if (!(fin >> profLevel[i]))
			break;
		if (!(fin >> weightFactor[i]))
			break;

		// Process the judges
		for (int j = 0; fin; ++j)
		{
			if (j >= MAXJUDGES) // If we have too many judges
			{
				// If the number of judges overflow the array size,
				// we have to clear off the remaining judge entries
				// so that we can continue reading from the next
				// player entry
				int k;
				if (!(fin >> k) || k == -1)
				{
					judgeCount[i] = MAXJUDGES; // Set judgeCount to limit
					break;
				}
				else
					continue;
			}
			if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
			{
				judgeCount[i] = j;
				break;
			}
			if (!(fin >> score[i][j]))
			{
				judgeCount[i] = j;
				break;
			}
		}
	}
	return i;
}

void ProcessScores ()
{
	int j;
	double totalScore;
	for (int i = 0; i < numPlayers; ++i)
	{
		for (j = 0, totalScore = 0; j < judgeCount[i]; ++j)
		{
			totalScore += score[i][j];
		}
		if (j != 0)
			average[i] = totalScore / j;
		weightedScore[i] = average[i] * weightFactor[i];
	}
}

void PrintReport (ofstream& fout)
{
	for (int i = 0; i < numPlayers; ++i)
	{
		fout << "Piano Player: " << pianoPlayer[i] << endl;
		for (int j = 0; j < judgeCount[i]; ++j)
		{
			fout << "Judge Number: " << judgeNumber[i][j] << "   "
				<< "Score: " << score[i][j] << endl;
		}
		fout << "Average Score: " << average[i] << endl
			<< "Weighted Score: " << weightedScore[i] << endl << endl;
	}
}

Enjoy.

0

I know that I am not that good. The reason I do not understand that if (!(fin stuff is because we have not learned it yet. I am not supposed to use things we have not learned yet. Thanks for making it work though. I will check my pm's. An ideas on how to add a selection sort. I hope you check this before 12 noon tommorow because that is when it is due

0

What do you want to sort? And how do you want it sorted (ascending, descending etc.)?

0

I want to sort by weighted player average descending. Also can you change the

if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
			{
				judgeCount[i] = j;
				break;
			}
			if (!(fin >> score[i][j]))
			{
				judgeCount[i] = j;
				break;
			}

I know it seems like I dont know what I am doing, but it is only partially that. I have to do it how she wants it. Here is what I have so far. I had to change some things to setup 2d arrays for weighted scores. she wants each score for each judge weigted. Everything is fine except those lines above and the selection sort. I need a sort of average descending. Here is the code exactly how i need it except for those two things.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

// Const Fields
const int MAXMEMBERS = 20;
const int MAXJUDGES = 7;

//set text values to numbers for switch statement for CatType
typedef int CATEGORY;
const int BALLAD    = 1;
const int CONCERTO  = 2;
const int MODERN    = 3;
const int SONATA    = 4;
const int WALTZ     = 5;
//set text values to numbers for switch statement for ProfLevel
typedef int PROFICIENCY;
const int PRIMARY    = 1;
const int INTERMEDIATE  = 2;
const int ADVANCEDINTERMEDIATE    = 3;
const int SENIOR = 4;





// Globals
int pianoPlayer[MAXMEMBERS];
double score[MAXMEMBERS][MAXJUDGES];
double weightFactor[MAXMEMBERS];
int profLevel[MAXMEMBERS];
int judgeNumber[MAXMEMBERS][MAXJUDGES];
double average[MAXMEMBERS];
double weightedScore[MAXMEMBERS][MAXJUDGES];
int categoryName;  //category of music the students are playing
int judgeCount[MAXMEMBERS];
int numPlayers;

// Methods
int ReadScores(ifstream& fin);
void PrintReport(ofstream& fout);
void ProcessScores();
void SwitchCatType(ofstream& fout,CATEGORY categoryName);
void SwitchProfLevel(ofstream& fout, PROFICIENCY profLevel[MAXMEMBERS]);
void Header(ofstream& fout);
void SetFormat(ofstream& fout);

int main()
{
	ifstream fin("PianoREV.data");
	ofstream fout("Report.out");

	if (!fin)
	{
		cout << "Error: Input File";
		return 1;
	}
	if (!fout)
	{
		cout << "Error: Output File";
		return 1;
	}

	numPlayers = ReadScores(fin);
	ProcessScores();
	PrintReport(fout);

	fin.close();
	fout.close();
}

/************************************************************************************************/
/* Function Name: SetFormat									*/
/* Function Purpose: Setup formatting of fout							*/
/* Input Parameters: none									*/
/* Return value: none										*/
/************************************************************************************************/
void SetFormat (ofstream& fout)
{
	fout.setf(ios::fixed);
	fout.setf(ios::showpoint);
	fout.precision(1);
	return;
}  //end Set Format

/************************************************************************************************/
/* Function Name: Header									*/
/* Function Purpose: Output the header of file							*/
/* Input Parameters: none									*/
/* Return value: none										*/
/************************************************************************************************/
void Header (ofstream& fout)
{
	// output header to file
	fout << "    \n\n	Federation of Music Teachers\n";
	fout << " Mardi Gras Piano Invitational Competition\n";
	fout << "	 Baton Rouge, Louisiana\n\n";
	return;
} //end Header
int ReadScores (ifstream& fin)
{
	int i= 0;
	fin >> categoryName;
	while (fin >> pianoPlayer)
	// input profLEvel, weightFactor, judgeNumbers, and scores
		fin >> profLevel[i];
		fin >> weightFactor[i];
		// Process the judges
		for (int j = 0; fin; ++j)
		{
			if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
			{
				judgeCount[i] = j;
				break;
			}
			if (!(fin >> score[i][j]))
			{
				judgeCount[i] = j;
				break;
			}
		}
	}
	return i;
}

void ProcessScores ()
{
	int j;
	double totalScore;
	for (int i = 0; i < numPlayers; i++)
	{
		for (j = 0; j < judgeCount[i]; j++)
		{
			weightedScore[i][j] = score[i][j] * weightFactor[i];
			totalScore += weightedScore[i][j];
		}
		average[i] = totalScore / j;
		totalScore = 0;
		
	}
}
/************************************************************************/
/* Function Name: SwitchProfLevel					*/
/* Function Purpose: determine what level player is and output the level*/
/*		     ftom numerical to actually name ex: 1 is primary   */
/* Input Parameters: level						*/
/* Return value: none							*/
/************************************************************************/
void SwitchProfLevel(ofstream& fout, PROFICIENCY profLevel[MAXMEMBERS])
{
	int i = 0;
	switch (profLevel[i])
	{
		case PRIMARY:
			fout << " Primary\n";
			break;
		case INTERMEDIATE:
			fout << " Intermediate\n";
			break;
		case ADVANCEDINTERMEDIATE: 
			fout << " Advanced Intermediate\n";
			break;
		case SENIOR:
			fout << " Senior\n";
			break;
		default:
			cout << " Invalid Proficiency Level\n";
			break;
	}
}  //end SwitchProfLevel
/************************************************************************/
/* Function Name: SwitchCatType						*/
/* Function Purpose: determine what category player is and output       */
/*		     from numerical to actually name ex: 1 is Ballad    */
/* Input Parameters: categoryName					*/
/* Return value: none							*/
/************************************************************************/
void SwitchCatType(ofstream& fout,CATEGORY categoryName)
{
	switch (categoryName)
	{
		case BALLAD:
			fout << " Ballad\n";
			break;
		case CONCERTO:
			fout << " Concerto\n";
			break;
		case MODERN: 
			fout << " Modern\n";
			break;
		case SONATA:
			fout << " Sonata\n";
			break;
		case WALTZ: 
			fout << " Waltz\n";
			break;
		default:
			cout << " Invalid Category\n";
			break;
	}
} //end SwitchCatType
/************************************************************************/
/* Function Name: PrintReport						*/
/* Function Purpose:  Recieve arrays and print to output file		*/
/* Input Parameters: 	pianoPlayer, score, judgeNumber, judgeCount 	*/			
/* Return value: none							*/
/************************************************************************/
void PrintReport(ofstream& fout)
{
	int i=0, j=0;
	Header(fout);
	SetFormat(fout);
	for (i = 0; i < numPlayers; i++)
	{
		//output piano player and category 
		fout << "Piano Player: ";
		fout << pianoPlayer[i] << "\n";
		fout << "Category:";
		// call switch function for category and Proficiency
		SwitchCatType(fout, categoryName);
		fout << "Proficiency Level:";
		SwitchProfLevel(fout, profLevel);
		fout << "           ";
		//outpur judges' to file use switch to get correct amount
		for (j =0; j < judgeCount[i]; j++)
		{		
			fout << "Judge";
			fout.width(10);	
		}
		fout << "\n         ";
		// output judges' numbers
		for (j =0; j < judgeCount[i]; j++)
		{
			fout.width(7);
			fout << judgeNumber[i][j] << "   ";	
		}
		fout << "\n     -----------------------------------------------------------------------\n";
		fout << "         ";
		//output scores		
		for (j =0; j < judgeCount[i]; j++)
		{
			fout.width(7);
			fout << score[i][j] << "   ";
		}
		fout << "\n         ";
		//output weighted scores
		for (j =0; j < judgeCount[i]; j++)
		{
			fout.width(7);
			fout << weightedScore[i][j] << "   ";
		}
		fout << "\nWeight: " << weightFactor[i];
		fout << "\nAverage Score: " << average[i] << "\n\n\n";
	}	
	return;
} //end PrintReport

the problem is that she is a bad teacher. I usually learn things on my own and this summer will become a c++ guru. I have learned a hell of a lot of stuff on my own, but never on time restraints doing shit the way someone else wants it. For instance why would she make me use an inferior method to the one you used? No one knows, but that is neither here nor there. All your help is greatly appreciated. The program runs great.

0

Before I start working on those, let me confirm these:

  1. You want the sort to be descending, according to average . This means if I have:
    4, 6, 3, 8, 1, 5, 7
    You want it to be sorted as:
    8, 7, 6, 5, 4, 3, 1
  2. A thing to note is that, if you were to sort the average , means that every single array is implicated—they all need to be sorted in relation to the average array. A lazy version of this is to have an index, i.e., another array to store the sorted indices of the arrays. I think the second method is a far more efficient and less error prone method, but I do not know if you are allowed to do such a thing.

Also, I'm sorry if I demeaned or offended you in anyway indirectly or directly (regarding the "you seemed not to know what you're doing" part). Yes, I understand now, that your lecturer is making things difficult for you. Just wanted to clear that up too.

0

no you didnt offend me. I am just used to being the one that solves all the problems. She has led us into the darka with no flashlight. How she does the selection if this helps is by seting a low index and a target. i believe it checks to see if each one is the low value and if it is resets that as the low one. she just gave us
selection = 0
low integer = -0
j = i+1
j = 1
low index j==2, low index j =2 or something. the lowindex i value change the values. I hope this helkps. I would think it would be the sloppier. she likes to do this shit. Any thoughts on how to replace the if(!( statements. I did it for profLevel and weight factor but cannot seem to get the others.

0

I know how to do a selection sort algorithm, no worry on that. But I need answers to those two questions...

  1. Are you sure you want it descending?
  2. Do you want the sort to reshuffle ALL the arrays, or you want an additional array that stores the sorted indices of all the arrays?

I'm working on the simplification of those statements, don't worry about that.

0

also my switch for the prof level is not cycling for each player. any thoughts. I will definately be hooking you up for all this help

0

This is as simple as I can go without breaking the logic...

fin >> judgeNumber[i][j];
			if (!fin || judgeNumber[i][j] == -1)
			{
				judgeCount[i] = j;
				break;
			}

			fin >> score[i][j];
			if (!fin)
			{
				judgeCount[i] = j;
				break;
			}

I've done the sorting thing, and I believe it should work. Didn't test because I did not have the data. You must add this to the top of the file, together with the other includes:

#include <algorithm>

Then the following is the method that does this:

void SortAverage ()
{
	int mvt[MAXMEMBERS];
	for (int i = 0; i < numPlayers - 1; ++i)
	{
		double max = average[i];
		for (int j = i + 1;  j < numPlayers; ++j)
		{
			if (max < average[j])
			{
				max = average[j];
				mvt[i] = j;
			}
		}
		if (i != mvt[i])
			swap(average[i], average[mvt[i]]);
	}
	for (int i = 0; i < (numPlayers - 1); ++i)
	{
		if (mvt[i] != 0)
		{
			swap(pianoPlayer[i], pianoPlayer[mvt[i]]);
			swap(weightFactor[i], weightFactor[mvt[i]]);
			swap(profLevel[i], profLevel[mvt[i]]);
			swap(judgeCount[i], judgeCount[mvt[i]]);
			for (int j = 0; j < MAXJUDGES; ++j)
			{
				swap(score[i][j], score[mvt[i]][j]);
				swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]);
				swap(weightedScore[i][j], weightedScore[mvt[i]][j]);
			}
		}
	}
}

The switch for your SwitchProfLevel is not working because:

  1. The method should take in an int , which represents the array index of the student you want to print. This applies to SwitchCatType too.
  2. As a sideline, you don't need to pass in the array, as mentioned before, I'll mention again, profLevel is a global variable, and globals do not need to be passed into the method to be accessed. This applies to SwitchCatType too.

In otherwords, your method signature for SwitchProfLevel should be

void SwitchProfLevel(ofstream& fout, const int& i)

Hope these help.

0

Sort array? Which array?

Try this out anyway, I fixed a logic error. Hope this helps.

void SortAverage ()
{
	int mvt[MAXMEMBERS];
	for (int i = 0; i < numPlayers - 1; ++i)
	{
		double max = average[i];
		for (int j = i + 1;  j < numPlayers; ++j)
		{
			if (max < average[j])
			{
				max = average[j];
				mvt[i] = j;
			}
		}
		if (mvt[i] != 0)
			swap(average[i], average[mvt[i]]);
	}
	for (int i = 0; i < (numPlayers - 1); ++i)
	{
		if (mvt[i] != 0)
		{
			swap(pianoPlayer[i], pianoPlayer[mvt[i]]);
			swap(weightFactor[i], weightFactor[mvt[i]]);
			swap(profLevel[i], profLevel[mvt[i]]);
			swap(judgeCount[i], judgeCount[mvt[i]]);
			for (int j = 0; j < MAXJUDGES; ++j)
			{
				swap(score[i][j], score[mvt[i]][j]);
				swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]);
				swap(weightedScore[i][j], weightedScore[mvt[i]][j]);
			}
		}
	}
}
0

What is the statement with cont int& i what does that mean? The sorting still gives me segmentation fault. If i use this it causes an infinite loop. Why?

while (judgeNumber[j] != -1)
{
fin >> score[j];
fin >> judgeNumber[j];
}

I subsituted that for the if(1( lines my teacher counted off for those because I wasnt supposed to know how to do that yet

Edited by happygeek: fixed formatting

0

Can't say for sure based on the snippet you posted, but based on what you have posted the most likely scenario is the information you are reading from doesn't have a -1 value to be read into judgeNumber[][]

Segmentation faults frequently occur when you try to overread an array, that is, when you try to access an invalid index of an array.

const int& i could be used when you want a variable of type referece (the & character) to type int with name i and you don't want the value of i to change, that is you want i to be constant, so you use the const keyword.

0

any ideas on how to get the selection sort to work. And it should be reading in a -1. There is one .

0

Selection sort strategy---find the element with the extreme value (low or high) that hasn't been sorted yet and swap it with the firt unsorted element.

Pseudocode for a selectionSort:

void selectionSort(arrayType * arrayName, int array_size)
  int i, j;   //loop counters
  int index;  //index of the minimal (or maximum if you are doing descending sort) value 

  declare outer loop to control what element you start at within arrayName.  i should range from zero to arraySize minus two.
  In the outer loop body:
       assume that current element is the extreme (smallest or largest, whatever) so assign i to index
       use an inner loop to look at all element in array with indexes higher than i (j should range from i + 1 to arraySize minus 1)
            in inner loop body:
                if(arrayName[index] less than (or greater than depending on whether sorting in ascending or descending order) arrayName[j])
                   assign j to index
      when inner loop is done the element at arrayName[index] will be the extreme value (low or high) of the unsorted elements
           if(index is not the same as i)
               then need to swap elements with indexes of index and i in all appropriate parallel arrays

Pseudocode to swap elements of an array:
void swapElements(int i, int j, arrayType1 * array1Name)
  arrayType1  temp

  temp = array1Name[i]
  array1Name[i] = array1Name[j]
  array1Name[j] = temp
0

Is there a way to make just the weightedd average be a fout.precision(2);?I trid to just put that in front but that obviously int the way to do it. thanks

0

nevermind on the precision thing i fixedi it. I am completely lost on the selection sort though. Here is what I got. It is incomplete. Can you point me in the right direction?

void SelectionSort()
{
	int numPlayers;
	int i, j;
	int lowIndex, temp;
	int mvt[MAXMEMBERS];
	for (int i = 0; i < (numPlayers - 1); i++)
	{
		lowIndex=i;
		for (numPlayers=0; numPlayers < i-1; numPlayers++)
		{
			if (mv
		if (mvt[i] != 0)
		{
			swap(pianoPlayer[i], pianoPlayer[mvt[i]]);
			swap(weightFactor[i], weightFactor[mvt[i]]);
			swap(profLevel[i], profLevel[mvt[i]]);
			swap(judgeCount[i], judgeCount[mvt[i]]);
			for (int j = 0; j < MAXJUDGES; ++j)
			{
				swap(score[i][j], score[mvt[i]][j]);
				swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]);
				swap(weightedScore[i][j], weightedScore[mvt[i]][j]);
			}
		}
	}
}
0

I have fiddlesd with it, but am obviously missing some points Here it is now::;

void SelectionSort(double WeightedScore[MAXMEMBERS], int numPlayers)
{
	int i, j;
	double indexOfNextSmallest;
	for (int i = 0; i < (numPlayers-1); i++)
	{
		for (j = i+1; j < numPlayers+1; j++)
		{
			indexOfNextSmallest = indexOfSmallest(weightedScore, startIndex, numPlayers);
			SwapValues(weightedScore[i], weightedScore[indexOfNextSmallest]);
		}
	}		
}
void SwapValues(int& v1, int& v2)
{
	int temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}
double indexOfSmallest(double weightedScore[MAXMEMBERS], int startIndex, int numPlayers)
{
	int min = weightedScore[startIndex],
		indexOfMin = startIndex;
	for (i = startIndex +1; i < numPlayers; i++)
	{	
		if (weightedScore[i] < min)
		{
			min = weightedScore[i];
			indexOfMin = index;
		}
	}
	return indexOfMin;
}

I dont even know if I am using the right arrays. it is do tommorow by noon so I may be screwed. It is the revision I got 18 out of 20 already, but really need to get the 20. Thanks

0
for (int i = 0; i < (numPlayers-1); i++)  //outer loop---first unsorted element
{
   indexOfNextSmallest = i;  //assume index i is the next smallest of the unsorted elements
   for (j = i+1; j < numPlayers; j++)  //inner loop---find smallest value of unsorted elements
   {
     if (weightedScore[j] < weightedScore[indexOfNextSmallest]) 
        indexOfNextSmallest = j;
  }
  if(i != indexOfNextSmallest)  //if i isn't the index of the next smallest
  {
     //swap elements at index i and indexOfNextSmallest in all of the parallel arrays
     swapScores(i, indexOfNextSmallest, weightedScore);
     swapLastNames(i, indexOfNextSmallest,  lastName);
     swapFirstNames(i, indexOfNextSmallest, firstName);
     //etc
   }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.