Help With Assigning An Array With rand()

  • Thread starter NDiggity
  • Start date
  • #1
54
0

Homework Statement


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.

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?
 

Answers and Replies

  • #2
mgb_phys
Science Advisor
Homework Helper
7,819
14
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.
 
  • #3
54
0
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!
 
  • #4
mgb_phys
Science Advisor
Homework Helper
7,819
14
Been there - done that , only bigger and dumber ! :biggrin:
 

Related Threads on Help With Assigning An Array With rand()

  • Last Post
Replies
13
Views
4K
Replies
7
Views
764
  • Last Post
Replies
3
Views
966
  • Last Post
Replies
5
Views
742
Replies
4
Views
2K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
764
Replies
0
Views
2K
  • Last Post
Replies
5
Views
2K
Top