Reversing an array In place

2020-02-14 05:10发布

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 in place without me having to go through the process of creating an entirely new array.

My for loop is running into some problems and I'm not sure where to go from here...i'm using i to take the element at the end and move it to the front and then j is being used as a counter to keep track of the elements...if there is an easier way to do this Any suggestions would be appreciated.

I'm New to this programming language so any extra info is greatly appreciated.

#include <stdlib.h>
#include <time.h>

int Random(int Max) {
  return ( rand() % Max)+ 1;
}

void main() {
  const int len = 8;
  int a[len];
  int i;
  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();

  //Reversing the array in place.
  for (i = a[len] -1; i >= 0, --i;) {
    a[i] = a[j];
    printf("%d ", a[j]);
    j++;
  }


}

标签: c arrays
11条回答
三岁会撩人
2楼-- · 2020-02-14 05:55

You are on the right track but need to think about that last for loop a little more and the assignment operation inside. The loop initialization is off, since i = a[len] - 1 will copy the value of the last entry to i. Since that value is a random number, your index will probably start out of bounds.

Next, you're copying half of the array to the other half and then back. That loop does the following: a[7] = a[0] a[6] = a[1] a[5] = a[2] a[4] = a[3] ...

At this point you've lost all of the initial values in a[4] through a[7].

Try this:

for( i = 0; i < len / 2; i++ ){
    int temp = a[i];
    a[i] = a[len - i];
    a[len - i] = temp;
}

Use a debugger and step through the loop watching the value of i, temp, and each element in the array

查看更多
beautiful°
3楼-- · 2020-02-14 05:56

You can reverse an array in place you don't need an auxiliary array for that, Here is my C code to do that

#include <stdio.h>
int main(void)
 {
    int arr[5]={1,2,3,4,5};
    int size=sizeof(arr)/sizeof(int);   
    int success= reverse(arr,size);
    if(success==1)
        printf("Array reversed properly");
    else
        printf("Array reversing failed");   

    return 0;
}


int reverse(int arr[], int size)
{
    int temp=0;
    int i=0;
    if(size==0)
        return 0;
    if(size==1)
        return 1;

    int size1=size-1;
    for( i=0;i<(size/2);i++)
    {
        temp=arr[i];
        arr[i]=arr[size1-i];
        arr[size1-i]=temp;
    }

    printf("Numbers after reversal are ");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
    return 1;

}
查看更多
Deceive 欺骗
4楼-- · 2020-02-14 05:58
#include<Stdio.h>
#include<string.h>
#define max 25
int main()
{ 
  char arr[max]="0123456789";
  strrev(arr);
  atoi(arr);

  return 0;
}
//you can also use built in functions such as strrev(); string reverse atoi just 
//changes string into integer
查看更多
狗以群分
5楼-- · 2020-02-14 06:03

For starters, instead of this:

for (i = a[len] -1; i >= 0, --i;) {

you want this:

for (i = len-1; i >= 0, --i;) {

but you also only want to go half-way through the array, so it would be

for (i = len-1; i > j, --i;) {
查看更多
该账号已被封号
6楼-- · 2020-02-14 06:06

Just my 2 cents...

#include <stdlib.h>
#include <stdio.h>

int main() {
      int arry[] = {0, 1, 2, 3, 4, 5};
      int* s = arry;
      int* e = arry + (sizeof(arry) / sizeof(arry[0])) - 1;
      while (s < e) {
        *e ^= *s;
        *s ^= *e;
        *e ^= *s;
        s++;
        e--;
      }
      for (size_t i = 0; i < (sizeof(arry) / sizeof(arry[0])); i++) {
        fprintf(stderr, "%d, ", arry[i]);
      }
      fprintf(stderr, "\n");
   }
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-02-14 06:07

Try this;

#include <stdlib.h>
#include <time.h>

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. :)

查看更多
登录 后发表回答