0

Getting junk number.

https://code.sololearn.com/cZkRZseLo0D1/?ref=app I am filing in random number into an array. Sorting should be before fill in it. However when I run my code. The first 20numbers are just junks Is my algorithms wrongs?

20th Nov 2018, 12:43 AM
Jimmy Chen
Jimmy Chen  - avatar
2 ответов
+ 1
Your algorithm may need some debugging, but you have another issue that may be making this harder to troubleshoot: 40: for (j=i; j>=0; j--) 41: { 42: if (num<arr[j-1]) 43: { 44: arr[j]=arr[j-1]; The reason you're getting junk (it's actually the timestamp) is the answer to the question: * What happens at lines 42 & 44 when j hits 0, at line 40? Fix that, then you could try printing your values as you load them: 32: cout << "[" << num << "]"; This may help you track what's happening (how the initial values move).
20th Nov 2018, 5:57 PM
Kirk Schafer
Kirk Schafer - avatar
0
but i wrote that if (i==0) goes into the arr[0]. else (.....rest of code). that i shouldnt be 0?
20th Nov 2018, 6:37 PM
Jimmy Chen
Jimmy Chen  - avatar