Advertisement
xSiRON

Inverti Elementi Array

Nov 27th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. void invertiArray(int A[], const int x);
  8.  
  9. int main(){
  10.     srand(time(NULL));
  11.     int n;
  12.     cout << "Inserisci la dimensione dell'array: ";
  13.     cin >> n;
  14.  
  15.     const int DIM = n;
  16.     int theArray[DIM];
  17.  
  18.     n = 0;
  19.     cout << "\nArray A: [";
  20.     while(n < DIM){
  21.         theArray[n] = rand()%100+1;
  22.  
  23.         if(n < DIM-1)
  24.             cout << theArray[n] << ", ";
  25.         else
  26.             cout << theArray[n] << "]";
  27.  
  28.         n++;
  29.     }
  30.  
  31.     invertiArray(theArray, DIM);
  32.     cout << endl;
  33.  
  34.     cin.ignore();
  35.     cout << "\nPremi INVIO per continuare...";
  36.     cin.get();
  37.     return 0;
  38. }
  39.  
  40. void invertiArray(int A[], const int x){
  41.     int i = x-1, j = 0;
  42.  
  43.     cout << "\nArray A invertito: [";
  44.     while(i >= x/2){
  45.         int temp = A[i];
  46.         A[i] = A[j];
  47.         A[j] = temp;
  48.         j++, i--;
  49.     }
  50.  
  51.     i = 0;
  52.     while(i < x){
  53.         if(i < x-1)
  54.             cout << A[i] << ", ";
  55.         else
  56.             cout << A[i] << "]";
  57.         i++;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement