Solved

hey jkr how do i print 3 letters out in alphabitical order

Posted on 2004-09-29
10
215 Views
Last Modified: 2010-04-01
#include <iostream>
using namespace std;

int main () {

string Letters;

Letter[0];
Letter[1];
Letter[2];
      
      cout << "Please enter three characters:" << endl;
      cin  >> Letter;
      
      if Letter[0]
   

if ( Letter[0] == Letter[1] || Letter[1] == Letter[2] || Letter[0] == Letter[2])
      cout << endl << "At least two characters are identical" << endl;


      return 0;
}
0
Comment
Question by:tedschnieders
  • 6
  • 2
  • 2
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Homework?
It is explained in the question you just have accepted. Using jkr's code:

#include <list>
#include <iostream>
using namespace std;

// some code to fill Letter[] array

    list<char> lc;
    list<char>::iterator it;

    for ( int i = 0; i < 3; ++i)
        lc.push_back(Letter[2] );

    lc.sort();

    for ( it = lc.begin(); it != lc.end(); ++it)
        cout << *it;
    cout << endl;

0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Sorry, there is a typo:
        lc.push_back(Letter[2] );
must be:
        lc.push_back(Letter[i] );
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Obviously you have to put there the portion that fills the array, maybe you can integrate all the code you have received and post it for review.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
This portion is unnecesary:
Letter[0];
Letter[1];
Letter[2];
0
 

Author Comment

by:tedschnieders
Comment Utility
see thats just way to complex for me right now
 
can i use some kind of mask or if statement

sorry i am such a pain in the ass but i do learn alot from u guys

thanks
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
Really this is the easiest way in C++, I can explain it step by step:

// Some "headers" needed by C++
#include <list>
#include <iostream>
using namespace std;

// Creates a "list" similar to an array, with sorting capabilities
    list<char> lc;

// Creates an "iterator", an object that is used to transverse the list
    list<char>::iterator it;

// This portion just insert three elements in the list (push_back method does)
    for ( int i = 0; i < 3; ++i)
        lc.push_back(Letter[2] );

// Here is the sortion method, couldn't be easier
    lc.sort();

// Here ordered data is displayed using the iterator object instead a "for" loop
    for ( it = lc.begin(); it != lc.end(); ++it)
        cout << *it;

// Adds a new-line character
    cout << endl;

If you want to make it with pure "if"s then will be look messy, for 3 element there are six ordering cases:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

then you have to make 6 "if"s, similar to (1st case):
if ( Letter[0] < Letter[1] && Letter[1] < Letter[2])
    cout << Letter[0] << Letter[1] << Letter[2] << endl;

you will need 5 more blocks like this.


 

0
 

Author Comment

by:tedschnieders
Comment Utility
thank i appreciate it
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
A small typo:
// This portion just insert three elements in the list (push_back method does)
    for ( int i = 0; i < 3; ++i)
        lc.push_back(Letter[2] ); // adds only last letter
should certainly be
// This portion just insert three elements in the list (push_back method does)
    for ( int i = 0; i < 3; ++i)
        lc.push_back(Letter[i] ); // adds all three letters
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Hey Steh, I have posted correction in my second comment!
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
Sorry, overlooked that.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now