Thread: Filling an array with random numbers

  1. #16
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    Do you mean to have a prototype that doesn't match the actual call, that again doesn't match the function itself?

    Todd

  2. #17
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Quote Originally Posted by Todd Burch View Post
    Do you mean to have a prototype that doesn't match the actual call, that again doesn't match the function itself?

    Todd
    ....hmmm, that may well be the problem... thanks :s

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    i think ive sorted that out... now on to the next one

    i now get told that 'called object is not a function' for the following line from main

    Code:
        else{
                 generatePrices( firstEntry);
                 averagePrice( startDay, endDay );
                 }
        return 0;
    This baffles me...

    (genuinely sorry for all the noob questions, i know how irritating it is..)

  4. #19
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    Similar to before, your function call does not match the function prototype or the function definition itself.

    Todd

  5. #20
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    I cant see what else i can do to them, arrrg i think ill just give it in as it is, at least i gave it a go. Thanks to all who replied


    Code:
    #include <stdio.h>
    
    void generatePrices(float *firstEntry);
    float averagePrice(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( firstEntry);
                 averagePrice( startDay, endDay);
                 }
        return 0;
    }   //end of main function
    
    /* *************************************************************************************************** */
    
    void generatePrices(float *firstEntry)
    {
         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(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 averahe to main
    }

  6. #21
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This might help you get better fluctations. It's just a simple linear interpolation that chooses a random coefficient.

    Code:
    float GetRandomFloatRange(float min,float max)
    {
      float coef = ((float)(rand () &#37; 10000))/10000.0f;
    
      return min + coef *(max-min);
    }
    Keep in mind rand() is a very crappy pseudo random number generator.
    Last edited by VirtualAce; 11-28-2007 at 05:44 PM.

  7. #22
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    See here:
    Code:
    #include <stdio.h>
    
    void generatePrices(float *firstEntry);
    float averagePrice(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; // Is this a variable or a function name?  It's BOTH!! 
        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;  // local to main!! 
        
        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( firstEntry);
                 averagePrice( startDay, endDay); // Where is the returned value assigned??? 
                 }
        return 0;
    }   //end of main function
    
    /* *************************************************************************************************** */
    
    void generatePrices(float *firstEntry)
    {
         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(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 averahe to main
    }
    Todd

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