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.
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
Can someone provide detailed comments on what this code is doing for each line? I posted...
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...
Can someone help me fix my C code. I am getting segmentation fault along with missing termination " /* These are the included libraries. */ #include #include #include #include void printTriangle(char *str); int main() { char sentence[81]; int done = 0; while(done != 1) { printf("Please enter the sentence or quit: "); fgets(sentence, 80, stdin); if(strcmp(sentence, "quit") == 0) done = 1; else printTriangle(sentence); } } void printTriangle(char *str) { int index = 0; int line = 1; int i;...
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 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 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(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 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...
C++ Programming I have finished the code but there's one error which shows that "strcpy" might be unsafe. Consider using strcpy_s instead. I've tried that but it's not working probably because I didn't implement it correctly. I am posting my code below. It'd be really helpful if you could fix all the strcpy errors and post it below. Thank you. Text.cpp: #include <iostream> #include <iomanip> #include <cassert> #include <cstring> #include "Text.h" Text::Text ( const char *charSeq ) { bufferSize...
C Language! Please, comment what each line of code does in the code below and answer this question: (sentence is fine) 1. Aside from pressing ctrl-(d/z) at the beginning of a line causing the getnchar() function to return NULL, is there any way to enter less than 9 characters? Code: void exer1(void) { char input[LEN]; char *check; getchar(); // Clearing character buffer. printf("Please enter 9 characters: "); // Prompting the user to enter // a specific input. ...
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...