A program that reads three characters and determine their order.
It is from my C++ class. I probably could figure it out if my prof would have told us we needed to read and do this problem in advance. One day is not enough time to do this in. Can anyone help me?
C++ Question. I NEED HELP!?
The general solution to this problems is Bubble sort but when you have small number of characters ( 3 here ) , its better to hand code the comparison levels as i did below :
#include %26lt;iostream.h%26gt;
void main()
{
char chs[3];
char temp;
cin %26gt;%26gt; chs;
if(chs[0] %26gt; chs[1])
{
temp=chs[0];
chs[0]=chs[1];
chs[1]=temp;
}
if( chs[0] %26gt; chs[2] )
{
temp=chs[0];
chs[0]=chs[2];
chs[2]=temp;
}
if( chs[1] %26gt; chs[2] )
{
temp=chs[1];
chs[1]=chs[2];
chs[2]=temp;
}
cout %26lt;%26lt; endl %26lt;%26lt; chs;
}
Reply:Hey Mohammad! Email me at a_screamobassist@yahoo.com I need to ask you some questions about the code and some other C++ questions. Report It
Reply:int main( )
{
char a, b, c;
char temp;
cin%26gt;%26gt; a %26gt;%26gt; b %26gt;%26gt; c;
if( b %26lt; a )
{
temp= b;
b=a;
a=temp;
}
if (c%26lt;a)
{
temp=c;
c=a;
a=temp;
}
if (b%26lt;c)
{
temp=b;
b=c;
c=temp;
}
}//main
I hope this works. If it doesn't, try switching the variables around.
Reply:It's been a while since I used C++, so I didn't attempt to code this myself. Hopefully you can use this pseudocode. (I wrote this in your post in a different category.)
The easiest sorting algorithm to code is the bubble sort. It's not used for practical purposes, but for a class, this will work fine.
function bubble_sort(list L, number listsize)
loop
has_swapped := 0 //reset flag
for number i from 1 to (listsize - 1)
if L[i] %26gt; L[i + 1] //if they are in the wrong order
swap(L[i], L[i + 1]) //exchange them
has_swapped := 1 //we have swapped at least once, list may not be sorted yet
endif
endfor
//if no swaps were made during this pass, the list has been sorted
if has_swapped = 0
exit
endif
endloop
endfunction
Reply:Here you go. This takes a command-prompt argument of characters.
Usage : file_name a e d g t r g
Obviously you can limit it to just three characters.
#include %26lt;cstdlib%26gt;
#include %26lt;iostream%26gt;
using namespace std;
int main(int argc, char *argv[]) {
if( argc == 1) {
cout %26lt;%26lt; endl %26lt;%26lt; "Usage : " %26lt;%26lt; argv[0] %26lt;%26lt; " char_1 char_2 ..." %26lt;%26lt; endl;
return 1;
}
char letters[argc-1];
int letters_length = argc-1;
for( int index = 1; index %26lt; argc; index++) {
letters[ index-1] = *argv[ index];
}
cout %26lt;%26lt; "Unsorted : " %26lt;%26lt; endl;
for( int index = 0; index %26lt; letters_length; index++) {
cout %26lt;%26lt; letters[ index] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
for( int index1 = 0; index1 %26lt; letters_length; index1++) {
for( int index2 = index1 + 1; index2 %26lt; letters_length; index2++) {
if( letters[index1] %26gt; letters[ index2]) {
letters[ index1] ^= letters[ index2];
letters[ index2] ^= letters[ index1];
letters[ index1] ^= letters[ index2];
}
}
}
cout %26lt;%26lt; endl %26lt;%26lt; "Sorted : " %26lt;%26lt; endl;
for( int index = 0; index %26lt; letters_length; index++) {
cout %26lt;%26lt; letters[ index] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment