Computer Science, asked by Traitor, 1 year ago

C++ program to swap alternate elements in an 1D array


Traitor: no

Answers

Answered by kvnmurty
4
// swap alternate numbers in an array . so 1st and 3rd.  
// 2nd and 4 are interchanged. again
//  5th and 7th are interchanged. so on....

include files

void main()  {   
    int data[100], i, n, temp;

     cin >> n;
     for ( i = 0; i<n ; i++)   {       cin >> data[i];   }
     for (i = 0 ;  i < =  n - 3 ; i += 2) {    //operate on 4 numbers at a time.        
                temp=data[i];   data[i] = data[i+2];   data[i+2] = temp;   i += 2 ;
               if (i < n - 1) {
                   temp = data[i-1] ;   data[i-1] = data[i+1] ;  data[i+1] = temp ;
               }
      }

       for (i = 0; i < n; i++)
              cout << data[i];

}
Similar questions