0

I'm trying to reverse an array without using any second array. Here's the code I've written, but for some reason it doesn't seems to be working.

#include<stdio.h>
#include<conio.h>
void getarrayvalues();
void main()
{
 int k,n;
 int i=0;
 int a[100];
 printf("Enter the value of n");
 scanf("%d", &n);
 printf("Ener the values of array");
 for(i=0;i<n;i++)
 {
   scanf("%d", &a[i]);
 }
 k=n;
 if(n%2==0)
 {
   for(i=0;i<n;i++)
   {
      a[i]=a[k];
      k--;
   }
 }
 else
 {
   for(i=0;i<n;i++)
   {
     if(k==((n/2)+1))
     {
       continue;
     }
     else
     {
       a[i]=a[k];
       k--;
     }
   }
 }
 printf("reverse values are");
 for(i=0;i<n;i++)
 {
  printf("%d", a[i]);
 }
}

After entering the array values, it returns to the blue code writing screen and doesn't prints the output. Any ideas ?

6
  • 1
    How about std::reverse? Commented Sep 20, 2013 at 19:14
  • 1
    C++ and C? Pick one. It looks like C to me. Commented Sep 20, 2013 at 19:15
  • 4
    Also, here is the obligatory main rant: void main() should be int main(). Commented Sep 20, 2013 at 19:16
  • You do assignment, you need swapping Commented Sep 20, 2013 at 19:16
  • 3
    Step through the code line by line in the debugger. At some point, the code will not do what you think. That's where the bug is. Commented Sep 20, 2013 at 19:23

5 Answers 5

3

The first error I see is that you start k as n. Well, position n of your array a is one after the last element, since array index starts from zero. You should start it as:

k=n-1;

Second, when you do

for(i=0;i<n;i++)
{
   a[i]=a[k];
   k--;
}

you overwrite the first half of the array with the inverted second half, but loses the original values of the first half in the process. If you don't save the previous value of a[i] somewhere else, it will be lost by the assignment. A good place the put the value of a[i] is, since you are reversing the array, in its symmetrical opposite position, that happens to be a[k]. The code would look like:

for(i=0;i<n;i++)
{
   int temp = a[i];
   a[i]=a[k];
   a[k]=temp;
   k--;
}

But doing so, when i == n/2, you will already have swapped the whole array, thus you must stop there:

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

Lastly, there is no need to treat differently the case where n is odd or even, because if it is odd, the middle element will already be where it needs to be...

Sign up to request clarification or add additional context in comments.

1 Comment

@Ivella This worked!Thanks for the suggestion of not checking the even-odd possibility.This reduced the length of my program and eventually made it more effective.
1

You're only assigning a[i] values. You need to assign a[k] values too. For example :

int temp;

//code

temp = a[i];
a[i] = a[k];
a[k] = temp;

You're basically duplicating the values of the array now.

EDIT

You should also look at what ivella said. If you don't take that into account, you'll end up accessing null memory (your program will crash)

Comments

1

Just do this-

for(i=0;i<n/2;++i)
swap(a+i,a+((n-1)-i));

Where swap(&x,&y) swaps the values of variables x and y.

Comments

0

You seem to be using C, not C++. You can accomplish the reversal without a second array like so:

int array[SIZE];
// fill in array
for (int i = 0; i < SIZE / 2; ++i)
{
    int t = array[i];
    array[i] = array[SIZE - i - 1];
    array[SIZE - i - 1] = t;
}

A C++ solution can be done in a few ways depending on the requirements. e.g.:

std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::reverse(v.begin(), v.end());

Comments

0
#include<stdio.h>
int main()
{
    char s[]="ratiranjankumar";
    int i,j;
    int count=0;

    for(i=0;s[i]!='\0';i++)
    {
        count++;
    }

    for(j=0;j<=count/2-1;j++)
    {
        s[j] ^= s[count-j];
        s[count-j] ^= s[j];
        s[j] ^= s[count-1];
    }

    for(i=0;i<=count;i++)
    {
        printf("%c",s[i]);
    }
}   

1 Comment

Please consider adding some comments describing, how this code works

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.