multiple arrays thru a function

This is late homework, but its a matter of principle now. I have to send 2 arrays thru an average and print function. My functions tested o.k. with each individual array but when i try to put both arrays thru only my random array will average. the grades array will print but wont average. im sure its something stupid but im tearing my hair out trying to figure it out. plz help b4 im completely bald.


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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;


void getGrades ( int grades[], int& size )
	{
		int counter;
		size = 10;
		cout << " \n\n ";
		cout << setw(40 + ( 52 / 2 )) << " Please enter 10 grades as integers to be averaged. " << endl;
		cout << " \n\n\n ";
		for ( counter = 0; counter <10; counter++ )
		{
			cout << setw(40) << " Enter grade #  " << (counter +1) << " --> ";
			cin >> grades[counter];
		}
    }
double computeAverage ( int randomArray[], int grades[], int size, float& average )
{
	
	int counter;
	float sum = 0.0;
		
	if ( grades[size] )
		for ( counter = 0; counter <size; counter++ )
		{
		sum+= grades[counter] ;
		average = ( sum / size );
		}
		return grades[size];

if ( randomArray[size] )
		for ( counter = 0; counter <size; counter++ )
		{
		sum+= randomArray[counter];
		average = ( sum / size );
		}
		return randomArray[size];
}

double printInts ( int randomArray[], int grades[], float average, int size )
{
	int counter;
	if ( randomArray[size] )
		for ( counter = 0; counter <size; counter++ )
		{
			cout << setw(35) << " The numbers " << ( counter + 1 ) << " generated " <<  randomArray[counter] << endl;
		}
		return randomArray[size];
	if ( grades[size] )
		for ( counter = 0; counter <size; counter++ )
		{
			cout << setw(35) << " The numbers " << ( counter + 1 ) << " generated " <<  grades[counter] << endl;
		}
		return grades[size];
}
void getRandom ( int randomArray[], int& size )
{
	int counter;
	int raNum;
	int rand_max = 50;
	int rand_min = 1;
	size = 20;
	cout << setw(40 + ( 35 / 2 )) << " This random generator will generate 20 random numbers between 1 and 50.\n " << endl;
	srand(static_cast<unsigned int>(time(0)));
		for ( counter = 0; counter <20; counter++ )
		{
			raNum = (rand() % (rand_max - rand_min + 1)) + rand_min;
			randomArray[counter] = raNum;
		}

	
}

		

int main ()
{
		// local constants
	
	int grades[10];
	int randomArray[20];
	int size;
	float average;
	
	
	


		system ("title Average Grades Array ");
		system ("color 1f ");
		
		cout << " \n\n\n ";                     
		getGrades ( grades, size );
		system ("cls");
		cout << " \n\n\n ";
		computeAverage ( randomArray, grades, size, average );
		printInts ( randomArray, grades, average, size );
		cout << " \n\n\n ";
		cout << setw(40) << " The average of these 10 grades is: " << average << endl;
		cout << " \n\n\n ";
		system("pause");
		system("cls");
		cout << " \n\n\n ";
		getRandom ( randomArray, size );
		printInts ( randomArray, grades, average, size );
		computeAverage ( randomArray,grades, size, average );
		cout << " \n\n\n ";
		cout << setw(40) << " The average of these 20 numbers is: " << average << endl;
    system ("pause");																									
    cout << " \n\n\n\n ";
	return 0;
}
if ( grades[size] )
What is this supposed to do? This will only ever fail if the element in grades[size] is 0, more importantly:
int grades[10];
The grades array only has 10 elements allocated, while size is 20, meaning you'll go out of bounds giving you garbage values for everything past grades[9], and I'm guessing thats why you're not getting a proper average.
the variable size = 10 or 20 depending on which array is sent to the function. so then grades[size] also = 10. if each array is compiled and sent thru the functions by themselves, it works perfectly. something to do with trying to send both thru them consecutively is causing the problem.
Warnis is right. Maybe you were testing if the size of the arrays is greater than 0? That's not how you do it. And then, on lines 34 and 53 you have a return. The function will end there, so the code after it won't run. If your compiler didn't warn you, I guess it's time to change it.
I noticed after looking through it again (Must've been really tired yesterday) that you have a lot of code repeating. So I changed your computeAverage and printInts function to only take one array instead of two.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double computeAverage ( int arr[], int size, float& average )
{
	
	int counter;
	float sum = 0.0;
	for ( counter = 0; counter <size; counter++ )
	{
		sum+= arr[counter];
		average = ( sum / size );
	}
	return average;
	
}

double printInts (int arr[], float average, int size )
{
	int counter;
	for ( counter = 0; counter <size; counter++ )
	{
		cout << setw(35) << " The numbers " << ( counter + 1 ) << " generated " <<  arr[counter] << endl;
	}
	return average;
}

also changed the calls to
1
2
3
4
5
computeAverage ( grades, size, average );
printInts ( grades, average, size );
...
printInts ( randomArray, average, size );
computeAverage ( randomArray, size, average );


And as I suspected earlier the problem was indeed that you were accessing elements out of bounds in grades, but also using unitialized values in the randomArray. Anyways, this minor change fixes both problems.
thank you very much. i mustve changed something after testing. been at this one for a week lol.
Topic archived. No new replies allowed.