Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   word sorting using array swap (http://www.go4expert.com/forums/word-sorting-using-array-swap-t16899/)

Yasiir2 13Apr2009 13:01

word sorting using array swap
 
Dear Friends

I wrote a code that prompt user to give 5 different words or name and than it will print given names or words in ascending order .code is running successfully but I am using Dec.C++ 4.9.9.2 and in this compiler my code is not running it close the window after getting 5 strings.please help me in this code.
Code:

#include<iostream>
#include<string>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()
{
 char array[5];
 char temp;
 for(int i=0;i<5;i++)
 {
  cout<<"enter string :"<<i+1<<": ";
  cin>>array[i];
    }
 for(int i=0;i<5-1;i++)
 {
  for(int j=i;j<5;j++)
  {
  if(array[i]>array[j])
  {
    temp=array[i];
    array[i]=array[j];
    array[j]=temp;
  }
  }
 }
 cout<<"sorting string\n";
 for(int i=0;i<5;i++)
 {
 cout<<array[i]<<endl;


Yasiir2 13Apr2009 13:03

Re: word sorting using array swap
 
sorry i forgot to mention that the code is running in trubo c but not in Dev C++.

nimesh 14Apr2009 10:10

Re: word sorting using array swap
 
Hi Yasiir,

I'm not familiar with Dev C++, but I can try with the program part.

Is this the complete code?
I can see that the last for loop is not completed.
Also the int main is not completed.

I don't know if this will be true for Dev C++ or not,
Does Dev C++ open a new window for I/O of the program?
If Yes, then I believe that after you display the sorted results, you'll have to wait for user input (action) to close the window, so that the last output is displayed to the user and the window still remains open, instead of the window closing directly, in which case the results will be displayed but it will not be visible to the user because the window gets closed.

Let me know If I'm right or not...

- Nimesh

Yasiir2 16Apr2009 09:19

Re: word sorting using array swap
 
Dear Nameesh

Yes you are right that that Dev C++ open a new window for IO of the programme and it is also true that it close directly after propmting 5 character.However I tried more and got the solution.I just add one more header file i.e stdio.h and conio.h and getch(); at the end and my code is correct now.But please explain me as I am in beggining of C++.why my window clos directly after taking 5 char.??

Thanks
Code:

#include<iostream>
#include<string>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()
{
 char array[5];
 char temp;
 for(int i=0;i<5;i++)
 {
  cout<<"enter string :"<<i+1<<": ";
  cin>>array[i];
    }
 for(int i=0;i<5-1;i++)
 {
  for(int j=i;j<5;j++)
  {
  if(array[i]>array[j])
  {
    temp=array[i];
    array[i]=array[j];
    array[j]=temp;
  }
  }
 }
 cout<<"sorting string\n";
 for(int i=0;i<5;i++)
 {
 cout<<array[i]<<endl;
}
 getch();
}


nimesh 16Apr2009 09:31

Re: word sorting using array swap
 
It's the normal behavior for the program.

You said to display it, so it displays the output and quits.

How will the program know that the user wants to watch the output for hours and then close the window.

So, we have to write and extra statement, for the user to give an additional input, any input, to indicate that he has done his output viewing and needs to close the window.

So, the statement... getch();

You can also add an output line like... "Press any key to close this window"

Yasiir2 16Apr2009 12:31

Re: word sorting using array swap
 
Thanks sir
I got the point.
I expect you will support me always like this.

Once again thank you.

Yasir


All times are GMT +5.5. The time now is 08:01.