Reversing an array In place

后端 未结 11 903
温柔的废话
温柔的废话 2020-12-21 04:20

Okay so I\'ve tried to print and Array and then reverse is using another array But I\'m trying to create a For Loop that will take an array and reverse all of the elements i

11条回答
  •  借酒劲吻你
    2020-12-21 05:24

    Try this;

    #include 
    #include 
    
    int Random(int Max) {
      return ( rand() % Max)+ 1;
    }
    
    void main() {
      const int len = 8;
      int a[len];
      int i,end;
      int j = 0;
      Randomize() ;
    
      srand(time(0));
      //Fill the Array
      for (i = 0; i < len; ++i) {
        a[i] = rand() % 100;
      }
    
      //Print the array after filled
      for (i = 0; i < len; ++i) {
        printf("%d ", a[i]);
      }
      printf("\n");
      getchar();
    
      for (i = 0; i < n/2; i++) {
          t = a[i];
          a[i]   = a[end];
          a[end] = t;
          end--;
      }
    
    }
    

    Hope this helps... :)

    Just for suggestion. Try to use meaningful variable name instead of just i,a.... That will help you while writing a bigger code. :)

提交回复
热议问题