Reverse array in place

Ok, so I tried to print, and Array, and then vice versa, used a different array. But I'm trying to create a For Loop that takes an array and cancels all the elements in place, without having to go through the process of creating a completely new array.

There are some problems in my loop, and I'm not sure where to go from here ... I use i to take the element at the end and move it to the front, and then j is used as a counter to track the elements ... if there is more An easy way to do this, any suggestions will be appreciated.

I am new to this programming language, so all the additional information is much 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++;
  }


}
+3
source share
9 answers

while . , , .

  i = len - 1;
  j = 0;
  while(i > j)
  {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    i--;
    j++;
  }

  //Output contents of now-reversed array.
  for(i = 0; i < len; i++)
    printf("%d ", a[i])
+4
void reverse_range(int* buffer, int left, int right)
{
    while (left < right)
    {
        int temp = buffer[left];
        buffer[left++] = buffer[right];
        buffer[right--] = temp;
    }
}

int a[3] = {1, 2, 3};
reverse_range(a, 0, 2);
+5

, , , . , i = a[len] - 1 i. , , , .

, . : a[7] = a[0] a[6] = a[1] a[5] = a[2] a[4] = a[3] ...

[4] [7].

:

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

, i, temp,

+1

2 ...

#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");
   }
+1

:

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

:

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

,

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

;

#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--;
  }

}

, ...:)

. i,a.... .:)

0

, . : int, float, double.

, NULL '\ 0'. , fooobar.com/questions/1506638/... .


#include <stdio.h>

// print items of an array by a format
#define PRINT_ARRAY(array, length, format) \
{ \
    putchar('['); \
    for (size_t i = 0; i < length; ++i) { \
        printf(format, array[i]); \
        if (i < length - 1) printf(", "); \
    } \
    puts("]"); \
}


// reverse an array in place
#define REVERSE_ARRAY(array, length, status) \
    if (length > 0) { \
        for (int i = 0; i < length / 2; ++i) { \
            double temp; \
            temp = array[i]; \
            array[i] = array[length - i - 1]; \
            array[length - i - 1] = temp; \
        } \
        *status = 0; \
    } \
    else if (length < 0) *status = -1; \
    else *status = 1;

#define SUCCESS_REVERSE_ARRAY_MSG "An array succefully reversed"
#define FAILED_REVERSE_ARRAY_MSG "Failed reverse for an array"
#define NO_CHANGED_REVERSE_ARRAY_MSG "An array no changed"


/*
    Print message about status reverse an array
 */
static void
print_msg_reverse_array_status(const int status)
{
    if (status == 0) printf("Status: %s\n", SUCCESS_REVERSE_ARRAY_MSG);
    else if (status == -1) printf("Status: %s\n", FAILED_REVERSE_ARRAY_MSG);
    else if (status == 1) printf("Status: %s\n", NO_CHANGED_REVERSE_ARRAY_MSG);
}


int
main (const int argc, const char *argv[])
{
    // keep value of status
    int status;

    puts("\tExample reverse of an integer array");
    int arr_int[5] = {1, 2, 3, 4, 5};

    status = 0;
    PRINT_ARRAY(arr_int, 5, "%d");
    REVERSE_ARRAY(arr_int, -1, &status);

    // will be an error, since a length is less 0, and the array is not changed
    print_msg_reverse_array_status(status);
    PRINT_ARRAY(arr_int, 5, "%d");

    status = 0;
    REVERSE_ARRAY(arr_int, 0, &status);

    // a length is equal to 0, so an array is not changed
    print_msg_reverse_array_status(status);
    PRINT_ARRAY(arr_int, 5, "%d");

    status = 0;
    REVERSE_ARRAY(arr_int, 5, &status);
    print_msg_reverse_array_status(status);
    PRINT_ARRAY(arr_int, 5, "%d");

    puts("\n\tExample reverse of an float array");
    float arr_float[5] = {0.78, 2.1, -3.1, 4, 5.012};

    status = 0;
    PRINT_ARRAY(arr_float, 5, "%5.3f");
    REVERSE_ARRAY(arr_float, 5, &status);
    print_msg_reverse_array_status(status);
    PRINT_ARRAY(arr_float, 5, "%5.3f");

    puts("\n\tExample reverse of an double array");
    double arr_double[5] = {0.00001, 20000.002, -3, 4, 5.29999999};

    status = 0;
    PRINT_ARRAY(arr_double, 5, "%8.5f");
    REVERSE_ARRAY(arr_double, 5, &status);
    print_msg_reverse_array_status(status);
    PRINT_ARRAY(arr_double, 5, "%8.5f");
    return 0;
}

I use GCC to compile and your result should be as follows

    Example reverse of an integer array
[1, 2, 3, 4, 5]
Status: Failed reverse for an array
[1, 2, 3, 4, 5]
Status: An array no changed
[1, 2, 3, 4, 5]
Status: An array succefully reversed
[5, 4, 3, 2, 1]

    Example reverse of an float array
[0.780, 2.100, -3.100, 4.000, 5.012]
Status: An array succefully reversed
[5.012, 4.000, -3.100, 2.100, 0.780]

    Example reverse of an double array
[ 0.00001, 20000.00200, -3.00000,  4.00000,  5.30000]
Status: An array succefully reversed
[ 5.30000,  4.00000, -3.00000, 20000.00000,  0.00000]

testing environment

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 8.6 (jessie)
Release:    8.6
Codename:   jessie
$ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
0
source

You can change the array in which you don't need the auxiliary array. Here is my C code for this

#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;

}
0
source
#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
-1
source

All Articles