Question

Can someone provide detailed comments on what this code is doing for each line? I posted...

Can someone provide detailed comments on what this code is doing for each line? I posted the assignment below. I am having trouble learning and getting assistance because I keep getting answers provided w/ good information on how the problem was solved. I have spent too much on tutoring this month and I am relying heavily on assistance here. Can you please assist with helping me understand each line of code that was written here?

Also- is there an easier way this could have been solved. My concern is that the assistance I am getting might be providing advanced methods that I haven't used yet. I am very new to programming.

Thank you.

#include <iostream>

using namespace std;

char firstLetter(char[],char);

int main()

{

char str[10], y{};

cout<<"\nThe first letter in alphabetical order is\n"<<

firstLetter(str,y)<<endl;

cout << "And ASCII Value is " << int(str[0]);

}

char firstLetter(char str[], char y)

{

int i,j;

for(i=0; i<10; i++)

{

cout<<"Please enter a lowercase letter: ";

cin>>str[i];

for (int i = 0; i <10; i++)

{

for (int j = i+1; j <10; j++)

{

if (str[i] > str[j]) //comparing the ascii value of characters

{

//sorting in alphabetical order

char temp = str[i];

str[i] = str[j];

str[j] = temp;

}

}

}

return str[0];

}

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. (Hint: you can order letters using ASCII code, for example, a < c.)               

Output should display the appropriate letter, as well as the associated ASCII code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program and its description is given using comments and sorting is done using for loop

#include <iostream>
using namespace std;
//Function prototype
char firstLetter(char[],char);

int main()
{
//in str array that can store upto 10 elements of char type
char str[10], y{};
// call firstLetter function with two arguments, first is char array and second is char y and it return first letter in alphabetical //order after sorting
char x=firstLetter(str,y);
// print the statement using cout and print first letter in alphabetical order returned by firstLetter() function in char x
cout<<"\nThe first letter in alphabetical order is\n"<<x<<endl;
// it print ascii value of first letter in alphabetical order
cout << "ASCII Value is " << int(x);
return 0;
}
char firstLetter(char str[], char y)
{
//declare two integer variables i and j
int i,j;
//in order to enter 10 lowercase letters by user, for loop is created that will execute 10 times
for(i=0; i<10; i++)
{
cout<<"Please enter a lowercase letter: ";
//cin is input statement used to take value from the users just like scanf in c
cin>>str[i];
}
//in order to sort letters in alphabetical order use two for loops
//first loop start with 0 that will take first letter and compare it with all other letters, if first letter ascii value is greater than //second one than swap it out, otherwise keep it as it is
//continue this first for loop until i reaches 10
for (int i = 0; i <10; i++)
{
//this loop start from i+1 in order to compare str[i] letter with all other letters and continue until it reaches 10
for (int j = i+1; j <10; j++)
{
if (str[i] > str[j])//comparing the ascii value of characters if str[i] is grater than str[j] than swap otherwise keep as it is
{
//sorting in alphabetical order
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
return str[0];

}

Output:

Please enter a lowercase letter: f
Please enter a lowercase letter: g
Please enter a lowercase letter: h
Please enter a lowercase letter: j
Please enter a lowercase letter: k
Please enter a lowercase letter: o
Please enter a lowercase letter: l
Please enter a lowercase letter: b
Please enter a lowercase letter: c
Please enter a lowercase letter: a

The first letter in alphabetical order is
a
ASCII Value is 97

Add a comment
Know the answer?
Add Answer to:
Can someone provide detailed comments on what this code is doing for each line? I posted...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I was asked to write a program that when divisible by 2- reverses the order of...

    I was asked to write a program that when divisible by 2- reverses the order of the letters in a sentence when divisible by 3- changes all lowercase letter into uppercase letters in a sentence when divisible by 5 skips the vowels in a sentence this is what I have so far. my reverseMode works fine. #include <iostream> #include <string> #include<cstring> using namespace std; void reverseMode(char* str); void upperMode(char* str); void goodbyeVowels(char* str); char str[50], rstr[50]; //initializing my character arrray...

  • I just started working on some code for a password creator. I was doing some testing and I keep g...

    I just started working on some code for a password creator. I was doing some testing and I keep getting this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47) at java.base/java.lang.String.charAt(String.java:693) at PasswordCreater.check(PasswordCreater.java:56) at Password.main(Password.java:23 In my code I am having the user create a password that is at least 6 characters long, but no more than 16. Also, include at least one lowercase letter, one uppercase letter, one number digit and one special...

  • c++ Please give a line by line explanation fo the design of this code (what is...

    c++ Please give a line by line explanation fo the design of this code (what is happening per line to make this program work) int ways(int amt,int* l,int n,int* res){ if (amt < 0 || (amt > 0 && n <= 0)) return 0; if (amt == 0){ string s = ""; int j = 0; if (res[0] > 0){ j = 1; cout << res[0] << " quarter/s, "; } if (res[1] > 0) { j = 1; cout...

  • C++ Can someone please help me with this problem- commenting each line of code so I...

    C++ Can someone please help me with this problem- commenting each line of code so I can understand how to solve this problem using the C++ programming language? I really need help understanding how to create a file for the program to read. Do I create the file in Visual basic or create a text file? I have the code, just need to know how to create the file for it to read. #include<fstream> #include<iostream> using namespace std; int main()...

  • Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption                     void SubEncrypt(cha...

    Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption                     void SubEncrypt(char *message, char *encryptKey) { int iteration = 0; printf("Enter Aphabet Encryption Key: \n"); scanf("%s", encryptKey);    for (iteration = 0; iteration < strlen(message); iteration++) { char letter = message[iteration]; if (letter >= 'A' && letter <= 'Z') {    letter = encryptKey[letter - 'A']; } message[iteration] = letter; } printf("CipherText message: %s\n", message); } //_________________________________________________________________________________________________________________________________________________...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • I don't know how to terminate the code in the second time that whether or not...

    I don't know how to terminate the code in the second time that whether or not the users want to continue to get a new number. PLEASE HELP. To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed...

  • The following lines of code all have problems. Identify what is wrong with each line of...

    The following lines of code all have problems. Identify what is wrong with each line of code The following lines of code all have problems. Identify what is wrong with each line of code. a) for(j=0; j<= 10; j++) cout << prices[j]; b) int array = {1,2,3,4}; c) int arr[3]; for (arr = 0; arr < = 10; arr++) d) char k; for (k=0; k<= 10; k++)

  • C++ language I am having some trouble with user validation, can someone look at my code...

    C++ language I am having some trouble with user validation, can someone look at my code and tell me what I am doing wrong: char firstInital, lastInitial;    int userAge;    std::cout << "Program 1-2: Get user initials and age in days\n ";    std::cout << "-------------------------------------------------------------------------\n";    std::cout << "Please enter the first letter of your first name: \n " << flush;    std::cin >> firstInital;    std::cout << "Please enter the first letter of your last name: \n...

  • Can someone help with this C++ code. I am trying to compile and I keep running...

    Can someone help with this C++ code. I am trying to compile and I keep running into these 4 errors. #include <iostream> #include <cassert> #include <string> using namespace std; typedef int fsm_state; typedef char fsm_input; bool is_final_state(fsm_state state) { return (state == 3) ? true : false; } fsm_state get_start_state(void) { return 0; } fsm_state move(fsm_state state, fsm_input input) { // our alphabet includes only 'a' and 'b' if (input != 'a' && input != 'b') assert(0); switch (state) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT