Chris N
Chris N

Reputation: 135

Reverse Array C

I have a problem for the below function.

void reverseAr1D(int ar[], int size)
{
   int temp[size], j = 0;

   for(int i = size - 1; i > -1; i--)
   {
       temp[j] = ar[i];
       j++;
   }

   j = 0;

   for(int i = size - 1; i >- 1; i--)
   {
       *(ar + j) = *(temp + i);
       j++;
   }

   for (int i = 0; i < size; i++)
       printf("%d ", temp[i]);
 }

I would like to reverse every element in the array ar[]. I tried to copy reversely to another array temp[]. Then copy the temp[] back to ar[]. I tried but is not working. Below is my code. Thanks.

#include <stdio.h>

void printReverse1(int ar[], int size);
void printReverse2(int ar[], int size);
void reverseAr1D(int ar[], int size);

int main()
{
    int ar[10];
    int size, i;

    printf("Enter array size: \n");
    scanf("%d", &size);
    printf("Enter %d data: \n", size);

    for (i = 0; i <= size - 1; i++)
        scanf("%d", &ar[i]);

    printReverse1(ar, size);
    printReverse2(ar, size);

    reverseAr1D(ar, size);
    printf("reverseAr1D(): ");

    if (size > 0) 
    {
        for (i = 0; i < size; i++)
            printf("%d ", ar[i]);
    }

    return 0;
 }



  void printReverse1(int ar[], int size)
{
 /* using index – Write your program code here */
  printf("printReverse1(): ");
  for(int i=size-1;i>-1;i--){

  printf(" %d ",ar[i]);
 }
 printf("\n");
 }



 void printReverse2(int ar[], int size)
 {
 /* using pointer – Write your program code here */
   printf("printReverse2(): ");
  for(int i=size-1;i>-1;i--){

  printf(" %d ",*(ar+i));
 }
 printf("\n");
}



void reverseAr1D(int ar[ ], int size)
{
 /* Write your program code here */
 int temp[size],j=0;
 for(int i=size-1;i>-1;i--){

 temp[j]=ar[i];


 j++;
 }
 j=0;
 for(int i=size-1;i>-1;i--){

 *(ar+j)=*(temp+i);


 j++;
 }
 for (int i=0; i<size; i++)
 printf("%d ", temp[i]);

 }

Upvotes: 1

Views: 2311

Answers (4)

SHIVAM SINGHAL
SHIVAM SINGHAL

Reputation: 1

#include<stdio.h>
#define SIZE 5
void main(){
    int i,j,temp,array[SIZE]={1,2,3,4,5},k;

    for(i=0,j=SIZE-1;i<j;i++,j--){
        temp=array[i];
        array[i]=array[j];
        array[j]=temp;
    }   

    for(k=0;k<SIZE;k++){
        printf("%d\n",array[k]);
    }
}

Upvotes: 0

Blaze
Blaze

Reputation: 16876

That's a lot of code to reverse an array of integers. Here's the classic way:

void arrayReverse(int* array, int size) {
    for (int i = 0; i < (size / 2); i++) {
        int swap = array[size - 1 - i];
        array[size - 1 - i] = array[i];
        array[i] = swap;
    }
}

Upvotes: 2

P.W
P.W

Reputation: 26800

What you are doing in your reverseAr1D function is to copy the already reversed elements in temp in the reversed order into ar. So you will end up with the original order of elements.

In your reverseAr1D function you have to change the second for loop to this:

for(int i = 0; i < size; i++){
 *(ar + j) = *(temp + i);
  j++;
}

Upvotes: 2

Frank-Rene Sch&#228;fer
Frank-Rene Sch&#228;fer

Reputation: 3352

May be, the fastest way to do it:

 int *front_p, *back_p;
 for(front_p=&ar[0], back_p=&ar[size-1]; front_p < back_p; ++front_p, --back_p) {
     tmp=*back_p; *back_p=*front_p; *front_p=tmp; 
 }

Upvotes: 2

Related Questions