Thread: Sorting inputed letters alphabetically...

  1. #1
    Registered User IanelarAzure's Avatar
    Join Date
    Sep 2002
    Posts
    7

    Question Sorting inputed letters alphabetically...

    I have a clue what to do, but am stuck right here. So far it only runs six times(correctly) the only problem is that it doesnt sort the letters. I know I have to convert them to numbers and back to letters, but cant figure out how. Here is my code:

    Code:
    #include<iostream>
    #include<string.h>
    using namespace std;
    
    int main(){
    	char letter[26];
    	int i=0,h=0,list[26],count,hold,holder,flag,j;
    	string repeat;
    	cout<<"You will enter six letters, to be sorted alphabetically."<<endl;
    	if(letter[i]=='A' || letter[i]=='a'){
    		list[i]=0;
    	}
    	if(letter[i]=='B' || letter[i]=='b'){
    		list[i]=1;
    	}
    	if(letter[i]=='C' || letter[i]=='c'){
    		list[i]=2;
    	}
    	if(letter[i]=='D' || letter[i]=='d'){
    		list[i]=3;
    	}
    	if(letter[i]=='E' || letter[i]=='e'){
    		list[i]=4;
    	}
    	if(letter[i]=='F' || letter[i]=='f'){
    		list[i]=5;
    	}
    	if(letter[i]=='G' || letter[i]=='g'){
    		list[i]=6;
    	}
    	if(letter[i]=='H' || letter[i]=='h'){
    		list[i]=7;
    	}
    	if(letter[i]=='I' || letter[i]=='i'){
    		list[i]=8;
    	}
    	if(letter[i]=='J' || letter[i]=='j'){
    		list[i]=9;
    	}
    	if(letter[i]=='K' || letter[i]=='k'){
    		list[i]=10;
    	}
    	if(letter[i]=='L' || letter[i]=='l'){
    		list[i]=11;
    	}
    	if(letter[i]=='M' || letter[i]=='m'){
    		list[i]=12;
    	}
    	if(letter[i]=='N' || letter[i]=='n'){
    		list[i]=13;
    	}
    	if(letter[i]=='O' || letter[i]=='o'){
    		list[i]=14;
    	}
    	if(letter[i]=='P' || letter[i]=='p'){
    		list[i]=15;
    	}
    	if(letter[i]=='Q' || letter[i]=='q'){
    		list[i]=16;
    	}
    	if(letter[i]=='R' || letter[i]=='r'){
    		list[i]=17;
    	}
    	if(letter[i]=='S' || letter[i]=='s'){
    		list[i]=18;
    	}
    	if(letter[i]=='T' || letter[i]=='t'){
    		list[i]=19;
    	}
    	if(letter[i]=='U' || letter[i]=='u'){
    		list[i]=20;
    	}
    	if(letter[i]=='V' || letter[i]=='v'){
    		list[i]=21;
    	}
    	if(letter[i]=='W' || letter[i]=='w'){
    		list[i]=22;
    	}
    	if(letter[i]=='X' || letter[i]=='x'){
    		list[i]=23;
    	}
    	if(letter[i]=='Y' || letter[i]=='y'){
    		list[i]=24;
    	}
    	if(letter[i]=='Z' || letter[i]=='z'){
    		list[i]=25;
    	}
    	for(count=0;count<6;count++){
    		hold=0;
    		cout<<"Please enter your letter: ";
    		cin>>letter;
    	}
    	for(j=0;j<i;j++){
    		flag=0;
    		if(list[j]>list[i-1]){
    			hold=j;
    			flag=1;
    		}
    		if(flag=1){
    			holder=list[i-1];
    			list[i-1]=list[hold];
    			list[hold]=holder;
    		}
    	}
    	cout<<"The correct order is:"<<endl;
    	for(h=0;h<6;h++){
    		cout<<list[h]<<" \n";
    	}
    	return(0);
    }
    Please dont recode this for me, or post any answers, just a few suggestions please.

    Thank You

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    there is much that is wrong here. Your big "if" series only will happen once and should be inside the "read a letter" loop. plus, your sort seems to be an attempt at bubble sort. It's incorrect. You need to look that up too.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Here's an example of a bubble sort that I wrote to show one of my friends how they worked... take a look, the 'sorting' is really pretty darn simple
    Code:
    #include <iostream>
    
    void ShowArray(int array[], int size)
    {
        // Show the array
        for (int i = 0; i < size; i++)
            std::cout << array[i] << ' ';
        std::cout << std::endl;
    }    
    
    void BSort(int array[], int size)
    {
        for (int i = 0; i < size-1; i++)
        {
            if (array[i] > array[i+1])
            {
                // Flip
                int temp = array[i+1];
                array[i+1] = array[i];
                array[i] = temp;
                // Reset head
                i = -1;
            }
        }
    }
    
    int main()
    {
        int mynums[] = { 5, 2, 4, 5, 1, 10, 53, 3, 45, 2 };
        int size = sizeof(mynums)/sizeof(int);
        ShowArray(mynums, size);
        BSort(mynums, size);
        ShowArray(mynums, size);
    
        return 0;
    }

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    And with a simple modification, it can sort letters too...
    Code:
    #include <iostream>
    
    void ShowArray(char array[], int size)
    {
        // Show the array
        for (int i = 0; i < size; i++)
            std::cout << array[i] << ' ';
        std::cout << std::endl;
    }    
    
    void BSort(char array[], int size)
    {
        for (int i = 0; i < size-1; i++)
        {
            if (array[i] > array[i+1])
            {
                // Flip
                char temp = array[i+1];
                array[i+1] = array[i];
                array[i] = temp;
                // Reset head
                i = -1;
            }
        }
    }
    
    int main()
    {
        char mychars[] = { 'A', 'D', 'B', 'Z', 'D', 'Q', 'U', 'I', 'A', 'P' };
        int size = sizeof(mychars)/sizeof(char);
        ShowArray(mychars, size);
        BSort(mychars, size);
        ShowArray(mychars, size);
    
        return 0;
    }
    Last edited by BMJ; 09-30-2002 at 12:02 PM.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This seems to work:
    Code:
     
    #include <queue>
    #include <iostream>
    
    int main()
    {
        using namespace std;
        priority_queue<char,vector<char>,greater<char> > chars;
        for (int i=0;i<6;++i)
        {
            cout << "Enter char number " << i << ":";
            char num;
            cin >> num;
            chars.push(num);
        }
        cout << "The characters sorted: ";
        while (chars.size() > 0)
            cout << chars.top() << " ",chars.pop();
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    There are a number of ways to go about this, the STL sort routine never hurts, qsort is a decent C based method, you could insert each item into an STL set and print them (std::set will automatically sort them), or a bunch of other methods. If you want to handcode a sorting routine, here is a resource you might enjoy:
    http://www.subduck.com/pages/tutorials/sorting.php

    -Prelude
    My best code is written with the delete key.

  7. #7
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380

    Re: Sorting inputed letters alphabetically...

    Originally posted by IanelarAzure
    Please dont recode this for me, or post any answers, just a few suggestions please.
    Oops!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an array alphabetically?
    By bluebob in forum C Programming
    Replies: 7
    Last Post: 03-09-2011, 04:53 AM
  2. Arrange letters of the string alphabetically
    By ama_cuber in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 03:40 AM
  3. sorting alphabetically
    By ahming in forum C Programming
    Replies: 8
    Last Post: 05-26-2005, 10:34 AM
  4. Sorting Alphabetically
    By orgy230 in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 02:28 PM