Hello amfedor12,
With all that has been said here is something that you can compare with your code. The comments in the code should help.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <iostream>
#include <iomanip> // --- Added.
using namespace std; // <--- Best not to use.
//const int array_SIZE = 7;
constexpr int ARRAY_SIZE{ 3 }; // <--- Using the C++ 2011 standards. Original value 7.
//int highINDEX, index; // <--- Moved to Main.
//using STUDID = int[ARRAY_SIZE];
//using SCORES = double[ARRAY_SIZE];
//void setStudentIDs(STUDID& studentID); // <--- Do the same in the function.
void setStudentIDs(int studentID[]);
void inputScores(int studentID[], double scores[]);
double getAverage(double scores[]);
int getHighIndex(double scores[], int highIndex);
int main()
{
int highIndex{}, index{}; // <--- ALWAYS initialize your variables. All of them
int studentID[ARRAY_SIZE]{ 123456, 234567, 345678 };
double scores[ARRAY_SIZE]{ 90, 95.5, 98 };
std::cout << std::fixed << std::setprecision(2); // <--- Added for the output.
//setStudentIDs(studentID);
std::cout << '\n';
//inputScores(studentID, scores);
std::cout << '\n';
double average = getAverage(scores);
getHighIndex(scores, highIndex); // <--- Sending "highIndex" which is not needed
// and not capturing the return value of the function.
// "highIndex" should be defined in the function.
system("pause");
cout
<< "\n"
"The average score was: " << average << '\n'
<< "The high score was student " << studentID[highIndex]
<< ", with a score of: " << scores[highIndex] << "\n\n\n";
system("pause");
return 0;
}
void setStudentIDs(int studentID[])
{
for (int index = 0; index < ARRAY_SIZE; index++) // <--- Best to define the loop iterator in the for statement.
{
cout << "Please enter Student ID: ";
cin >> studentID[index];
}
}
void inputScores(int studentID[], double scores[])
{
for (int index = 0; index < ARRAY_SIZE; index++)
{
cout << "Please enter the score for Student " << studentID[index] << ": ";
cin >> scores[index];
}
}
double getAverage(double scores[])
{
double sum{};
double average{};
for (int index = 0; index < ARRAY_SIZE; index++)
{
sum += scores[index];
}
average = sum / ARRAY_SIZE;
return average;
}
int getHighIndex(double scores[], int highIndex)
{
for (int index = 0; index < ARRAY_SIZE; index++)
{
if (scores[index] > scores[highIndex])
highIndex = index;
}
return highIndex;
}
|
For line 37 the function is working correctly, but you are not collecting the returned value of the function. So on line 47 its value would ne garbage for an uninitialized variable or (0)zero for an initialized variable because it never receives a proper value.
Lines 10 and 11 create user defined variables, I believe that is what it is called, and line 12 shows you how to use it. Also in "main" you just need
STUDID studentID{};
. One advantage to this is the ability to pass the arrays be reference. When you define
void setStudentIDs(int studentID[])
this degrades to a pointer. By using
void setStudentIDs(STUDID& studentID)
you can see the entire array in the function and not just the first element or the 1 that the for loop has indexed.
Lines 22 and 23 are set up for testing. This way when you comment lines 27 and 33 you can concentrate on the rest of the program because you know the the input functions work and it is easy to change the numbers for different results.
The header file I added and line 25 set the number of decimal places the print in the output. Line 25 only needs done once, but you can still change "setprecision" any time that you need to.
Something to think about.
Andy