Help with homework, professor won't respond..

closed account (LvfERXSz)
Help...
I've reached out to the professor multiple times, I've reached out to my classmates, I've reached out to classmates in other classes and nobody will give me the time of day.. It is a fully online class with 0 teacher interaction.
I think I have the structure right, but I keep having issues with my variables. Any advice or pointers are appreciated, I am very new at this.

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
  // Lab8
// 
//Program to enter student's IDs into an array, and then ask for the scores of the students, enter those into an array, then display the average of the scores and show the student with the highest score

#include <iostream>

using namespace std;

int studentID[], highINDEX, index;
double scores[];
const int array_SIZE = 7;


	int main() 
	{
		double average = 0;
		int studentID[array_SIZE];
		double scores[array_SIZE];
	
	void setStudentIDs(int studentID[]);
	
	system("pause");
	
	void inputScores(int studentID[], double scores[]);
	
	system("pause");
	
	double getAverage(double scores[]);
	
	system("pause");
	
	int getHighIndex(double scores[]);
	
	system("pause");
	
	cout << "The average score was:" << average << endl;
	cout << "The high score was student " << studentID[highINDEX] << "," << endl;
	cout << "with a score of: " << scores[highINDEX] << endl;

	system("pause");

	return 0;
}

void setStudentIDs(int studentID[])
{
while(!(index>7))
{
	cout << "Please enter Student ID:";
	cin >> studentID[index];
} 
}


void inputScores(int studentID[], double scores[])
{
	for (index = 0; index < array_SIZE; index++)
	{
		cout << "Please enter the score for Student " << studentID[index] << ":";
		cin >> scores[index];
		
		index = index + 1;
	}
	
}


double getAverage(double scores[])
{
	double average = 0;
	double sum = 0;

	while (index > 7)
	{
		sum = sum + scores[index++];
	}

	average = sum / array_SIZE;

	return average;

}


int getHighIndex(double scores[])
{
	for (index = 0; index < array_SIZE; index++)
	{
		if (scores[index] > scores[highINDEX])
			highINDEX = index;
		else
		{
			index += index;
		}
	}
	return highINDEX;
}
> I've reached out to the professor multiple times,
Did you pay them money?

> int studentID[], highINDEX, index;
> double scores[];
Remove the empty arrays.
Move the global variables into main.

> void setStudentIDs(int studentID[]);
These are function prototypes, and should be outside main()

Eg
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
#include <iostream>

using namespace std;

// this is a function prototype
void setStudentIDs(int studentID[]);

const int array_SIZE = 7;


int main() 
{
	double average = 0;
	int studentID[array_SIZE];
	double scores[array_SIZE];
	
	setStudentIDs(studentID);  // this is a call, with a parameter
	return 0;
}

void setStudentIDs(int studentID[])
{
    for ( int index = 0 ; index < array_SIZE ; index++ )
    {
	cout << "Please enter Student ID:";
	cin >> studentID[index];
    } 
}

closed account (LvfERXSz)
Thank you for your response. That helps me as I was very confused why I was unable to enter the array elements.
when you say the global variables you mean highINDEX, index, and average? When I move these into main the program does not want to compile but when I have them as global variables the program compiles but produces inaccurate information (gives average as 0 and incorrect highIndex

#include <iostream>

using namespace std;

const int array_SIZE = 7;


void setStudentIDs(int studentID[]);
void inputScores(int studentID[], double scores[]);
double getAverage(double scores[]);
int getHighIndex(double scores[]);

int main()
{
int highINDEX, index;

double average = 0;
int studentID[array_SIZE];
double scores[array_SIZE];

setStudentIDs(studentID);

inputScores(studentID, scores);

getAverage(scores);

getHighIndex(scores);

system("pause");

cout << "The average score was:" << average << endl;
cout << "The high score was student " << studentID[highINDEX] << "," << endl;
cout << "with a score of: " << scores[highINDEX] << endl;

system("pause");

return 0;
}
closed account (LvfERXSz)
and yes it is a paid course through the community college...
closed account (LvfERXSz)
Okay fixed the highIndex function, now the average function is the last issue.
Using int highINDEX, index it allows compilation when I use them as global variables but not when I put them in main

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
#include <iostream>

using namespace std;

const int array_SIZE = 7;
int highINDEX, index;

void setStudentIDs(int studentID[]);
void inputScores(int studentID[], double scores[]);
double getAverage(double scores[]);
int getHighIndex(double scores[]);

int main()
{
	double average;
	int studentID[array_SIZE];
	double scores[array_SIZE];

	setStudentIDs(studentID);

	inputScores(studentID, scores);

	getAverage(scores);

	getHighIndex(scores);

	system("pause");

	cout << "The average score was:" << average << endl;
	cout << "The high score was student " << studentID[highINDEX] << "," << endl;
	cout << "with a score of: " << scores[highINDEX] << endl;

	system("pause");

	return 0;
}

void setStudentIDs(int studentID[])
{
	for(int index = 0; index <array_SIZE; index++)
	{
		cout << "Please enter Student ID:";
		cin >> studentID[index];
	}
}


void inputScores(int studentID[], double scores[])
{
	for (index = 0; index < array_SIZE; index++)
	{
		cout << "Please enter the score for Student " << studentID[index] << ":";
		cin >> scores[index];
	}

}


double getAverage(double scores[])
{
	double sum = 0;
	double average = 0;

	for(index = 0; index < array_SIZE; index++)
	{
		sum += scores[index];
	}

	average = sum / array_SIZE;

	return average;

}


int getHighIndex(double scores[])
{
	for (index = 0; index < array_SIZE; index++)
	{
		if (scores[index] > scores[highINDEX])
			highINDEX = index;
	}
	return highINDEX;
}
closed account (LvfERXSz)
Think I got it figured out. Let me know what you think because I used global variables still..

Thank you for those few pointers though apparently that's all I needed

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
#include <iostream>

using namespace std;

const int array_SIZE = 7;
int highINDEX, index;

void setStudentIDs(int studentID[]);
void inputScores(int studentID[], double scores[]);
double getAverage(double scores[]);
int getHighIndex(double scores[]);

int main()
{
	
	int studentID[array_SIZE];
	double scores[array_SIZE];

	setStudentIDs(studentID);

	inputScores(studentID, scores);

	double average = getAverage(scores);

	getHighIndex(scores);

	system("pause");

	cout << "The average score was:" << average << endl;
	cout << "The high score was student " << studentID[highINDEX] << "," << endl;
	cout << "with a score of: " << scores[highINDEX] << endl;

	system("pause");

	return 0;
}

void setStudentIDs(int studentID[])
{
	for(int index = 0; index <array_SIZE; index++)
	{
		cout << "Please enter Student ID:";
		cin >> studentID[index];
	}
}


void inputScores(int studentID[], double scores[])
{
	for (index = 0; index < array_SIZE; index++)
	{
		cout << "Please enter the score for Student " << studentID[index] << ":";
		cin >> scores[index];
	}

}


double getAverage(double scores[])
{
	double sum = 0;
	double average = 0;

	for(index = 0; index < array_SIZE; index++)
	{
		sum += scores[index];
	}

	average = sum / array_SIZE;

	return average;

}


int getHighIndex(double scores[])
{
	for (index = 0; index < array_SIZE; index++)
	{
		if (scores[index] > scores[highINDEX])
			highINDEX = index;
	}
	return highINDEX;
}
The global variables are unnecessary and potential dangerous. In your case highINDEX is even uninitialized which may cause unexpected result. So remove the global variables (global constants are ok);
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
#include <iostream>

using namespace std;

const int array_SIZE = 7;
int highINDEX, index;

void setStudentIDs(int studentID[]);
void inputScores(int studentID[], double scores[]);
double getAverage(double scores[]);
int getHighIndex(double scores[]);

int main()
{
	
	int studentID[array_SIZE];
	double scores[array_SIZE];

	setStudentIDs(studentID);

	inputScores(studentID, scores);

	double average = getAverage(scores);

	int highINDEX = getHighIndex(scores); // Note: highINDEX

	system("pause");

	cout << "The average score was:" << average << endl;
	cout << "The high score was student " << studentID[highINDEX] << "," << endl;
	cout << "with a score of: " << scores[highINDEX] << endl;

	system("pause");

	return 0;
}

void setStudentIDs(int studentID[])
{
	for(int index = 0; index <array_SIZE; index++)
	{
		cout << "Please enter Student ID:";
		cin >> studentID[index];
	}
}


void inputScores(int studentID[], double scores[])
{
	for (int index = 0; index < array_SIZE; index++) // Note: int
	{
		cout << "Please enter the score for Student " << studentID[index] << ":";
		cin >> scores[index];
	}

}


double getAverage(double scores[])
{
	double sum = 0;
	double average = 0;

	for(int index = 0; index < array_SIZE; index++) // Note: int
	{
		sum += scores[index];
	}

	average = sum / array_SIZE;

	return average;

}


int getHighIndex(double scores[])
{
	int highINDEX = 0; // Note

	for (int index = 0; index < array_SIZE; index++) // Note: int
	{
		if (scores[index] > scores[highINDEX])
			highINDEX = index;
	}
	return highINDEX;
}
So remove the global variables (global constants are ok);

A lot of programmers speak of constant variables, which is an oxymoron, but you need to be prepared to hear this when talking to others. Global constants are not global variables: they do not vary (which eliminates the problems the global variables create)!

a little more on the subject...
OOP (classes and struct) actually work a lot like beginner programs loaded with global variables. The class members are 'global' (in a smaller, localized and protected fashion) so you will come to using those which prevents having to pass everything to every function (hooray) but protects the variables (you will see when you get there): its sort of the best of both worlds.

procedural coding (as above, functions outside of objects) require all the variables to be passed into the functions, which can get old but there is a time and a place to use the style.

Last edited on
The funny wording arises from the fact that 'variable' is an actual term used within the standard, used to differentiate from a function or struct/class definition.

So you get valid phrases like, "non-inline variable of non-volatile const-qualified type", "In any constexpr variable declaration", "Declaring a variable const can affect its linkage"
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
closed account (LvfERXSz)
Thank you everyone for your words of wisdom and input
Topic archived. No new replies allowed.