# Thread: Filling an array with random numbers

1. ## Filling an array with random numbers

Ok, another week, another assignment, another question

I'm trying to write a program that will track a share price, and to do so need to fill an array with random fluctuations, say 0.5 either side of the startPrice (which the user will supply)..

I am not sure how to fill an array with random numbers, so far i have

Code:
```     int prices[]; //declare array  (not sure how big yet)
int numDays, i;
float firstEntry, startPrice;

Printf ("Please enter the starting price of the share you wish to track: ");
scanf ("%d", &startPrice );

prices[0]= startPrice;

for (i=1; i < numdays; i++)
{
prices[i] = startPrice - 0.5 + rand() % startprice + 1.5;  //generates prices for share
}```
will this do the job?

Thanks!

2. Do YOU think that will give you a fluctuation of 50% of the starting price? It doesn't look right to me. And this is not a matter of programming, but about math at the level of about 5-7 years in school.

--
Mats

3. Sorry, my fault i should make it clearer. I need a fluctuation of 0.5 from the start price (ie, if start price is £1, prices between 50p and £1.50.

Regardless of the questionable maths, would this send the fluctuations to the array?

Sorry if it seems a stupid question, im a n00b... thanks for taking the time to reply

4. Why don't you print the contents of your array? That way, you will see what it does, and if it's reasonable in comparison to what you expect.

--
Mats

5. I'm trying to do that atm, but keep getting various errors.. If i haven't got any further in a hour or so i'll post my attempt and the errors.
Thanks for the help, i know noobish questions are annoying as hell

6. Looks right to me. If you're getting errors (except for that blank array), then it should be elsewhere in the code I believe?
BTW, your array is int and your price is float. Is this intentional? You wouldn't be able to hold 1.5 in that int array.

7. Originally Posted by Elysia
Looks right to me. If you're getting errors (except for that blank array), then it should be elsewhere in the code I believe?
BTW, your array is int and your price is float. Is this intentional? You wouldn't be able to hold 1.5 in that int array.

8. The assignment is to write a program containing 3 fuctions, main, a function to fill the array and one to work out the average share price between startDay and endDay which are entered by the user.

Its far from finished, so don't be too harsh with me.. (unless you really think i'm an idiot)

Im now getting the error
"invalid operands to binary" and "invalid operands to binary %" from the line in red

I've tried googling, and think its probably something to do with pointers or the startPrice type... any hints?

Thanks a lot to anyone who actually wades through the code, i know its not pretty

right, here is my code (so far) in full...

Code:
```#include <stdio.h>

void generatePrices(float *firstEntry, float *startPrice);
float averagePrice(float *firstEntry, int *numDays, float *startPrice);
const int NUMDAYS = 10;
int main()
{
float firstEntry, startPrice;
generatePrices;
averagePrice;
return 0;
}

void generatePrices(float *firstEntry, float *startPrice)  //function used to generate prices
{
float prices[NUMDAYS]; //declare array
int i;

Printf ("Please enter the starting price of the share you wish to track: ");
scanf ("%f", &startPrice );
if (startPrice <= 0) {
printf ("Start price must be above 0, check input.\n");

prices[0]= *startPrice;

for (i=1; i < (NUMDAYS - 1); i++)
{
prices[i] = startPrice - 0.5 + rand() % startPrice + 1.5;  //generates prices for share
}

}

float averagePrice(float *firstEntry, int *numDays, float *startPrice);   //function to calculate average
{
int startDay, endDay, j;
float sum = 0.0, average;
printf ("Please enter the start day (0 - 10): ");
scanf ("%d", &startDay );
printf ("\nPlease enter the end day (0 - 10): ");
scanf ("%d", &endDay );

if ( startDay < 0 || startDay > NUMDAYS || endDay < 0 || endDay > NUMDAYS || endDay < startDay) {
return 0;
} else
{
for (j = startDay; j < endDay; j++ ) {
sum = sum + prices[j];
}
average = (sum / NUMDAYS);         //calculate average price
return (average);                  //return averahe to main
}```

9. % is illegal with type float. You must use an integer:

The following doesn't compile:
Code:
```	float f1 = 10, f2 = 3;
float f3 = f1 % f2;```
The following does compile:
Code:
```	int f1 = 10, f2 = 3;
int f3 = f1 % f2;```
But I'm not sure % is the right operator for the job. It's difficult to read sometimes and certainly not some 5th or 6th grade math.
If you want fluxations of +/- 0.50, why not generate a number between 0 and 0.5, then do another random to check if you should subtract or add?

10. If you want a number 0..0.5, you can do this: x = 0.5 *(float)rand() / RAND_MAX;

--
Mats

11. thanks, i'll give it a go.

For some reason the compiler is now giving me a different error, its saying
"[Warning] statement is a reference, not call, to function `generatePrices' ", im at a loss as to why its just started doing this as i dont think i've changed anything (ive tried copy + pasting the code from my post into the compiler, but it gives the same error)...
what have i done wrong now?

thanks both of you

12. Code:
`generatePrices; // Gets the address of the function generatePrices (although in the standard, you really should have to do &generatePrices).`
To actually CALL a function, you must use paranthesis (even if the function takes no arguments):
Code:
`generatePrices(); // Calls the function`
But your function also takes arguments - so pass them with commas in between:
Code:
`generatePrices(arg1, arg2);`
Read up a little on functions and you'll find it's a little better.

13. ahhh thanks

14. Code:
```    generatePrices();
averagePrice();```
If you want to call a function, you need to put parenthesis at the end.

--
Mats

15. I'm still struggling with this one im afraid
Heres my revised code..
My (current) problem is that im getting 'syntax error before float' for the line in red.. I've tried changing it around, and i can't get anywhere with it. Thanks to anyone willing to give me a hand (sorry the code is a bit hard to read at times, layout is not one of my strong points).
As always, if im being an idiot feel free to say so...
Code:
```#include <stdio.h>

void generatePrices(float *firstEntry, int *numDays, float *startPrice);
float averagePrice(float *firstEntry, int *startDay, int *endDay);
const int NUMDAYS = 14;  //constant used as size of the array

int main()
{
int firstDay, lastDay;
int *startDay = &firstDay;  //creates a pointer to firstDay
int *endDay = &lastDay;     //creates a pointer to lastDay
float beginPrice, averagePrice;
float *startPrice = &beginPrice;
float prices[NUMDAYS]; //declares array 'prices' of size NUMDAYS

printf ("Welcome to stock tracker\n");
printf ("To begin, please enter the starting price of the share you wish to track: ");
scanf ("%f", &beginPrice);  //sarting price entered by user and stored as firstEntry
prices[0] = beginPrice;

float *firstEntry = &prices[0];

printf ("\nPlease enter the day you wish to track from (1 - 14): ");
scanf ("%d", &firstDay);
startDay -= 1;
printf ("\nPlease enter the day you wish to track to (1 - 14): ");
endDay -= 1;
scanf ("%d", &lastDay);  //startDay and endDay entered by user and sent to variables

if ( firstDay < 0 || firstDay > NUMDAYS || lastDay < 0 || lastDay > NUMDAYS || lastDay < firstDay || firstDay == lastDay)
{
printf ("\nError: please check that days are within range 1 - 14 and startDay is lower than EndDay");
} //inputs checked for errors
else{
generatePrices( float *firstEntry);
averagePrice( int *startDay, int *endDay );
}
return 0;
}   //end of main function

/* *************************************************************************************************** */

void generatePrices(float *firstEntry, float *startPrice);
{
int i, j;

j = (int) *firstEntry; //cast firstEntry as an int

for (i=1; i < (NUMDAYS - 1); i++)
{
prices[i] = j - 0.5 + rand() % j + 1.5;  //generates prices for share
}
}

/* ***************************************************************************************************** */

float averagePrice(float *firstEntry, int *startDay, int *endDay);
{
float sum = 0.0, average;
int k;

for (k = startDay; k < endDay; k++ ) {
sum += prices[k];
}
average = (sum / NUMDAYS);         //calculate average price
return (average);                  //return average to main
}```