reverse number arrangement in another array problem


The problem is :

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.


Here is my code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
#include<conio.h>

main()
{
      int num[10];
      int x;
      
      for(x=0;x<10;x++)
      {
                       printf("Enter number %d : ",x);
                       scanf("%d", &num[x]);
                       
                       }
                       
                       for(x=0;x<10;x++)
                       {
                                        printf(" %d ",num[x]);
                                        
                       }
                       
                       //clrscr ();
                       num[x]=0;
                       
                       for(x=10;x>-1;x--)
                       {
                                 printf(" %d ",num[x]);
                                 
                                 }
                                 
                                 getch();
                                  
                                 }
                                         
                                        
      
      
      
      
      
      
      
      
      
      
      



What codes do I lack in this program? What code shall I replace this last portion of my program? I hope you all can help me. Thank you!
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.

Here is my new code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
#include<conio.h>

main()
{
      int num[10];
      int reverse[10];
      int x,numPrinted;
      
      for(x=0;x<10;x++)
      {
                       printf("Enter number %d : ",x);
                       scanf("%d", &num[x]);
                       
                       }
                       
                       for(x=0;x<10;x++)
                       {
                                        printf(" %d ",num[x]);
                                        
                       }
                       
                       num[0]=reverse[9];
                       reverse[9]=num[0];
                   
                       
                               printf("n Your number in reverse are : n");
                               for(x=10;x>=0;x--)
                               {
                                      printf(" %d ",reverse[x]);
                                                                 
                                 
                                 }
                                 
                                 getch();
                                  
                                 }
                                         
                                    



Still the program did not print the elements in the reverse array. I am really confused about this.
closed account (D80DSL3A)
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".

Hope this helps.
closed account (zwA4jE8b)
for(i = 0; i < 10; i++)
one line here to reverse!

print array here
Maybe you can try swapping the arrays values to cut the loop iterations in half.

I got the new code and its working but I think this must be in a form of a right loop :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
#include<conio.h>

main()
{
      int num[10];
      int reverse[10];
      int x,numPrinted;
      
      for(x=0;x<10;x++)
      {
                       printf("Enter number %d : ",x);
                       scanf("%d", &num[x]);
                       
                       }
                       
                       for(x=0;x<10;x++)
                       {
                                        printf(" %d ",num[x]);
                                        
                       }
                       
                       //num[0]=reverse[9];
                       reverse[9]=num[0];
                       reverse[8]=num[1];
                       reverse[7]=num[2];
                       reverse[6]=num[3];
                       reverse[5]=num[4];
                       reverse[4]=num[5];
                       reverse[3]=num[6];
                       reverse[2]=num[7];
                       reverse[1]=num[8];
                       reverse[0]=num[9];
                       
                       
                               printf("n Your number in reverse are : n");
                               //for(x=10;x>=0;x--)
                               for(x=0;x<10;x++)
                               {
                                      printf(" %d ",reverse[x]);
                                                                 
                                 
                                 }
                                 
                                 getch();
                                  
                                 }
                                         
                                        
      
      



This line :
1
2
3
4
5
6
7
8
9
10
  reverse[9]=num[0];
                       reverse[8]=num[1];
                       reverse[7]=num[2];
                       reverse[6]=num[3];
                       reverse[5]=num[4];
                       reverse[4]=num[5];
                       reverse[3]=num[6];
                       reverse[2]=num[7];
                       reverse[1]=num[8];
                       reverse[0]=num[9];



^ 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 ?




closed account (D80DSL3A)
Wow. You actually DID go with the 10 explicit assignments! It works, but it should be done with one line.

The magic line of code you're looking for is: reverse[9-x] = num[x];
Put that on line 14. You can then remove lines 24-33.

This is what CreativeMFS and I were hinting at.

Also, for(x=0;x<10;x++) will give both arrays in the desired order.
But you sill have not opened the file and read the numbers from the file into the array! ...step one
Topic archived. No new replies allowed.