Help With Assigning An Array With rand()

  • Thread starter Thread starter NDiggity
  • Start date Start date
  • Tags Tags
    Array
AI Thread Summary
The discussion revolves around a programming assignment that requires creating a function to find the largest number in an array, set it to zero, and print the results. The user encountered an issue where all values in the array appeared as 20, despite the function returning different totals. The problem was identified as using the same loop index across multiple loops, leading to incorrect printing of values. A suggestion was made to declare the loop index within the loop to avoid such errors. The user acknowledged the mistake and expressed gratitude for the assistance received.
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:
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Back
Top