0

I am a beginner in C++ and may be the question is very basic but I am not understanding the point. I have an array which is {10,20,30,40,50,60,70,80,90,100}; and I want to print it like {100,90,80,70,60,50,40,30,20,10}. I also have the below code for this:

#include <iostream>

using namespace std;

int main()
{

  int ara[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
  int i, j, temp;
  for(i = 0, j = 9; i < 10 && i<j; i++, j--) {
    temp = ara[j];
    ara[j] = ara[i];
    ara[i] = temp;

  }


  for(i = 0; i < 10; i++) {
    cout<<ara[i]<<endl;
  }
  return 0;
}

But I am not sure why I am using && i<j in the below line. If I do not use it then my array out put comes like {10,20,30,40,50,60,70,80,90,100}; I want a clear explanation so that I can understand.

for(i = 0, j = 9; i < 10 && i<j; i++, j--) {
user3751012
  • 533
  • 1
  • 8
  • 20
  • 2
    Normally, this would be done using `std::sort`. But the thing is the `i < 10` is the truly redundant part. `j` is always less than 10. – chris Jun 20 '14 at 04:34
  • I recommend you pick up a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++ and start reading. – Captain Obvlious Jun 20 '14 at 04:37
  • This isn't a sort, it's reversing the array. Without the `i – Blastfurnace Jun 20 '14 at 04:40

3 Answers3

4

The way that you're doing it, you're basically swapping the first element in the array with the last element in the array and working your way towards them middle of the array.

Like an array like: 1,2,3,4,5

The first iteration would swap 1 and 5. The second iteration would swap 2 and 4. The third iteration nothing will be swapped.

At this point your array will look like:

5,4,3,2,1

In the third iteration i = j.

if you didn't have the i < j condition, the for loop will continuing iterating.

The fourth iteration would swap the 4 and 2 again. The fifth iteration would swap the 5 and 1 again. Thus, your array would become:

1,2,3,4,5 again.

So the i < j condition is crucial to reverse an ascending array. The i < 10 part is redundant.

Side note:

Its probably much easier to use std::sort

cmo
  • 66
  • 7
  • I was going to suggest that (`std::sort`) as well, either that or build a sorting algorithm; all this does is swap them around, it doesn't take into account the actual values of what's stored in the array. – Thermatix Jun 20 '14 at 04:46
2

It appears that the algorithm just simply switches elements going from the outside in. Here's what's happening:

{10, 20, 30, 40, 50, 60, 70, 80, 90, 100} -> {**100**, 20, 30, 40, 50, 60, 70, 80, 90, **10**}

{**100**, 20, 30, 40, 50, 60, 70, 80, 90, **10**} -> {**100**, **90**, 30, 40, 50, 60, 70, 80, **20**, **10**}

and so on.

However, it needs to stop once it reaches the center; otherwise, it would simply flip after i=5 and j=4.

  • (i=5, j=4) {100,90,80,70,60,50,40,30,20,10}

If it were to continue after this, it would switch elements 5 and 4 again, resulting in...

{100,90,80,70,**50**,**60**,40,30,20,10}

The way it is right now, you only need the i < j conditional to correctly sort this one out.

Notice, that this is not a true sorting mechanism, as the sorting mechanism is based on the order of the array, not its contents. A true sorting algorithm arranges an array's elements based on high -> low, low -> high, or some other deviant rule. This algorithm simply flips the order of the array around.

CinchBlue
  • 6,046
  • 1
  • 27
  • 58
1

U can use std::sort for sorting..what you are trying to do is called reversing..