1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help With Assigning An Array With rand()

  1. Nov 14, 2007 #1
    1. The problem statement, all variables and given/known data
    I need to write a function that takes an int array and the size of the array as parameters, then finds the largest num in the array, returns this value and then sets the value to zero.
    Then in the main part of the program, I need to create an int array of 20 nums, assign all 20 values a random value between 1 and 100, print the values of the array out, then run the function 10 times, print the total of all the numbers returned by the function, and print out the new contents of the array.

    3. The attempt at a solution
    This is what I have: http://cpp.tastethepaste.org/1721
    My problem is that when I print out the array, all 20 values are 20, the total of all the numbers returned by the function is different every time tho. And after the fact, it prints out the 20 values again, which are all still 20. What am I doing wrong?
     
  2. jcsd
  3. Nov 14, 2007 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    In the last two loops you are using the loop index from the previous loop - so you are actually printing the same value.

    As a tip always declare the loop index inside the loop;

    for (int i=0;i<BLAH;i++) {
    }

    Then the compiler will warn you if you access it outside the loop.
     
  4. Nov 14, 2007 #3
    Wow, dumb mistake :p. Thank you very much for taking the time to look at my code, I appreciate it. All is good now, thanks again!
     
  5. Nov 14, 2007 #4

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Been there - done that , only bigger and dumber ! :biggrin:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Help With Assigning An Array With rand()
Loading...