Thread: Alphabetical Order Project

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    19

    Alphabetical Order Project

    I'm working on a program that accepts ten lowercase letters and then displays the one that would come first in alphabetical order. I'm using an array right now and I'm a little stuck on the logic for deciding the output. Here's what I have so far:

    Code:
    #include <iostream>
    using namespace std;
    #define SIZE 10
    
    
    char firstLetter(char[],char);
    
    
    int main()
    {
        char letters[SIZE],y;
        
        cout<<"\nThe first letter in alphabetical order is\n"<<
        firstLetter(letters,y)<<endl;
    }
    
    
    char firstLetter(char letters[], char y)
    {
        int i;
        y=letters[0];
        for(i=0; i<SIZE; i++)
        {
            cout<<"Please enter a lowercase letter: ";
            cin>>letters[i];
            if (letters[i]<letters[SIZE-i])
            {
                y=letters[i];
            }
        }
        
        return y;
    }

  2. #2
    Guest
    Guest
    In terms of logic, you should separate the asking from the checking.

    1. Ask the user to enter 10 letters
    2. Determine and print the "first" letter

    So your firstLetter function should only handle the latter.

    As it's currently designed, there is no point in having the second (y) argument. You're immediately overwriting it inside the function, so you might as well have declared it there and then.

    Btw, is this for school/learning the basics, or personal interest (best practices) in C++? Recommendations might vary considerably based on that.

    For the sake of readability, I recommend the use of spaces around operators:
    Code:
    int n = 0;
    cout << "Some message" << endl;
    for (int i = 0; i < 10; ++i) { ... } // note: you should declare loop variables in the for statement

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    I'm doing it slightly differently than I would normally do it because it's an assignment:

    "You have 10 lowercase letters that you would like to arrange in alphabetical order. Create a program to determine the first letter in alphabetical order out of the 10 letters entered. (Please have your program enter the 10 letters one at a time.) This should be done in a function using this prototype:

    char firstletter (char x, char y);

    Make sure you use a for loop expression inside your function."

  4. #4
    Guest
    Guest
    Ah yes, that's good to know.

    Do you have an idea what the inputs x and y are supposed to represent? It's not obvious to me.

    Note that x is not an array type in the instructions -- I doubt you can take as much liberty.

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    An array seemed the most efficient method to use in this situation, and my professor has a fair amount of leniency for the method as long as the output is spot on. I'm just not sure what kind of expression I could use that would ensure each letter gets compared to the previous one entered in the array. Since a for loop is supposed to be used, I thought the more efficient way would be to put the request for another letter in the same loop as the checking.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I don't know your professor but surely if he has given you the function prototype, and told you to use it, his leniency wouldn't go as far as changing said prototype. Or would it?

  7. #7
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    Quote Originally Posted by Hodor View Post
    I don't know your professor but surely if he has given you the function prototype, and told you to use it, his leniency wouldn't go as far as changing said prototype. Or would it?
    Last time I was given a prototype in the form (x,y) I used an array and got a good grade, so I'm not too worried about that. For line 26 in my code I tried
    Code:
    if (letters[i] < letters[i-1])
    {
        y = letters[i];
    }
    but it didn't work. I'm not sure what kind of comparison can be made there, or if I'm on the wrong track altogether.

  8. #8
    Guest
    Guest
    I'll give you a hint: If you ask for each letter within the same loop (as in your original code), then you don't need to use an array at all, and your function needn't take any arguments.

    Imagine ten of your classmates each said a letter, one after another, and you had to memorize the "lowest" letter being said. Would you need to remember all letters (array), or can you change the lowest letter (variable) in your mind while the letters are being spoken?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Guest View Post
    I'll give you a hint: If you ask for each letter within the same loop (as in your original code), then you don't need to use an array at all, and your function needn't take any arguments.

    Imagine ten of your classmates each said a letter, one after another, and you had to memorize the "lowest" letter being said. Would you need to remember all letters (array), or can you change the lowest letter (variable) in your mind while the letters are being spoken?
    I am old enough to need to use an array. It is taking me a few seconds to decide which letter is lower sometimes.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    Quote Originally Posted by Guest View Post
    I'll give you a hint: If you ask for each letter within the same loop (as in your original code), then you don't need to use an array at all, and your function needn't take any arguments.

    Imagine ten of your classmates each said a letter, one after another, and you had to memorize the "lowest" letter being said. Would you need to remember all letters (array), or can you change the lowest letter (variable) in your mind while the letters are being spoken?
    How would I do that without an array though? In a loop, the current value of the variable would overwrite the previous before I could compare them. I could use two variables, one for the smallest entered, and one for the current letter being entered, but I'm not sure how I would implement that in a loop.

  11. #11
    Guest
    Guest
    I could use two variables, one for the smallest entered, and one for the current letter being entered
    Bingo.

    I'm not sure how I would implement that in a loop.
    Create the two variables (before the loop) and write/compare them within the loop. Short of writing it for you, I cannot say more. Think carefully about the initial value of the "smallest entered" variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming alphabetical order
    By sakiarg in forum C Programming
    Replies: 4
    Last Post: 03-24-2014, 06:06 PM
  2. names in alphabetical order?
    By n3cr0_l0rd in forum C Programming
    Replies: 21
    Last Post: 02-06-2009, 08:59 PM
  3. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM
  4. Sorting in Alphabetical order
    By Andre Santiago in forum C Programming
    Replies: 1
    Last Post: 12-13-2002, 06:14 PM
  5. Alphabetical order
    By BubbleMan in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 03:38 PM

Tags for this Thread