hey guys..ok so i did this problem about 80% but i cnt figure out the last part which is sort the characters in order of increasing size.

Question:

Input: 4 words (strings with no spaces) and the order in which they are to be displayed, forward alphabetical, reverse alphabetical or in order of increasing size.

Output: the words in their selected order.

Sample run 1:

Please enter 4 words: wall table answer set
In what order would you like to display the words?
a: alphabetical order
r: reverse alphabetical order
s: in order of increasing size
Enter a, r or s: s
1) set
2) wall
3) table
4) answer


Sample run 2:

Please enter 4 words: wall table answer set
In what order would you like to display the words?
a: alphabetical order
r: reverse alphabetical order
s: in order of increasing size
Enter a, r or s: r
1) wall
2) table
3) set
4) answer

No arrays, programmer defined functions or loops. You may use if statements and/or switch statements in your code. Also, you may use the built-in swap() function

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main ()

{

	const int SIZE = 40;
	char first[SIZE], second[SIZE], third[SIZE], fourth[SIZE], order;
	cout<<"Please enter four words: ";
	cin>>first>>second>>third>>fourth;
	
	
	cout << "In what order would you like to display the words?"<<endl;
	cout << "a: alphabetical order"<<endl;
	cout << "r: reverse alphabetical order"<<endl;
	cout << "s: in order of increasing size"<< endl;
	cout << "Enter a, r or s: ";
	cin >> order;
	
	/*if (order == 'a')
		if (strcmp (first,second)<0 && strcmp (first,third)<0 && strcmp(first, fourth)<0)
		//	  if (strcmp (first, third)<0)
		
	cout<< first;
	
	else cout<<"yo"<<endl;*/
	

	string f = first, s = second, t = third, fo = fourth;
	if (order == 'a')
	{
	
	if (f>s)
	swap (f,s);
		if (s>t)
		swap (s,t);
			if (f>s)
			swap(f,s);
				if (f>fo)
				swap (f,fo);
					if (s>fo)
					swap(s,fo);
					if (t>fo)
					swap (t,fo);	   
	cout<<"1) "<<f<<endl;
	cout<<"2) "<<s <<endl;
	cout<<"3) "<<t<<endl;
	cout<< "5) "<<fo<<endl;
			
	}
	
	else if (order == 'r')
	{
	if (f>s)
	swap (f,s);
		if (s>t)
		swap (s,t);
			if (f>s)
			swap(f,s);
				if (f>fo)
				swap (f,fo);
					if (s>fo)
					swap(s,fo);
					if (t>fo)
					swap (t,fo);
	
	
	cout<<"1) "<<fo<<endl;
	cout<<"2) "<<t <<endl;
	cout<<"3) "<<s<<endl;
	cout<< "5) "<<f<<endl;
			
	}
	
	return 0;
	
	
}
Ancient Dragon commented: Thanks for using code tags correctly. +26
Fbody commented: next time, make it part of your original thread +0

Recommended Answers

All 7 Replies

A much better post, too bad it's not in your other thread...

Anyway...

You would have to use the strlen function to get the string lengths. Based on what I see, you should be able to get the rest.

You don't use strlen() to get the length of std::string object -- use its length() method

if( f.length() < s.length() )
{
    swap(f,s);
}

Now do that for all combinations if the strings f, s, t, and fo;

Also, a much better way to format your code is to align all the if statements up, unless they are nested, because it makes the code much easier to read and understand.

if( a < b )
   swap(a,b);
if( b < c )
   swap(b,c);
if( c < d )
   swap(c,d);
// nested if statements are like this
if( a < b)
{
    if( b < c)
    {
        if( c < d)
        {
             swap(a,d);
        }
    }
}
commented: you are awsome :) +1

hey guys thakns for all the help:)
i have a question about the nest??
if i use this code(code below >>>)..do i need to use only swap statement or do i need to use swap for each if statement??? and along with that when i tried to see compare d with a,b and c i had to go through 4 statement..is there is easy way???

if (a<b)
{
     if (b<c)
     {
         if (c<d)
          {
          swap(a,d)

and again thanks for your help!!!!:)

hey guys thakns for all the help:)
i have a question about the nest??
if i use this code(code below >>>)..do i need to use only swap statement or do i need to use swap for each if statement??? and along with that when i tried to see compare d with a,b and c i had to go through 4 statement..is there is easy way???

if (a<b)
{
     if (b<c)
     {
         if (c<d)
          {
          swap(a,d)

and again thanks for your help!!!!:)

I believe Ancient Dragon's point regarding nesting was a formatting issue, not a coding issue. I didn't read his post as a suggestion to use or not use nested loops. He is saying to line things up to make it easy for the HUMAN reader to follow. The compiler couldn't care less. It's not 100% obvious whether you INTEND your if statements to be nested in post 1 (the indentation suggests that intent). Regardless, as written, they are NOT nested.

You are prevented from using the common sorts by the prohibition against loops. Thus you need to use a bunch of if statements, nested or otherwise. There is more than one way to do this. Get four items and organize them step by step till they are in the right order. Write down the exact steps as you do them. At that point you have your algorithm. THEN try to code it. That would be my approach. Figure out exactly what you do in real life, write it down, then turn your English instructions into C++ instructions.

@ VernonDozier
thank you so much for explaining...i sorted them the same way i would do in real life...step by step...and i worked..but i was just curious to know if there is any other way with less steps...and im new to c++ and thanks you and also dragon for pointing out the format errors...again thanks for the help :)

>>is any other way with less steps

If you didn't have the restrictions placed on the project, definitely. With current restrictions and without seeing your current code, who knows if it might be done with fewer lines/comparisons or not.

@ Lerner ... Hey thanks for the reply.. My code is working fine .. N I do understand the restriction part .. Maybe if I get some free time I try to figure out with LEss steps .. N again thanks to everyone whi replied :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.