/* can any 1 tell me wats the problem."Que: reverse an array without using temporary array " This works for N =5 but when N =6 it displays the original values of an array. */

#include <stdio.h>
#include <conio.h>
#define N 6
void print(int b[N])
{
	int i;
	printf("\n");
	for(i=0;i<N;i++)
	{
		printf("%3d",b[i]);
	}
}
void swap(int a[N])
{
	int i,j;
	for(i=0,j=N-1;i<N,j>=0;i++,j--)
	{
		if(i!=j)
		{//swapping
			a[i]=a[i]+a[j];
			a[j]=a[i]-a[j];
			a[i]=a[i]-a[j];
		}
		else
		{
			break;
		}
	}
}
void read(int b[N])
{
	int i;
	for(i=0;i<N;i++)
	{
		printf("\n [%d] : ",i+1);
		scanf("%d",&b[i]);
	}

}
void main()
{
	int a[N],i,j;
	clrscr();
	read(a);
	print(a);
	swap(a);
	print(a);
	getch();
}

Dani AI

Generated

Nice catch by on stopping at the midpoint. One more gotcha in your original loop: the condition i<N, j>=0 uses the comma operator, so only the rightmost expression (j>=0) actually controls the loop. That keeps iterating even after i passes the midpoint, guaranteeing the second half gets swapped back to the original order. Making the loop condition solely about the two pointers (left vs. right) avoids both problems.

Also, the add/subtract swap is risky. With int, signed overflow is undefined behavior in C, so values near INT_MAX or INT_MIN can silently break your program. You do not need a temporary array to reverse in-place, and using a single temporary variable is still O(1) extra space and perfectly valid for the "no temp array" requirement. It is clearer, safer, and typically compiles to optimal code.

Here is a tidy, portable version you can drop in. It uses size_t for the length, avoids non-standard headers, and stops exactly at the midpoint:

void reverse(int *a, size_t n)
{
    for (size_t i = 0, j = n ? n - 1 : 0; i < j; ++i, --j) {
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
}

Practical tips:

  • Pass the length instead of relying on #define N so the function works for any array size.
  • Test with n = 0, 1, even, and odd lengths, and with large magnitudes to catch overflow-prone swaps.
  • If you insist on XOR swap, ensure i < j so you never swap the same element with itself.

Recommended Answers

All 6 Replies

i and j will never be equal when the item count is even. You're swapping both halves of the array in that case, which ultimately produces the original sequence. Perhaps instead of using that weird logic for your swapping loop, just use the relationship of i and j as the primary condition:

void swap(int a[N])
{
    int i, j;
    
    for (i = 0, j = N - 1; i < j; i++, j--)
    {
        a[i]=a[i]+a[j];
        a[j]=a[i]-a[j];
        a[i]=a[i]-a[j];
    }
}

dev90:
There's mistake in your logic:--it will also not work for N=2,4 i.e, even numbers. because for N=6 when
i=0 j=5,
i=1,j=4,
1=2,j=3,
i=3,j=2,
i=4,j=1,
i=5 j=1,
what is been seen here is that the array elements are swaped twice for the first half half part, array is reversed and for the next half part it is reversed again making it original array.

: i think Narue already explained that! You said nothing new!

NP:- yep I saw it later when I ve done my research on that question and given my time , so I thought to post what i see otherwise i would not ve posted it, and that's why I didn't post the code as it is similar to that of Narue's post.

Thanks Mr.Narue and Mr.avinash
ihavnt thought dat bt i've solved it in sm code of mine giving condition (i<j)
as outer loof of FOR.

bt usr logic is better and now i understand my mistake.
so thank you 1nce again......

Dude, this isn't twitter; spell your words properly. Even native English speakers would have trouble translating that gibberish, and Daniweb has many members who aren't native speakers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.