FUNCTION OVERLOADING:Would this have been the best way to print the name and gpa or was there an easier way

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

using namespace std;


struct studentType
{
	string name;
	double gpa;
};

// this function returns the biggest integer in the list.
int getMax(int list[], int size)
{
	int result = list[0];

	for (int i = 0 ; i < size ; i ++)
	{
		if (list[i] > result)
			result = list[i];
	}

	return result;
};

double getMax(studentType list[], int size)
{
	double result = list[0].gpa;

	for (int i = 0 ; i < size ; i ++)
	{
		if (list[i].gpa > result)
			result = list[i].gpa;
	}

	return result ;
};

// (a) overload the getMax function to return the student 
// with the highest gpa
//...

int main()
{
	const int SIZE = 6;
	int numbers[SIZE] = {11, 33, 444, 55, 66 -100};
	studentType students[3] = {{"john", 3.01}, {"alice", 3.9},  {"liz", 2.5}};

	cout << getMax(numbers, SIZE) << endl;	
	for(int i = 0; i < 3;i++){
		if(students[i].gpa == getMax(students,3)){
			cout<<students[i].name<<"\t"<<students[i].gpa<<endl;
		}


	}//ends for loop
	
	

	// (b) call the getMax function using students list and 
	// print the name and gpa of the student with the highest gpa
	//...
 

	system("pause");
	return 0;
}
Last edited on
You reads "studends" array twice.
It would be better if getMax returns not max value but index with max value.
so in main() you could use
1
2
int x = getMaxIdx(students,3);
cout<<students[x].name<<"\t"<<students[x].gpa<<endl;

So you can avoid second loop. It can seed up your program if you use big arrays.
Where am I reading in students array twice? The first array was a test trial only and that array only holds just numbers. Can you explain what do you mean by that?Thanks
Where am I reading in students array twice?

Actually I think you're reading it 3 times :). Line 52 is executed 3 times (once for each time through the loop) and each time it calls getMax() which reads through the entire students array.
Your right dhayden. Now I see it means. I took the advice of Konstantin32 and rewrote the code.

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

using namespace std;


struct studentType
{
	string name;
	double gpa;
};

// this function returns the biggest integer in the list.
int getMax(int list[], int size)
{
	int result = list[0];

	for (int i = 0 ; i < size ; i ++)
	{
		if (list[i] > result)
			result = list[i];
	}

	return result;
}
int getMax(studentType list[], int size)
{
	double result = list[0].gpa;
	int index = 0;

	for (int i = 0 ; i < size ; i ++)
	{
		if (list[i].gpa > result)
			result = list[i].gpa;
			index = i-1;
	}

	return index;
}

// (a) overload the getMax function to return the student 
// with the highest gpa
//...

int main()
{
	const int SIZE = 6;
	int numbers[SIZE] = {11, 33, 444, 55, 66 -100};
	studentType students[6] = {{"john", 3.01}, {"alice", 3.6},  {"liz", 2.5},{"Mike",3.8},
	    {"Jennifer",4.1},{"Stacey",3.7}
	};

	cout << getMax(numbers, SIZE) << endl;
	
	int index = getMax(students, 6);
	
	cout<<students[index].name << "\t" << students[index].gpa <<endl;

	// (b) call the getMax function using students list and 
	// print the name and gpa of the student with the highest gpa
	//...
 

	
	return 0;
}

 
Last edited on
35: index = i-1;
Why not index = i; ?
Konstantin2 I kept getting an index higher. An +1 index pretty much
Line 35 is misleading. It is not governed by the if on line 33. It is always executed. Enclose statements in braces to make a compound statement that is governed by the if:

1
2
3
4
5
6
7
8
    for (int i = 0; i< size; ++i)
    {
        if (list[i].gpa > result)
        {
            result = list[i].gpa;
            index = i;
        }
    }
cire its always the brackets. I cant believe I missed that. Such a clumsy mistake. thanks
Other way is to use comma operator:
1
2
3
for (int i = 0; i< size; ++i)
     if (list[i].gpa > result)
          result = list[i].gpa, index = i;

Is it nicer?
Or this one:
1
2
3
for (int i = 0; i< size; ++i)
     if (list[i].gpa > result)
          result = list[index = i];

Last edited on
result = list[i].gpa, index = i;
This produces the same effect with more confusing code. Readability is important so I wouldn't do it this way.
result = list[index = i];
This might possibly result in faster or smaller code, but again, it's harder to read.

In the real world, code maintenance is very important, and that means it's important for a new programmer to be able to look at your code and tell what it's doing. For this reason I think you should write code that expresses what you want as simply and plainly as possible like cire's original code:
1
2
result = list[i].gpa;
index = i;

Thank you Konstantin2 and dhayden for the advice.
Topic archived. No new replies allowed.