1. ## 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. 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. 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. 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. 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. 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. Originally Posted by Hodor
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. 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'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.

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