Thread: Filling an array with random numbers

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    30

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    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. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    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. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Quote Originally Posted by Elysia View Post
    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.
    Thanks, i hadn't noticed that

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    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) {
             printf ("error, please check inputs.\n");
             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. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    % 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?
    Last edited by Elysia; 11-21-2007 at 12:09 PM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want a number 0..0.5, you can do this: x = 0.5 *(float)rand() / RAND_MAX;

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    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. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    ahhh thanks

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        generatePrices();
        averagePrice();
    If you want to call a function, you need to put parenthesis at the end.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    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
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 02:01 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Random numbers.
    By Nate2430 in forum C++ Programming
    Replies: 5
    Last Post: 01-27-2002, 09:26 AM