Print Array in Reverse

Pages: 12
Hi, im trying to write a program using arrays and for the last part i need to print it in reverse. I know theres something wrong in my void function because im not too sure how to even go about it. Everytime i run it, it spazes out. Any help is very much appreciated.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>

using namespace std;

void start (int boxes [10]);
void move (int squares [10], int x, int y, int z);
void add (int arr [10], int first, int last);
void print (int arr [10]);
void print_reverse ( int arr [10]);


int main ()
{
    int my_arr [10];
    
    cout << "The original array is:\n";
    print (my_arr);
    
    start (my_arr);
    cout << "\n\nThe array after start is:\n";
    print (my_arr);
    
    move (my_arr, 2, 4, 6);
    cout << "\n\nThe array after move is:\n";
    print (my_arr);
    
    add (my_arr, 3, 7);
    cout << "\n\nThe array after add is:\n";
    print (my_arr);
    
    print_reverse (my_arr);
    cout << "\n\nThe array in reverse is:\n";
    print (my_arr);

    
    cout << "\n\n";
    system ("PAUSE");
    return EXIT_SUCCESS;
}

 
void start (int boxes [10])
{
     int index, count;
     
     count = -5;
     for (index = 0; index < 10; index++)
     {
         boxes [index] = count;
         count++;
     }
}

void move (int squares [10], int x, int y, int z)
{
     int temp;
     
     temp = squares [x];
     squares [x] = squares [y];
     squares [y] = squares [z];
     squares [z] = temp;
}

void add (int arr [10], int first, int last)
{
     int m;
     
     for (m = first; m <= last; m++)
         arr [m]++;
}

void print (int arr [10])
{
     int z;
  

     for (z = 0; z < 10; z++)
         cout << arr [z] << " ";
}

void print_reverse (int arr [10])
{
     int z;
     
     for (z = 10-1; z >= 0; z++)
         cout << arr [z] << " ";
     
}
your print_reverse function has a loop that starts at the last index (9) and then increments it and does this until it is less than 0 which would never happen and your array only holds 10 items so after the first time through it is outside the bounds, it needs to be decremented instead
Read aloud for (z = 10-1; z >= 0; z++)
so then i would just turn it around to z <= 0; then?

I did that and it took it a long time to run. And it just reprinted what i had from the previous add function, it didnt reverse.
no, do like ne555 said and read aloud: z = 9 while z is greater than or equal to 0 keep looping and then add 1 to z. so next time around z would be 10 then 11 then 12 and so on, you don't want z to get any bigger then 9 you want it to start to go down until z < 0... so z should be decremented not incremented
so just replace it with z < 0; ? im sorry, im not very good with arrays...

for (z = 10-1; z >= 0; z++)

z = 10-1 // why not just use 9 ? no need for math here. z=9

z>=0 /* this is correct considering z starts at 9. we stop checking when z hits 0. z<=0 makes no sense, we would have to start z at something like -9, which is not how arrays work. There is no such thing as array[-9] */

z++ /* z starts at 9, now you increment z after every loop z++ . so after the first loop z=10 . second loop z++ again, now z = 11. BUT our array is arr[10]. our array is not that big. you need to decrement z . 9 to 8 to 7 to 6 to 5 ... you see the pattern ? z++ is the ONLY problem here. */
so does that mean i would need to change the z++ to z-- to count down then?
something this small is actually very important to understand. I really suggest u draw it out on paper and try to understand what is happening with your loop.

arr [0] [1] [2] [3] [4] [5] .....

for (z = 10-1; z >= 0; z++)
//your loop changes z

arr[z] // z =9 so same as saying arr[9]

keep looping.


keep in mind what we are doing is very very simple. if you get confused, u are thinking to hard.
I guess im not really understanding loops. This is what i have now

1
2
     for (z = 9; z >= 0; z--)
         cout << " " << arr [z];


This prints my array in reverse, but it does it after my add function output.
well done.

if you want to print the array in reverse before you use the add function, just call the print_reverse before you call add().

or you can add a new line after a output to make things look better.

basically

cout<<"/n";

somewhere between functions.

add();
cout<<"/n";
print_reverse();


or stick the cout inside a function at the very end.
Last edited on
Well I want to print the reverse after the add, but as it is right now, when I run it, the reverse prints on the same line as my add, just directly after it.
cout<<"/n"; will create a new line, much like hitting enter between paragraphs. play with it.
I'm away from my program right now, but in my main I already have my output for the reverse "The array in reverse is" and under that is the same output from my add line that's not in reverse. My reverse array continues on the line from my add. So by just putting in the court<<"/n" will move my reverse array into the correct spot and get rid of the line of numbers that are there right now?

Lets just say for example, it looks something like this

 
The array after add is:
0 1 2 3 4 5 5 4 3 2 1 0

The array in reverse is:
0 1 2 3 4 5


Last edited on
looks like you ran the reverse twice.
How could that have happened? I have my above code from my first post, and I fixed my for loop from above.
@Grimlocket46

I think I see what happened..

1
2
3
4
5
6
7
 add (my_arr, 3, 7);
    cout << "\n\nThe array after add is:\n";
    print (my_arr);  // You print the array
    
    print_reverse (my_arr); // Now you print in reverse, but with no newline to separate them
    cout << "\n\nThe array in reverse is:\n";
    print (my_arr); // Now you reprinted the original array 


Re-position print_reverse (my_arr); after the cout << "\n\nThe array in reverse is:\n";
Try this...

void print_reverse( int ar[])
{
int i=9;
while(i<=0)
{
cout<<ar[i]<<" ";
i--;
}
}
Either will work:

1
2
3
4
5
6
7
// First:
for(const int *IP(my_arr + 9); (IP >= Array); --IP)
    std::cout << *IP << ", ";

// Second:
for(int I(9); (I >= 0); --I)
    std::cout << my_arr[I] << ", ";

Personally, the first is better for a few reasons:

-Since IP is constant, the compiler can optimise it.
-Its iteration is more effective since there's fewer computations.

Wazzak
Last edited on
They are not equivalent, the second one starts out of bounds and ends too early.
Pages: 12