Array by calling function

Hello !

I have a, hopefully quick question.

I my code below i've made an array that has 100 zeroes in it. After that i make a function that makes random numbers that stops creating new numbers after it exceeds a total of 1000.

Now my problem is, that i want to make a for loop in my main that prints out these numbers that are accumulated together, by calling the function fillArray.

I hope that you somewhat understand what i mean.

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
  #include<stdio.h>
#include<stdlib.h>



#define SIZE 100 // Size defineres som 100.

int main()
{
	int fillArray(int[], int); // fillArray initialiseres.
	int count; //count initiliseres.
	int n[SIZE] = { 0 }; // alle elementer sættes til 0.
	int j;

	count = fillArray(n, SIZE); // count sættes til antal værdier der er i arrayet 
	printf("count=%d\n", count); // smider

	for (j = 0; j < count; j++)
	{
		
	}
	
	return 0;
}

int fillArray(int emtyArray[], int size)
{
	
	int i=0;
	int total = 0;

	while(i<=SIZE-1)
	{
		emtyArray[i]= rand() % 99 + 1;
		total = emtyArray[i] + total;
		if (total > 1000)
			break;
		i++;
	}
	printf("total=%d\n", total);
	return i+1;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
while(i<=SIZE-1)
	{
		emtyArray[i]= rand() % 99 + 1;

                printf("???", ???);

		total = emtyArray[i] + total;
		if (total > 1000)
			break;
		i++;
	}
	printf("total=%d\n", total);


Also why return i + 1?
Last edited on
My assignment tells me that i need to use a for loop in my main to print it out.

I was thinking of the same solution, but it doesnt meet the assignments requirements :(

edit: +1 isnt supposed to be there. didn't see that, my assignment wants me to print out the number of randomized values until exceeding 1000. I do that by returning i.
Last edited on
closed account (48T7M4Gy)
use a for loop in my main to print it out

So all you need to do is copy the appropriate lines from the function into the loop you have already outlined in main()

1
2
3
4
for (j = 0; j < count; j++)
	{
		printf(???, ???);
	}


Hint: One aspect you need to consider is the name of the array being passed to the function.
Last edited on
yeah i was thinking of doing that.

but for some reason i cant wrap my head around it.

which array do i need? Or do i need to make a new one?


Last edited on
closed account (48T7M4Gy)
which array do i need?

See next comment.

Or do i need to make a new one?

Definitely NO. Maybe you need to take a short break but I'll give you another hint. The name of the array for the print line in main is staring you in the face, it appears in main and the function you passed it to where you call it emtyArray, and there is only one array anyway! :)
Maybe i do need a break, sometimes you just cant see the problem, or right answer.

Well the only anwser i can come up with is to use n?
closed account (48T7M4Gy)
Just try it! Whatever happens you won't break anything.
Tried it, without debugging.

and it just gives me 17 numbers of 1637012. <--- changes everytime i run it of cause.

If i use break point and debugging, i see that n has a lot of different values.
Last edited on
I got it

1
2
3
4
5
for (j = 0; j < count; j++)
	{
		printf("Value : %d\n", n[j]);
	}
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.