fork download
  1. //@Author Damien Bell
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7.  
  8. const int size = 5;
  9. int array1[size]={0, 1, 2, 3, 4};//array1[0], array1[1], array1[2], array1[3], array1[4];
  10. int array2[size]={0};
  11.  
  12. int i=5, j=0, k=0;//ignore me for now!
  13.  
  14. /* Reversing an array using for statements
  15.   for(int i=0; i < size; i++){
  16.   array2[i] = array1[size-i-1];
  17.   }
  18.  
  19.  
  20.  
  21.   for (int i=0; i<5; i++){ // This outputs the first array
  22.   cout << array1[i]<<endl;
  23.   }
  24.  
  25.   cout <<endl<< endl;
  26.  
  27.   for (int i=0; i<5; i++){ // This outputs the second array
  28.   cout << array2[i]<<endl;
  29.   }
  30.   */
  31.  
  32. while (i>0){//Outer loop
  33. while(j < k){//Inner loop
  34. array2[j] = array1[i]; // Sets array 1 to the opposite part of array 2.
  35. j++; // increments the controller for the position in array 2
  36. }//Ends the inner loop
  37. k++; //Increments the controller for when the inner loop happens
  38. i--;//Decrements the outer loops counter
  39. }//ends the outer loop
  40.  
  41. for (int i=0; i<5; i++){ // Outputs array 1
  42. cout << array1[i]<<endl;
  43. }
  44.  
  45. cout <<endl<< endl; // Blank lines for formatting purposes.
  46.  
  47. for (int i=0; i<5; i++){ //Output array 2
  48. cout << array2[i]<<endl;
  49. }
  50.  
  51.  
  52.  
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 2724KB
stdin
Standard input is empty
stdout
0
1
2
3
4


4
3
2
1
0