Function project - Please provide feedback on this for me

Hey guys, brand new C++ Programmer here. For an upcoming assignment, I got this to work correctly but I would like feedback on it. If you see any errors, anything that you would have done different, please tell me because I want to learn.

The entire code is below, and this is the way the teacher wanted it. Everything as it's own function, so int main is just calling the functions. Also have to use Arrays, use a Float for average, and no pointers or loops allowed yet.

You might think the setw(30) is a little long, but he is going to be typing in a long word for those, so he wanted it that way.

I was wondering if this part was right, and when I say right I mean if the teacher would mark me off some points for overdoing something:

1
2
3
// Get Assignment Grades
	cout << endl;
	grades[NUM_ITEMS] = getAssignmentGrade(grades, assign, NUM_ITEMS);


it just didn't seem right to me with "grades" typed in twice like that (on left side, and then as variable)

Also this part below, does everything look right to you, including the return gradeNum[3]; part?

1
2
3
4
5
6
7
8
9
10
11
12

int getAssignmentGrade(int gradeNum[], string assignName[], int size)
{
	cout << "Enter the grade for " << assignName[0] << ": ";
	cin >> gradeNum[0];
	cout << "Enter the grade for " << assignName[1] << ": ";
	cin >> gradeNum[1];
	cout << "Enter the grade for " << assignName[2] << ": ";
	cin >> gradeNum[2];
	return gradeNum[3];
}


And finally here is the entire code, again please let me know if I did anything you think is odd, or "the long way" of doing it. This is only my 2nd programming assignment in the class, but so far I have an "A" grade. Thank you.

Again this does compile and work correctly for me, I am just looking for any feedback that anyone would like to provide since I am brand new at C++

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
// Headers
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// Global variables
const int NUM_ITEMS = 3;

// Function declaration
string getStudentName();
void getAssignmentName(string assignName[], int size);                    // Assignment Names
int getAssignmentGrade(int gradeNum[], string assignName[], int size);    // Assignment Grades
float getGradeAvg(float gradeAverage, int gradeNum[]);                    // Grade Average
void displayAvg(string studName, string assignName[], int gradeNum[], 
                float gradeAverage, int size);                            // Display Average

int main()
{
	// Declare variables
	string studentName,
	       assign[NUM_ITEMS];	// Assignment Names
	int grades[NUM_ITEMS];		// Assignment Grades	
	float average;			// Grade Average

	
	// Get Student Name
	studentName = getStudentName();
	
	// Get Assignment Names
	cout << endl;
	getAssignmentName(assign, NUM_ITEMS);
	
	// Get Assignment Grades
	cout << endl;
	grades[NUM_ITEMS] = getAssignmentGrade(grades, assign, NUM_ITEMS);
	
	// Get Grade Average
	cout << endl;
        average = getGradeAvg(average, grades);
	
        // Display Average
        cout << endl;
        displayAvg(studentName, assign, grades, average, NUM_ITEMS);

    return 0;
}

string getStudentName()
{
	string studentName;
	cout << "Enter student first and last name: "; 
	getline(cin, studentName);
	return studentName;
}

void getAssignmentName(string assignName[], int size)
{
	cout << "Enter the name of assignment 1: ";
	getline(cin, assignName[0]);
	cout << "Enter the name of assignment 2: ";
	getline(cin, assignName[1]);
	cout << "Enter the name of assignment 3: ";
	getline(cin, assignName[2]);
}

int getAssignmentGrade(int gradeNum[], string assignName[], int size)
{
	cout << "Enter the grade for " << assignName[0] << ": ";
	cin >> gradeNum[0];
	cout << "Enter the grade for " << assignName[1] << ": ";
	cin >> gradeNum[1];
	cout << "Enter the grade for " << assignName[2] << ": ";
	cin >> gradeNum[2];
	return gradeNum[3];
}

float getGradeAvg(float gradeAverage, int gradeNum[])
{
	gradeAverage = (gradeNum[0] + gradeNum[1] + gradeNum[2]) / 3.0f;
	return gradeAverage;
}

void displayAvg(string studName, string assignName[], int gradeNum[], float gradeAverage, int size)
{
    cout << setprecision(1) << fixed << showpoint;
    cout << "The average for " << studName << " is " << gradeAverage << endl;
    cout << endl;
    cout << "Here are your grades: " << endl << endl;
    cout << setw(30) << assignName[0] << ": " << setw(3) << gradeNum[0] << endl;
    cout << setw(30) << assignName[1] << ": " << setw(3) << gradeNum[1] << endl;
    cout << setw(30) << assignName[2] << ": " << setw(3) << gradeNum[2] << endl;
    cout << endl;
    cout << "Thank you for playing." << endl;
    cout << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
int getAssignmentGrade(int gradeNum[], string assignName[], int size)
{
	cout << "Enter the grade for " << assignName[0] << ": ";
	cin >> gradeNum[0];
	cout << "Enter the grade for " << assignName[1] << ": ";
	cin >> gradeNum[1];
	cout << "Enter the grade for " << assignName[2] << ": ";
	cin >> gradeNum[2];
	return gradeNum[3];
}


You may want to consider using a loop. and why are you trying to return gradeNum[3] from this function? Remember since you defined your array with a size of 3 the legal index values are 0, 1, and 2, an index of 3 would be accessing the array out of bounds.
You do not need to return anything from your function as you have output parameter already:
1
2
3
grades[NUM_ITEMS] = getAssignmentGrade(grades, assign, NUM_ITEMS);
//...
int void getAssignmentGrade(/*...*/)



Thank you for the feedback, I cant use loops yet per the assignment instructions, loops will be coming on a future assignment.

I removed the "grades[NUM_ITEMS] =" from
getAssignmentGrade(grades, assign, NUM_ITEMS);

Also changed "int getAssignmentGrade" to a void and removed it's return value of: return gradeNum[3];

I will post my updated, final code below. If anyone has any other advice or tips, or if you see any other mistakes feel free to share them.

I am brand new to C++ but I am learning it pretty good so far, just need more practice and feedback.

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

// Headers
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// Global variables
const int NUM_ITEMS = 3;

// Function declaration
string getStudentName();
void getAssignmentName(string assignName[], int size);                    // Assignment Names
void getAssignmentGrade(int gradeNum[], string assignName[], int size);    // Assignment Grades
float getGradeAvg(float gradeAverage, int gradeNum[]);                    // Grade Average
void displayAvg(string studName, string assignName[], int gradeNum[], 
                float gradeAverage, int size);                            // Display Average

int main()
{
	// Declare variables
	string studentName,
	       assign[NUM_ITEMS];	// Assignment Names
	int grades[NUM_ITEMS];		// Assignment Grades	
	float average;			// Grade Average

	
	// Get Student Name
	studentName = getStudentName();
	
	// Get Assignment Names
	cout << endl;
	getAssignmentName(assign, NUM_ITEMS);
	
	// Get Assignment Grades
	cout << endl;
	getAssignmentGrade(grades, assign, NUM_ITEMS);
	
	// Get Grade Average
	cout << endl;
        average = getGradeAvg(average, grades);
	
        // Display Average
        cout << endl;
        displayAvg(studentName, assign, grades, average, NUM_ITEMS);

    return 0;
}

string getStudentName()
{
	string studentName;
	cout << "Enter student first and last name: "; 
	getline(cin, studentName);
	return studentName;
}

void getAssignmentName(string assignName[], int size)
{
	cout << "Enter the name of assignment 1: ";
	getline(cin, assignName[0]);
	cout << "Enter the name of assignment 2: ";
	getline(cin, assignName[1]);
	cout << "Enter the name of assignment 3: ";
	getline(cin, assignName[2]);
}

void getAssignmentGrade(int gradeNum[], string assignName[], int size)
{
	cout << "Enter the grade for " << assignName[0] << ": ";
	cin >> gradeNum[0];
	cout << "Enter the grade for " << assignName[1] << ": ";
	cin >> gradeNum[1];
	cout << "Enter the grade for " << assignName[2] << ": ";
	cin >> gradeNum[2];
}

float getGradeAvg(float gradeAverage, int gradeNum[])
{
	gradeAverage = (gradeNum[0] + gradeNum[1] + gradeNum[2]) / 3.0f;
	return gradeAverage;
}

void displayAvg(string studName, string assignName[], int gradeNum[], float gradeAverage, int size)
{
    cout << setprecision(1) << fixed << showpoint;
    cout << "The average for " << studName << " is " << gradeAverage << endl;
    cout << endl;
    cout << "Here are your grades: " << endl << endl;
    cout << setw(30) << assignName[0] << ": " << setw(3) << gradeNum[0] << endl;
    cout << setw(30) << assignName[1] << ": " << setw(3) << gradeNum[1] << endl;
    cout << setw(30) << assignName[2] << ": " << setw(3) << gradeNum[2] << endl;
    cout << endl;
    cout << "Thank you for playing." << endl;
    cout << endl;
}
Last edited on
1. It's a good thing you removed the grades[NUM_ITEMS] = from this line is as ot only was it unnecessary, it was invalid!

grades[NUM_ITEMS] = getAssignmentGrade(grades, assign, NUM_ITEMS);

As the size of the array is NUM_ITEMS, there is no NUM_ITEMS'th element -- just 0, 1, ... NUM_ITEMS-1. So you were trying to return and a set a non-existent element just after the end of the array.

2. Are you required to have the size parameter for getAssignmentName(), getAssignmentGrade(), ... ? As the parameter is unused you could just lose it. Or you could use it to protect your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void getAssignmentName(string assignName[], int size)
{
	if(1 <= size)
	{
		cout << "Enter the name of assignment 1: ";
		getline(cin, assignName[0]);
	}
	if(2 <= size) 
	{
		cout << "Enter the name of assignment 2: ";
		getline(cin, assignName[1]);
	}
	if(3 <= size)
	{
		cout << "Enter the name of assignment 3: ";
		getline(cin, assignName[2]);
	}
}


3. gradeAverage should not be a function parameter

1
2
3
4
5
6
float getGradeAvg(int gradeNum[])
{
	// now a local variable
	float gradeAverage = (gradeNum[0] + gradeNum[1] + gradeNum[2]) / 3.0f;
	return gradeAverage;
}


Andy
Last edited on
Thanks Andy. The reason why I put "int size" on the function declaration and header is because if I did not, I was getting errors saying "too many arguments".

We did a similar assignment in class where the instructor used int size and NUM_ITEMS just like I did below. I was assuming this was correct since the instructor did it this way, but wasn't so sure about it:


 
void getAssignmentName(string assignName[], int size); 


 
getAssignmentName(assign, NUM_ITEMS);


1
2
3
4
5
6
7
8
9
void getAssignmentName(string assignName[], int size)
{
	cout << "Enter the name of assignment 1: ";
	getline(cin, assignName[0]);
	cout << "Enter the name of assignment 2: ";
	getline(cin, assignName[1]);
	cout << "Enter the name of assignment 3: ";
	getline(cin, assignName[2]);
}



I never heard of using int size before, I am going to ask more about it in class next week. But this project is due the day before the next class meets.

We cant use if statements on this project yet since we haven't covered those yet. This one has to be done real basic.

And for the assignment, everything had to be a function, so that is why getGradeAvg is a function. Would you normally just have the float getGradeAvg under int main?

thanks
Would you normally just have the float getGradeAvg under int main?

Not quite sure I get you.

It makes sense for getGradeAvg() to be a function, but you don't have to pass the variable average to it as it doesn't need it for the calculation.

What you're interested in is the result returned by the function, which you assign to average as required already.

Here you're passing the current value of average, which is going to be random as you haven't initialized it:

average = getGradeAvg(average, grades);

as the function gets a copy of the variable (as you're paasng by value rather than reference), changing it in getGradeAvg() won't have any effect on the variable in main(), so it's far better that it's made a local variable. It's a waste of time passing it to getGradeAvg().

1
2
3
4
5
6
float getGradeAvg(float gradeAverage, int gradeNum[])
{
	// variable arrives with random value and immediately get trampled on
	gradeAverage = (gradeNum[0] + gradeNum[1] + gradeNum[2]) / 3.0f;
	return gradeAverage;
}


could just write

1
2
3
4
float getGradeAvg(float gradeAverage, int gradeNum[])
{
	return (gradeNum[0] + gradeNum[1] + gradeNum[2]) / 3.0f;
}


Andy
Last edited on
I did this, and thanks that part does look better:

1
2
3
4
float getGradeAvg(float gradeAverage, int gradeNum[])
{
	return (gradeNum[0] + gradeNum[1] + gradeNum[2]) / 3.0f;
}


but the other part, are you saying to change this:

 
average = getGradeAvg(average, grades);


to this?

 
average = getGradeAvg();


because doing that gives a compiler error that says: too few arguments to function getGradeAvg(float, int*)

do I need to change something in the function declaration and header when I remove parameters from the call?

 
float getGradeAvg(float gradeAverage, int gradeNum[]); 

That's not what I said. See point 3 in my post before last.

The function signature you need is float getGradeAvg(int gradeNum[]);

Andy
Got it, I just fixed that. Thanks a lot, he probably would have taken off a point or 2 for that unneeded code.
Topic archived. No new replies allowed.