Reversed array

I need to print the contents of an array both in the original order and after inverting its contents. My problem is doing the second part without using reverse(), any sugguestions?

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

main(){
	//rng
	srand(int(time(NULL)));

	int i=0, dim=0, idim=0, CASUALI[0]={}, INVERSO[0]={};
	
	cout<<"Scegli la lunghezza dell'array: ";
	cin>>dim;
	cout<<"Stampa ordinata \n";
	
	for(i=0; i<dim; i++){
		CASUALI[dim]= rand()%100+1;
		cout<<CASUALI[dim]<<"\t";
	}
	cout<<endl<<"Stampa inversa \n";
	//print inverted
	for(i=4; i!=0; i--){
		INVERSO[idim]=CASUALI[dim];
		cout<<INVERSO[idim]<<"\t";
		dim--;
		idim++;	
	}

	
}


My idea was of getting the int in the last cell of the first array, put it in the first of the second, then go to the second to last of the first array and put it in the second of the second array and so on.
Last edited on
You don't need to change the order or the elements in the array. Just start at element [n-1] and loop backwards (subtract 1 each loop) until you get to element [0]. Print out each element inside the loop.
How many elements are there in an array that has 0 elements?

Your code assumes that there are as many as the user wishes.
That is not true. That is undefined behaviour.
Topic archived. No new replies allowed.