Question

In: Computer Science

using c++. ALWAYS GRADE MY ANSWERS nstructions Write a program that prompts the user to input...

using c++. ALWAYS GRADE MY ANSWERS

nstructions

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.

For example, it should output the individual digits of:

  • 3456 as 3 4 5 6
  • 8030 as 8 0 3 0
  • 2345526 as 2 3 4 5 5 2 6
  • 4000 as 4 0 0 0
  • -2345 as 2 3 4 5

#2

Instructions

Write a program that prompts the user to input a sequence of characters and outputs the number of vowels.
(Use the function isVowel written in Programming Exercise 2.)

Your output should look like the following:

There are # vowels in this sentence.

... where # is the number of vowels.

#3

Instructions

You are given a file consisting of students’ names in the following form: lastName, firstName middleName. (Note that a student may not have a middle name.)

Write a program that converts each name to the following form: firstName middleName lastName. Your program must read each student’s entire name in a variable and must consist of a function that takes as input a string, consists of a student’s name, and returns the string consisting of the altered name. Use the string function find to find the index of ,; the function length to find the length of the string; and the function substr to extract the firstName, middleName, and lastName

here is the file:

Miller, Jason Brian
Blair, Lisa Maria
Gupta, Anil Kumar
Arora, Sumit Sahil
Saleh, Rhonda Beth
Spilner, Brody

Solutions

Expert Solution

Screenshot of question1:

The Output Obtained:

The Code Used:

#include<iostream>
using namespace std;
int main()
{
   int k = 0;
   int number;
   int arr[20]; //this array will store the individual digits of the integer.
   int sum = 0;
   cout<<"Enter an integer: "; //asking the user to enter a number.
   cin>>number;
   if(number<0) //if the number is negative we turn it positive by multipling -1.
   {
       number*=-1;
   }
   while(number>0) //we extract each digit from the last and store it in the array.
   {
       arr[k++] = number%10;
       number/=10;
   }
   for(int i=k-1;i>=0;i--) //this array is printing from k-1 to 0 cause the array contains the digits in reverse order.
   {
       cout<<arr[i]<<" "; //printing the digits.
       sum+=arr[i]; //calculating the sum of the sums.
   }
   cout<<endl;
   cout<<"The sum of the digits are: "<<sum<<endl; //printing the sum.
   return 0;
}

The Screenshot Of Question2:

The Output Obtained:

The Code Used:

#include<iostream>
using namespace std;
int isVowel(char ch) //checking if the character is vowel or not and returning 1 or 0 respectively.
{
   if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
   {
       return 1;
   }
   return 0;
}
int main()
{
   int vowelCount = 0;
   string str;
   cout<<"Enter a string: "; //taking the string a input from the user.
   getline(cin,str);
   for(int i=0;i<str.size();i++) //iterating through the string and counting the numer of vowels.
   {
       if(isVowel(str[i]))
       {
           vowelCount++;
       }
   }
   cout<<"There are "<<vowelCount<<" vowels in this sentence."<<endl; //printing the vowelCount.
   return 0;
}

The Screenshot of Question3:

The Ouput Obtained:

The Code Used:

#include <iostream>
#include <fstream>
#include<string>
using namespace std;
string studentName(string str)
{
   int pos = str.find(","); //we find the position of , and store it in pos variable.
   string lastName = str.substr(0,pos); //next we take the slice of the string before , and store it in lastName string.
   //The line below will take the string slice after the ", " till the end therefore it will
   //contain the first name and the middle name.
   string firstMiddleStr = str.substr(pos+2,str.size()-(pos+2));
   return firstMiddleStr+" "+lastName; //we return the string.
}
int main()
{
   string filename;
   cout<<"Enter the file name: "; //asking the filename from the user.
   cin>>filename;  
   ifstream in(filename); //opening the file.
   if(!in) { //if and error occurs while opening the file.
   cout << "Cannot open input file.\n";
   return 1;
   }
   string line;//this will store the lines in the text file.
   while(getline(in,line)) { //Here we read one line at a time and call the student Name function and print the string.
   cout<<studentName(line)<<endl;
   }
   in.close();
   return 0;
}

I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.


Related Solutions

using c++. ALWAYS GRADE MY ANSWERS Write a program that reads students’ names followed by their...
using c++. ALWAYS GRADE MY ANSWERS Write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a program in c++ that prompts the user to input a coin collection of number...
Write a program in c++ that prompts the user to input a coin collection of number of quarters, dimes, nickels and pennies. The program should then convert the coin collection into currency value as dollars. The coin values should all be whole numbers and the resulting currency value should be displayed with two decimals. An example of user interaction is as follows: Coin Convertor Enter number of quarters: 3 Enter number of dimes: 1 Enter number of nickels: 4 Enter...
In C program #include<stdio.h> Write a program that prompts the user to input the elapsed time...
In C program #include<stdio.h> Write a program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. Example (Numbers with underscore indicate an input): Enter the elapsed time in seconds: 9630 The elapsed time in seconds = 9630 The equivalent time in hours:minutes:seconds = 02:40:30 HINT: Pay attention to the printf format descriptors.
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
Write a C++ program that prompts the user (or “Player 1”) to input an integer value...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value between 1 and 3 (where 1=paper, 2=scissor, and 3=rock). This input should be passed into a string function called player_RPS(), and returns a string value indicating the selection of paper, scissors, or rock (as mentioned above). Next, the returned string value, along with a generated input from the computer, should be passed into a void function called RPS_comparison(), and determines whether the user’s input...
Write a C program that repeatedly prompts the user for input at a simple prompt (see...
Write a C program that repeatedly prompts the user for input at a simple prompt (see the sample output below for details). Your program should parse the input and provide output that describes the input specified. To best explain, here's some sample output: ibrahim@ibrahim-latech:~$ ./prog1 $ ls -a -l -h Line read: ls -a -l -h Token(s): ls -a -l -h 4 token(s) read $ ls -alh Line read: ls -alh Token(s): ls -a -l -h 2 token(s) read $...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Exercise 4 – Lists and input Using a function Write a program that prompts the user...
Exercise 4 – Lists and input Using a function Write a program that prompts the user to input a sequence of words. The program then displays a list of unique words (words that only occurred once, i.e. no repeated).
Write a program(C#) that prompts the user for her home planet. Based on the user's input...
Write a program(C#) that prompts the user for her home planet. Based on the user's input the program will display the following: Input: earth Message: earth. You are an Earthling and you have 10 fingers Input: VENUS Message: VENUS. You are a Venusian and you have 12 fingers Input: Mars Message: Mars. You are a Martian and you have 8 fingers any other input Message: I am sorry I don't know of that planet You may use either the ToUpper()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT