tags:

views:

72

answers:

1

why this code does not work? it does not show me output

#include <stdlib.h>
#include <iostream>
#include <string.h>
void Sort(int *arr,int length){
    int *iter=arr;
    char buf[12],buf1[12];
     while ((iter++)< (arr+length)){

          if (iter==arr || (strcmp(itoa(*iter,buf,10),itoa(*(iter-1),buf1,10))>=0)){
       iter++;
          }
          else{
              *iter^=*(iter+1);
              *(iter+1)^=*iter;
              *iter^=*(iter+1);
              iter--;
          }

          }


     }

     int main(){

         int a[]={1,2,10,100,19,21,2,4,31};
         int n=sizeof(a)/sizeof(int);
         Sort(a,n);
          for(int i=0;i<n;i++)
            std::cout<<a[i]<<"  ";




          return 0;
     }

please help

+3  A: 

Here is the output using gcc 4.5.1:

> g++ -o test test.cpp
> test.exe
1  2  10  100  19  21  2  4  31

As you can see, it compiles and runs fine in my place. Whether it works like intended is another matter though.

Are you sure you saved your changes before compiling ? What compiler are you using ?


Moreover, you should better use a std::vector to store the integers and std::sort with a custom comparator object to do the sort.

ereOn
The expected output is 1 10 100 19 2 2 31 4. Sorting by digit.
Manoj R
@Manoj R: Sure. But the OP states that his example does not output anything at all. So I guess his first problem is to solve this before.
ereOn
@ereOn :- Oh my apologies for miscommunication.
Manoj R
You don't need a custom comparator. Just use a `std::vector<std::string>`, and sort that. I.e. move the int-to-string conversion before the sort phase.
MSalters