You are struggling a little with how the language works and some conceptual ideas.
First, I hope you are actually using proper indentation. This will help you significantly.
Call
srand(time(0));
first thing in
main()
-- it should not appear in any function.
names
int indexOfHighest;
does
not call any function. All it does is declare an integer variable. Part of what is hurting you is not being
exact about what is going on with a given line of code. If you are unclear or mistaken about what a thing is doing then it will be impossible to reason about it.
// calling the functions
This is both vague and incorrect. Be
specific. Improvement:
1 2 3 4
|
// These two will be the indices of the largest and smallest
// values in the TestScores[] array, respectively.
int indexOfHighest;
int indexOfLowest;
|
//Test overload function
You aren't overloading any functions. You also are not testing anything. Improvement:
|
// Find the indices of the largest and smallest values in TestScores[].
|
sum
This name says you are adding something. But you are not.
You are actually finding the indices of the largest and smallest values in TestScores[].
Find a better name for your function. The more obvious and droll the better:
1 2 3 4 5
|
find_index_of_largest_and_smallest
find_largest_and_smallest
get_indices_of_max_and_min
get_max_and_min
// and so on
|
functions
The purpose of a function is one or both of:
1. Perform some action
2. Obtain some value
Your 'sum' function attempts to return two values. You have already noticed that you are returning a value (named "value") that you are completely ignoring. This is a good clue that you don't need to
return
the value. Change the function to void.
(The values returned are returned via reference arguments, which is a completely separate mechanism for returning values.)
1 2 3 4 5
|
void get_largest_smallest_indexes(int TestScores[], int ArraySize, int& IndexHighestScore, int& IndexLowestScore)
{
...
// no return statement
}
|
Then, in
main()
, you don't need to create a bogus variable:
1 2
|
// Find the indices of the largest and smallest values in TestScores[]
get_largest_smallest_indexes(TestScores, NUMBER_OF_GRADES, indexOfHighest, indexOfLowest);
|
Okay, so there is another purpose to a function:
3. Not repeat common code
You have two completely different functions just to print the student scores. Unless this is a requirement for your homework, you really only need one. Combine them.
1 2 3 4 5 6 7 8 9 10 11
|
// Create our random collection of student grades and display what we have
createScores(TestScores, NUMBER_OF_GRADES);
displayScores(TestScores, NUMBER_OF_GRADES, -1, -1);
...
// Find the indices of the largest and smallest values in TestScores[]
get_largest_smallest_indexes(TestScores, NUMBER_OF_GRADES, indexOfHighest, indexOfLowest);
// Display every score except the two we found
displayScores(TestScores, NUMBER_OF_GRADES, indexOfHighest, indexOfLowest);
|
Notice also that the create and display functions no longer use the global value NUMBER_OF_GRADES -- instead they take the number of grades as argument.
keep related things together
It is easy to mix I/O in with everything, but this just makes life a headache.
You have a function that is designed to display scores -- it's whole purpose is to generate an output table.
Yet, you have the stuff that displays the header to that table
somewhere else (in
main()
). Why is that?
Keep related stuff together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void displayScores(int scores[], int nscores, int ignoreIndex1, int ignoreIndex2)
{
cout << "Student Scores\n";
cout << "Index Score Grade Status\n";
cout << "------------------------------------\n";
for (int i = 0; i < nscores; i++)
{
cout << setw( 6 ) << left << i;
cout << setw( 6 ) << left << scores[i];
cout << setw( 6 ) << left << LetterGrade(scores[i]);
if (i == ignoreIndex1 || i == ignoreIndex2)
{
cout << "Omitted\n";
}
else
{
cout << "Used\n";
}
}
cout << "------------------------------------\n";
}
|
The
setw()
manipulator is designed to provide a
minimum number of characters output. Notice how I adjusted the code to line things up under the header.
Also notice how I changed the argument names. I did that on purpose to point out that formal arguments are different things than the actual arguments supplied to a function -- name means nothing except a hint to the human programmer as to what the thing represents.
I could have two arrays. One for grades from class A and one from class B. But I can reuse the functions for either array.
1 2 3 4 5 6 7 8 9
|
int classAScores[NUMBER_OF_GRADES];
int classBScores[NUMBER_OF_GRADES];
...
cout << "Class A\n";
displayScores(classAScores, NUMBER_OF_GRADES, -1, -1);
cout << "Class B\n";
displayScores(classBScores, NUMBER_OF_GRADES, -1, -1);
|
final notes
You are otherwise doing pretty well for a start. The basic design of your "find index of largest & smallest" and print functions are pretty good.
Well, that's enough from me. Hope this helps.