Populating an Array

This is a discussion on Populating an Array within the C++ Programming forums, part of the General Programming Boards category; 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; } ...

  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
    2,692
    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 12:46 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    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,308
    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, 01:39 PM
  3. populating 2-D array
    By edster in forum C Programming
    Replies: 4
    Last Post: 10-22-2009, 03:42 PM
  4. Populating a Struct
    By larry_2k4 in forum C Programming
    Replies: 4
    Last Post: 10-21-2009, 10:27 PM
  5. Populating a multi-dimensional array problem
    By Zildjian in forum C Programming
    Replies: 7
    Last Post: 10-06-2003, 09:17 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21