1

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]);

 }
10
  • 3
    This seems like the perfect time to learn how to debug your programs. Commented Oct 4, 2018 at 10:14
  • 2
    In what way is your code not working? Commented Oct 4, 2018 at 10:15
  • 4
    At first glance you're reversing it twice: you reverse them as you copy them into temp and then reverse them again as you copy them back. So no change overall. Commented Oct 4, 2018 at 10:18
  • 1
    One of the important things in coding is to use proper indentation. This makes your code more readable and also you can understand the flow better. Commented Oct 4, 2018 at 10:18
  • 1
    FWIW, the classic C reverse something algorithm is: i = 0, j = size -1; while (j > i) { swap elements i and j; ++i; --j; }. Commented Oct 4, 2018 at 10:21

4 Answers 4

2

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++;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

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

Comments

0
#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]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.