You are to read 10 numbers from a data file into an array named list. Create another 10 element array named Reversed that is to contain the same items as list but in reverse order. For example, the first element in list will be placed in the last position of Reverse, the second element in list will be placed in the second-to-last position in Reverse, etc. After Reversed has been created, output the contents of both arrays.
Now my problem is :
I really don't have an idea on how to make another array for the reverse that puts the first element in the list array to the last element in the reverse array. My instructor said "My output is correct but my program is wrong" and he will not accept it. I am really confused about this program.
It looks like he wants you to create another array called Reverse, copy your original array into reverse backwards, and then output the contents of Reverse.
Arrays go from 0 to n-1, so line 23 is out of bounds (as well as 27 in first iteration)
Instead of printing the cell put it in the other array, that it is traversed in the other direction.
read 10 numbers from a data file into an array named list.
Do this first.
Then as Browni3141 says:
You will now have your 10 numbers in an array named list[10]
Declare another array reverse[10] and using a for loop copy the 10 elements from list[10] into array reverse[10] starting with element 9 (of list[10]) and ending with element 0 using negative incrementing (--i) in the loop.
Printing out reverse[10] starting with element [0] will reverse the number order in the list array.
I already declared the reverse array but still I don't know the exact code for copying the elements in the list array to the reverse array. I tried to copy the elements from the list to reverse array but still this didn't work.
Line 23 is a bad idea. It copies over num[0] with an uninitialized value.
Line 24 is going in the right direction.
Next you would want: reverse[8]=num[1];
then: reverse[7]=num[2];
and so on until all 10 reverse[] elements have been assigned values.
But don't do it with 10 explicit assignments like that! Do it with one line inside the first for loop.
Use the variable x to figure which element of reverse[] should be assigned. How are the values of 1 and 8 related. 7 and 2? 6 and 3? Use some algebra here.
Also, when you print the contents of the reverse[], you can output the values in straight order (0-9) because the values in the array are already "reversed".
^ This is the same as this line right? for(x=9;x>-1;x--) <-- How come my instructor told me that this line is not the right loop for the other array? How will I change this to the right loop ?