English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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?

2007-01-29 11:39:52 · 4 answers · asked by a_screamobassist 2 in Computers & Internet Programming & Design

I need a code for the first line i stated.

2007-01-29 11:51:02 · update #1

Deshi. I would love you for helping me, but the only thing it doesnt do is put them in order. if i put yaz it stays like that and I need to be able to put yaz and get ayz.

2007-01-29 12:05:31 · update #2

Kookiemon it didnt work. I went to compile and run it and came up with errors. Thanks for helping though.

2007-01-29 12:41:39 · update #3

4 answers

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

void main()
{


char chs[3];
char temp;

cin >> chs;

if(chs[0] > chs[1])
{
temp=chs[0];
chs[0]=chs[1];
chs[1]=temp;

}

if( chs[0] > chs[2] )
{
temp=chs[0];
chs[0]=chs[2];
chs[2]=temp;
}
if( chs[1] > chs[2] )
{
temp=chs[1];
chs[1]=chs[2];
chs[2]=temp;

}


cout << endl << chs;
}

2007-01-29 14:15:19 · answer #1 · answered by Mo 3 · 0 0

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] > 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

2007-01-29 13:09:41 · answer #2 · answered by MamaBean 3 · 0 0

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
#include

using namespace std;

int main(int argc, char *argv[]) {
if( argc == 1) {
cout << endl << "Usage : " << argv[0] << " char_1 char_2 ..." << endl;
return 1;
}

char letters[argc-1];
int letters_length = argc-1;

for( int index = 1; index < argc; index++) {
letters[ index-1] = *argv[ index];
}

cout << "Unsorted : " << endl;
for( int index = 0; index < letters_length; index++) {
cout << letters[ index] << " ";
}
cout << endl;

for( int index1 = 0; index1 < letters_length; index1++) {
for( int index2 = index1 + 1; index2 < letters_length; index2++) {
if( letters[index1] > letters[ index2]) {
letters[ index1] ^= letters[ index2];
letters[ index2] ^= letters[ index1];
letters[ index1] ^= letters[ index2];
}
}
}

cout << endl << "Sorted : " << endl;
for( int index = 0; index < letters_length; index++) {
cout << letters[ index] << " ";
}
cout << endl;
}

2007-01-29 12:22:43 · answer #3 · answered by Kookiemon 6 · 0 0

int main( )
{
char a, b, c;
char temp;
cin>> a >> b >> c;

if( b < a )
{
temp= b;
b=a;
a=temp;
}
if (c {
temp=c;
c=a;
a=temp;
}
if (b {
temp=b;
b=c;
c=temp;
}
}//main

I hope this works. If it doesn't, try switching the variables around.

2007-01-29 11:54:53 · answer #4 · answered by deshi 3 · 0 0

fedest.com, questions and answers