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.
#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;
}
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.
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! :)