Help With Assigning An Array With rand()

  • Thread starter Thread starter NDiggity
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the creation and manipulation of an integer array in a programming context. Participants address issues related to assigning random values to the array, finding the largest number, and modifying the array's contents.

Discussion Character

  • Homework-related

Main Points Raised

  • One participant describes a function that should find the largest number in an array, return it, and set that position to zero, while also generating an array of random integers.
  • Another participant points out a potential issue with the loop index being reused, suggesting that the loop index should be declared inside the loop to avoid accessing it outside its intended scope.
  • A later reply acknowledges the mistake and expresses gratitude for the assistance, indicating that the issue has been resolved.
  • Another participant humorously relates to the original poster's mistake, suggesting a shared experience with similar coding errors.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the coding mistake and the suggested solution, with no significant disagreement present in the discussion.

Contextual Notes

There may be limitations related to the random number generation and how it is implemented, as well as the specific behavior of the array after modifications, but these aspects remain unaddressed in the discussion.

Who May Find This Useful

Readers interested in programming, particularly in array manipulation and debugging common coding errors, may find this discussion beneficial.

NDiggity
Messages
53
Reaction score
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?
 
Physics news on Phys.org
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.
 
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!
 
Been there - done that , only bigger and dumber ! :biggrin:
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
9
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 8 ·
Replies
8
Views
3K