Array help

thank you.
Last edited on
1
2
int ar[25];
fillArray(ar, 100);


1
2
3
4
5
void fillArray(int a[], int size)
{
	for (int i = 0; i < 100; i++)
		a[i] = rand() % 100 + 1;
}

Why do you try to write 100 elements into an array with the size of 25?
Can you specify the problem? "A debug error" isn't very clear. Maybe you meant a compiler error, when your program won't run.

Also, you include <vector>, but you never use it. using a std::vector to store your integers will make your code easier. For example, you won't have to pass in a size parameter.
Last edited on
The code is consistent in that it consistently ignores the size parameter - or passes the wrong value in the first place. For example here:
31
32
33
34
35
void printArray(int a[], int size)
{
	for (int i = 0; i < 25; i++)
		cout << a[i] << endl;
}


Line 33 should use the function parameter, not use some arbitrary number plucked from thin air.
31
32
33
34
35
void printArray(int a[], int size)
{
	for (int i = 0; i < size; i++)
		cout << a[i] << endl;
}


Inside function main() you could do something like this:
1
2
3
4
5
6
7
8
9
    const int SIZE = 25;
    
    int ar[SIZE];

    fillArray(ar, SIZE);

    printArray(ar, SIZE);

    int count = countEvens(ar, SIZE);


After that, the value 25 should appear just once in the entire program, where the constant is defined.

Similarly, the value 100 should appear just once in the entire program, where the random number is generated.

Last edited on
thanks for the help. I figured it out thanks to you guys.
Please DON'T delete your question after you've got your answer. It makes this thread useless as a learning resource for anyone else. It's a selfish abuse of this forum.

Please edit your post to restore the original question.
MikeyBoy. sorry. I won't do it again. I forgot what I wrote in the original question and I do not have the original broken code anymore either
Topic archived. No new replies allowed.