trying to pass an array to and from a void function

Hello everyone,

I'm trying to pass 2 arrays into a void funtion, and return values to one function.

this is the the program I'm working with, after I'm done I have to split it into 3 files, a header, a main, and a separate cpp file for the functions to live in.

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

using namespace std;

void processArrary(int numberCount[], int Numbers[], int intnumberSize, int numberCountSize);

int main()
{

	int Scores[26] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189};
	int numberCount[8] = { 0 };

	processArrary(Scores, numberCount, 8, 26);


	cout << "The Number of Students between   0 and  24 is: " << numberCount[0]<< endl;
	cout << "The Number of Students between  25 and  49 is: " << numberCount[1]<< endl;
	cout << "The Number of Students between  50 and  74 is: " << numberCount[2]<< endl;
	cout << "The Number of Students between  75 and  99 is: " << numberCount[3]<< endl;
	cout << "The Number of Students between 100 and 124 is: " << numberCount[4]<< endl;
	cout << "The Number of Students between 125 and 149 is: " << numberCount[5]<< endl;
	cout << "The Number of Students between 150 and 174 is: " << numberCount[6]<< endl;
	cout << "The Number of Students between 175 and 200 is: " << numberCount[7]<< endl << endl;


	system("pause");
	return 0;
}

void processArrary(int Numbers[], int numberCount[], int intnumberSize, int numberCountSize = 0)

{
	for (int j = 0; j < intnumberSize; j++)
	{
		if (Numbers[j] < -1 && Numbers[j] > 25)

		{
			numberCountSize = numberCountSize ++;
			numberCount[0] = numberCountSize;
		}
		else
		if (Numbers[j] < 25 && Numbers[j] > 50)

		{
			numberCountSize = numberCountSize ++;
			numberCount[1] = numberCountSize;
		}
		else
		if (Numbers[j] < 50 && Numbers[j] > 75)

		{
			numberCountSize = numberCountSize ++;
			numberCount[2] = numberCountSize;
		}
		else
		if (Numbers[j] < 75 && Numbers[j] > 100)

		{
			numberCountSize = numberCountSize ++;
			numberCount[3] = numberCountSize;
		}
		else
		if (Numbers[j] < 100 && Numbers[j] > 125)

		{
			numberCountSize = numberCountSize ++;
			numberCount[4] = numberCountSize;
		}
		else
		if (Numbers[j] < 125 && Numbers[j] > 150)

		{
			numberCountSize = numberCountSize ++;
			numberCount[5] = numberCountSize;
		}
		else
		if (Numbers[j] < 150 && Numbers[j] > 175)

		{
			numberCountSize = numberCountSize ++;
			numberCount[6] = numberCountSize;
		}
		else
		if (Numbers[j] < 175 && Numbers[j] > 200)

		{
			numberCountSize = numberCountSize ++;
			numberCount[7] = numberCountSize;
		}
	}

}


The goal of this program is to separate and count the groups of numbers then output the amount of numbers in each group. Near as I can tell, everthing should work, but I'm getting all zeros to be displayed in each group.
Near as I can tell, a couple of things are happening. mind you this is guesswork, educated guesswork, but guesswork nonetheless.

first, the grades aren't getting passed into the function.

second, they are getting passed in, but they're getting lost somewhere. I genuinely don't know where though.
Hi @mattig89ch,


 
if (Numbers[j] < -1 && Numbers[j] > 25)

this statement says
that if a number is
less than -1 (-2,-3,-4,..,-n)
and greater than 25 at
the same time,
this is impossible!

and i do not
know why you pass
numberCountSize
and then int numberCountSize = 0
changes the value to
zero;

The goal of that line is to say if a number is between 0 and 24 then add 1 to the count.

is it so simple that I got my signs mixed up? I've been beating my head against this program for 6 hours trying to get it to work, this is the closest I've come to actually getting this program to work.
If you want to say if a number is between 0 and 24, then rewrite the condition to test for that. What you've actually checking for is whether a number is both lower than -1 and greater than 25 at the same time!
ok, so fair enough, I admit to being wrong there. I went ahead and re-wrote that bit. heres what I'm working with now:

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
void processArrary(int Numbers[], int numberCount[])

{
	int numberCountSize = 0;
	int intnumberSize = 0;

	for (int j = 0; j < intnumberSize; j++)
	{
		if (Numbers[j] > -1 && Numbers[j] < 25)

		{
			numberCountSize = numberCountSize ++;
			numberCount[0] = numberCountSize;
		}
		else
		if (Numbers[j] > 25 && Numbers[j] < 50)

		{
			numberCountSize = numberCountSize ++;
			numberCount[1] = numberCountSize;
		}
		else
		if (Numbers[j] > 50 && Numbers[j] < 75)

		{
			numberCountSize = numberCountSize ++;
			numberCount[2] = numberCountSize;
		}
		else
		if (Numbers[j] > 75 && Numbers[j] < 100)

		{
			numberCountSize = numberCountSize ++;
			numberCount[3] = numberCountSize;
		}
		else
		if (Numbers[j] > 100 && Numbers[j] < 125)

		{
			numberCountSize = numberCountSize ++;
			numberCount[4] = numberCountSize;
		}
		else
		if (Numbers[j] > 125 && Numbers[j] < 150)

		{
			numberCountSize = numberCountSize ++;
			numberCount[5] = numberCountSize;
		}
		else
		if (Numbers[j] > 150 && Numbers[j] < 175)

		{
			numberCountSize = numberCountSize ++;
			numberCount[6] = numberCountSize;
		}
		else
		if (Numbers[j] > 175 && Numbers[j] < 200)

		{
			numberCountSize = numberCountSize ++;
			numberCount[7] = numberCountSize;
		}
	}

}


I think using numberCountSize what an extra step I didn't need. So I got rid of that step and used this instead.

Edit: its still outputting zeros, so I went back. This way makes more sense to me. I also changed the prototype to make sure the function only has the 2 arrays in it. its not giving me any build errors, just outputting zeros. But its due in 10 min. So, if I don't receive any responses by then I'm going to have to submit it as is. Hopefully I get partial credit.
Last edited on
It's fairly simple

First:
Fixing processArray function prototype
you do not need pass int numberCountSize

Second:
Implementation improvements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 for(int i=0;i<SIZE_SCORES;i++){
                if(scores[i]>=0&&scores[i]<=24){ //between   0 and  24
                        ++counter[0]; //adding one to array counter index 0
                }else if(scores[i]>=25&&scores[i]<=49){//between  25 and  49
                        ++counter[1]; //adding one to array counter index 1
                }else if(scores[i]>=50&&scores[i]<=74){//between  50 and  74
                        ++counter[2]; //adding one to array counter index 2
                }else if(scores[i]>=75&&scores[i]<=99){//between  75 and  99
                        ++counter[3]; //adding one to array counter index 3
                }else if(scores[i]>=100&&scores[i]<=124){//between 100 and 124
                        ++counter[4]; //adding one to array counter index 4
                }else if(scores[i]>=125&&scores[i]<=149){//between 125 and 149
                        ++counter[5]; //adding one to array counter index 5
                }else if(scores[i]>=150&&scores[i]<=174){//between 150 and 174
                        ++counter[6];  //adding one to array counter index 6
                }else if(scores[i]>=175&&scores[i]<=200){//between 175 and 200
                        ++counter[7]; //adding one to array counter index 7
                }//end if..else if..

        }//end for 


And the final
source code looks
like 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
//CScores.cpp
//##

#include <iostream>
using std::cout;
using std::endl;

void processArray(int scores[],int const SIZE_SCORES,int counter[]);

int main(){

        int const SIZE_SCORES=26;
        int const SIZE_COUNTER=8;

        int Scores[SIZE_SCORES] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189};
        int Counter[SIZE_COUNTER]={}; //all of them Zeros 

        processArray(Scores,SIZE_SCORES,Counter);

        cout << "The Number of Students between   0 and  24 is: " << Counter[0]<< endl;
        cout << "The Number of Students between  25 and  49 is: " << Counter[1]<< endl;
        cout << "The Number of Students between  50 and  74 is: " << Counter[2]<< endl;
        cout << "The Number of Students between  75 and  99 is: " << Counter[3]<< endl;
        cout << "The Number of Students between 100 and 124 is: " << Counter[4]<< endl;
        cout << "The Number of Students between 125 and 149 is: " << Counter[5]<< endl;
        cout << "The Number of Students between 150 and 174 is: " << Counter[6]<< endl;
        cout << "The Number of Students between 175 and 200 is: " << Counter[7]<< endl << endl;




return 0; //indicates success
}//end of main

void processArray(int scores[],int const SIZE_SCORES,int counter[]){
        for(int i=0;i<SIZE_SCORES;i++){
                if(scores[i]>=0&&scores[i]<=24){
                        ++counter[0];
                }else if(scores[i]>=25&&scores[i]<=49){
                        ++counter[1];
                }else if(scores[i]>=50&&scores[i]<=74){
                        ++counter[2];
                }else if(scores[i]>=75&&scores[i]<=99){
                        ++counter[3];
                }else if(scores[i]>=100&&scores[i]<=124){
                        ++counter[4];
                }else if(scores[i]>=125&&scores[i]<=149){
                        ++counter[5];
                }else if(scores[i]>=150&&scores[i]<=174){
                        ++counter[6];
                }else if(scores[i]>=175&&scores[i]<=200){
                        ++counter[7];
                }//end if..else if..

        }//end for
}//end function processArray 



---
Eyenrique-MacBook-Pro:Desktop Eyenrique$ ./CScores 
The Number of Students between   0 and  24 is: 1
The Number of Students between  25 and  49 is: 2
The Number of Students between  50 and  74 is: 0
The Number of Students between  75 and  99 is: 6
The Number of Students between 100 and 124 is: 1
The Number of Students between 125 and 149 is: 3
The Number of Students between 150 and 174 is: 5
The Number of Students between 175 and 200 is: 8
Topic archived. No new replies allowed.