Array size variables in functions

I need to write a function that takes in numbers from an array and figures out the highest, lowest, and average of those numbers, but I am a bit unsure of how to get the function to work adequately with regard to the size of the array.

I already know the problem is with my calls having the array size set to 50, so instead of dividing to get the average by the amount actually in the array its dividing by 50. So obviously the numbers Im getting are off. Now I believe I need to use some sort of variable instead, but I cant seem to get it to work.

Below I have included only the parts of my program that are relevant to the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//variables
int evenArray[50] = {0};
int oddArray[50] = {0};
int Array[50] = {0};

//calls
cout << "Even Sum:\t" << Sum(evenArray, 50) << endl;
cout << "Odd Sum:\t" << Sum(oddArray, 50) << endl;

//function
float Sum(int array[], int size)
{
	float sum = array[0];
	float average;
	for (int i = 1; i < size; i++) {
		sum += array[i];
	}
	average = sum / size;
	cout << "The sum is: " << sum;
	cout << "The average is: " << average;
	return sum;
}


EDIT: Also, I am aware that the couts create redundant and hard to read output, however I often have redundant couts as a check while Im still working on the program. I clean it up once I know the program works.
Last edited on
average = sum / size;
Do you realize that you're doing integer math (meaning no fractions) in this calculation? For example 5 / 6 would yield zero.

No i did not realize that, but I can already tell thats not what the major flaw is with the function.
Last edited on
closed account (48T7M4Gy)
You'll need to show us sample input and corresponding output.
This is the program in its entirety as I have it so far.

With regards to the average, it should be 54.26 for the evenArray and 55.3 for the odd.
As of right now its giving 32 and 22 respectively.

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void evenOdd(ifstream &, int[], int[], int[]);
void align(int[], int);
float Sum(int[], int);
int avg(int[], float);

int main()
{
	ifstream infile;
	ofstream evenfile;
	ofstream oddfile;
	int evenArray[50] = {0};
	int oddArray[50] = {0};
	int Array[50] = {0};
	int num = 0;
	int max = 0;
	int min = 0;
	int count = 0;
	int sum = 0;
	int average = 0;
	
	cout << "Start of program." << endl;
	cout << "Opening infile..." << endl;
	infile.open("array.txt");

	if (!infile)
	{
		cout << "Cannot open file, terminating program." << endl;
		exit(1);
	}
	else
	{
		cout << "File opened successfully." << endl;
	}

	cout << "Opening evenfile..." << endl;
	evenfile.open("even.txt");

	if (!evenfile)
	{
		cout << "Cannot open file, terminating program." << endl;
		exit(1);
	}
	else
	{
		cout << "File opened successfully." << endl;
	}

	cout << "Opening oddfile..." << endl;
	oddfile.open("odd.txt");

	if (!oddfile)
	{
		cout << "Cannot open file, terminating program." << endl;
		exit(1);
	}
	else
	{
		cout << "File opened successfully." << endl;
	}

	while (!infile.eof())
	{
		evenOdd(infile, evenArray, oddArray, Array);
		cout << "Even Sum:\t" << Sum(evenArray, 50) << endl;
		cout << "Odd Sum:\t" << Sum(oddArray, 50) << endl;
		cout << "Numbers over even:" << avg(evenArray, average) <<endl;
		cout << "Numbers over odd:" << avg(oddArray, average)<< endl;
		cout << endl;
	}
	cout << endl;
	cout << "End of program." << endl;
	infile.close();
	evenfile.close();
	oddfile.close();
	return 0;
}

/***************************************************************/
void evenOdd(ifstream & infile, int evenArray[], int oddArray[], int array[])
{
	int a = 0;
	int b = 0;

	for (int i = 0; infile >> array[i]; i++)
	{
		if (array[i] % 2 == 0)
		{
			evenArray[a] = array[i];
			a++;
		}

		else
		{
			oddArray[b] = array[i];
			b++;
		}
	}
	cout << endl;
	cout << "Even Array" << endl;
	align(evenArray, a);
	cout << "Odd Array" << endl;
	align(oddArray, b);
}
/***************************************************************/
void align(int array[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << setw(3) << array[i];
		if ((i + 1) % 10 == 0)
			cout << endl;
	}
	cout << endl;
}
/***************************************************************/
float Sum(int array[], int size)
{
	int max = array[0];
	int min = array[0];

	for (int i = 0; i < 50; i++)
	{
		if (array[i] > max)
		{
			max = array[i];
		}
		else if (array[i] < min)
		{
			min = array[i];
		}
	}

	float sum = array[0];
	float average;
	for (int i = 1; i < size; i++) {
		sum += array[i];
	}
	average = sum / size;
	cout << "The sum is: " << sum << endl;
	cout << "The average is: " << average << endl;
	cout << "Min: " << min << endl;
	cout << "Max: " << max << endl;
	return average;
}
/***************************************************************/
int avg(int array[], float average)
{
	while (array[0] > average)
		return array[0];
}
closed account (48T7M4Gy)
It would have been better if you'd just shown your sample input and output.

My guess is you are using the number 50 for your calculations when in fact the actual count of items should be used in getting the average etc. Line 23 refers to count but count isn't used.
Im aware that its the 50, but I am unsure of how to go about doing it with an actual count.
closed account (48T7M4Gy)
You have to count the number of elements as they are entered into a particular array. That's where the variable count you already have comes into play. You probably need several count variable, 1 for each array, eg count_even, count_odd etc

eg in pseudocode

1
2
3
4
5
6
count = 0
loop:
    read in a value
    store value in array[count]
    increment count
    continue in loop


Would it be possible to use the i variable, such as the one in if (array[i] > max) as the count?
closed account (48T7M4Gy)
Probably not because 'count' in this case is there to be the actual number of items read in and is the substitute for 'size' which has been set at 50 to accomodate the actual number.

'i' is the current index and it is limited to be < count in any for loop

1
2
3
4
for(int i = 0; i < count; i++)
{
    // do stuff with array[i] as required etc
}
Ok so what I did was in my evenOdd function I added two different counts, one for the evens and one for the odds, and that seemed to do the trick.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void evenOdd(ifstream & infile, int evenArray[], int oddArray[], int array[], int & evenCount, int & oddCount)
{
	int a = 0;
	int b = 0;

	for (int i = 0; infile >> array[i]; i++)
	{
		if (array[i] % 2 == 0)
		{
			evenArray[a] = array[i];
			a++;
			evenCount++;
		}

		else
		{
			oddArray[b] = array[i];
			b++;
			oddCount++;
		}
	}

closed account (48T7M4Gy)
On the right track. Try something like this. You'll need to pass back the number i. A good choice would be to have a variable arrayCount instead of 'i'. Alternatively just add evenCount and oddCount.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void evenOdd(ifstream & infile, int evenArray[], int oddArray[], int array[], int & evenCount, int & oddCount)
{
	evenCount = 0;
	oddCount = 0;

        int i = 0;

	while(infile >> array[i])
        {
		if (array[i] % 2 == 0)
		{
			evenArray[evenCount] = array[i];
                        evenCount++;
		}

		else
		{
			oddArray[oddCount] = array[i];
                        oddCount++;
		}
	}


PS you need to incorporate a check that the number of values read in don't exceed the array limit, in this case 50.
Last edited on
Topic archived. No new replies allowed.