Thread: Populating an Array

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Populating an Array

    Code:
    int main()
    {
        const double array[5]={};
        sumValues(array,5);
        int number,value;
        for(int i=0;i<number;i++){
            value = (rand()%100)+1;
            value = array[i];
            
            cout<<value<<endl;
        }
        return 0;
    }
    Hi all. I am trying to populate an array using random numbers; the above code has no errors, but when compiling it does not output any numbers on the screen.

    Have I gone wrong somewhere? Or am I on the right track?

    I would like to thank you in advance for any advice or help given. Really appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You are not initializing "numbers" before using it.
    I assume you're initializing "array[5]" in the "sumValues()" function.

    [edit]
    This code seems wrong. You are overwriting the random number placed in "value" immediately after with a value from "array." Also, if "number" is greater than 5, you will overrun your "array" array.
    Last edited by Matticus; 08-14-2012 at 11:46 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following code:
    Code:
        for(int i=0;i<number;i++){
            value = (rand()%100)+1;
            value = array[i];
             
            cout<<value<<endl;
        }
    You are not actually ever using your random number. You put your random number into the variable value then immediately replace that number with whatever array[i] contains. What does array[i] contain?

    Jim

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    How many random numbers are there supposed to be?
    Do you know that the = symbol in C++ only copies the thing from the right hand side into the left hand side?

    You should post the sumValues function as well, because from what we can see here, it looks like there will be some issues with it too.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Populating Array Difficulty
    By mcertini in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2010, 12:42 AM
  2. Populating 2-D arrays
    By fgg in forum C Programming
    Replies: 4
    Last Post: 10-13-2010, 12:39 PM
  3. populating 2-D array
    By edster in forum C Programming
    Replies: 4
    Last Post: 10-22-2009, 02:42 PM
  4. Populating a Struct
    By larry_2k4 in forum C Programming
    Replies: 4
    Last Post: 10-21-2009, 09:27 PM
  5. Populating a multi-dimensional array problem
    By Zildjian in forum C Programming
    Replies: 7
    Last Post: 10-06-2003, 08:17 AM

Tags for this Thread