Help With Assigning An Array With rand()

  • Thread starter NDiggity
  • Start date
  • Tags
    Array
In summary, the task is to write a function that takes an int array and its size as parameters, finds the largest number in the array, returns this value, and sets it to zero. In the main program, an array of 20 numbers is created and assigned random values between 1 and 100. The values are then printed, and the function is run 10 times. The total of all the returned values and the new contents of the array are printed. The issue was that the loop index was being reused, causing incorrect values to be printed. The solution is to declare the loop index inside the loop.
  • #1
NDiggity
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?
 
Physics news on Phys.org
  • #2
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
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
Been there - done that , only bigger and dumber ! :biggrin:
 

1) What is the purpose of using the rand() function when assigning an array?

The rand() function is used to generate a random number within a specified range. In the context of assigning an array, it can be used to randomly select elements from a given set of values.

2) How do I specify the range for the rand() function when assigning an array?

The rand() function takes two parameters: a minimum and a maximum value. These values can be specified within the parentheses following the function name. For example, to generate a random number between 1 and 10, you would use rand(1, 10).

3) Can I use the rand() function to assign non-numerical values to an array?

Yes, you can use the rand() function to assign any type of value to an array. This includes strings, objects, and other data types.

4) How can I ensure that the rand() function generates unique numbers when assigning an array?

The rand() function generates pseudo-random numbers, meaning that the same sequence of numbers can be generated if the function is called multiple times with the same parameters. To ensure unique numbers, you can use other functions such as shuffle() or array_rand() to randomize the order of the elements in the array.

5) Are there any alternative functions to rand() for assigning an array with random values?

Yes, there are alternative functions such as mt_rand() and random_int() which use different algorithms to generate random numbers. These functions may have different performance or security implications, so it is important to choose the appropriate function for your specific use case.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
783
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top