Concept explainers
22.41E a) Program Description:
The program declares an array of characters and integer to store the alphabets and corresponding counts initialized to 0;
It takes multi line inputs from the user and stores those into a
22.41E a) Program
/* * 22_41a_charactercount.cpp string handling and total alphabet counts table */ #include<string> #include<iostream> #include<cstring> #include<vector> usingnamespace std; intmain() { char alphaBets[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T','U', 'V', 'W', 'X', 'Y', 'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; int counts[]={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0}; vector<string> input; string str, strInput; //take input from user cout<<"Please enter any text. Enter ZZZ to finish typing:\n\n"; getline(cin, str); while ( str!= "ZZZ") { input.push_back(str); getline(cin, str); } //process the input string for (constauto& em: input){ strInput = em; for(int i=0; i < strInput.length();i++){ char test = strInput.at(i); for(int j=0;j <sizeof(alphaBets); j++){ if( test == alphaBets[j]){ counts[j]+=1; } } } } cout<<"The Alphabets counts in the supplied text are as below :\n\n\n"; for(int n=0;n <sizeof(alphaBets); n++){ cout<<"Count of "<<alphaBets[n]<<" = "<<counts[n]<<endl; } return 0; }//end main
22.41E a) Sample Output
Sample Output 22.41 a)
Please enter any text. Enter ZZZ to finish typing:
This is another test, of a justification program
for 65 chars long, with so many inputs and ! and commas,
Therefore, I have to admit this is just
a very basic program which does nothing but keeps testing
the input provided for length and applies justification logic
to the right & left parts of 65th position.
There can be many possible improvements
to this very very basic program. Thanks !
ZZZ
The Alphabets counts in the supplied text are as below :
Count of A = 0
Count of B = 0
Count of C = 0
Count of D = 0
Count of E = 0
Count of F = 0
Count of G = 0
Count of H = 0
Count of I = 1
Count of J = 0
Count of K = 0
Count of L = 0
Count of M = 0
Count of N = 0
Count of O = 0
Count of P = 0
Count of Q = 0
Count of R = 0
Count of S = 0
Count of T = 3
Count of U = 0
Count of V = 0
Count of W = 0
Count of X = 0
Count of Y = 0
Count of Z = 0
Count of a = 23
Count of b = 5
Count of c = 9
Count of d = 7
Count of e = 26
Count of f = 8
Count of g = 9
Count of h = 18
Count of i = 28
Count of j = 3
Count of k = 2
Count of l = 6
Count of m = 10
Count of n = 19
Count of o = 25
Count of p = 13
Count of q = 0
Count of r = 22
Count of s = 25
Count of t = 31
Count of u = 7
Count of v = 7
Count of w = 2
Count of x = 0
Count of y = 6
Count of z = 0
22.41E b) Program Description
The program stores lines of text input into a vector<string> and then iterates over each element, tokenizing strings into words using strtok and keeps a count of the words in an array which is finally printed out. The max word length assumed is 45 as that's the longest word in English dictionary (pneumonoultramicroscopicsilicovolcanoconiosis ).
22.41E b) Program
/* * 22_41b_wordslengthcpp string handling and total words & their length counts table */ #include<string> #include<iostream> #include<cstring> #include<vector> usingnamespace std; intmain() { //Assume the maximum word length = 45 ( as per modern dictionary //keep counts lengthwise , index 0 for length one char int counts[45]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; vectorinput; string str, strInput; //take input from user cout<<"Please enter any text. Enter ZZZ to finish typing:\n\n"; getline(cin, str); while ( str!= "ZZZ") { input.push_back(str); getline(cin, str); } constchar space[2]= " "; //process the input string for (constauto& em: input){ char * temp = newchar[em.length() + 1]; strcpy(temp, em.c_str()); char * word = strtok(temp, space); while(word != 0){ int wordLen = strlen(word); if( wordLen >0) counts[wordLen-1]+=1; word =strtok(NULL, space); } } cout<<"The Words Length and their counts in the supplied text are as below :\n\n" <<"\tWord Length\t\t\t\t"<<"Total count\n" <<"\t------------\t\t\t\t------------\n"<<endl; for(intn=0;n< 45; n++){ cout<<"\t "<<n+1<<"\t\t\t\t "<<counts[n]<<endl; } return 0; }//end main
22.41E b) Sample Output
pneumonoultramicroscopicsilicovolcanokoniosis is a disease caused by exposure to volcanic silica, also termed as silicosis.
ZZZ
The Words Length and their counts in the supplied text are as below :
Word Length Total count
------------ ------------
1 2
2 4
3 0
4 1
5 0
6 3
7 1
8 2
9 1
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 0
27 0
28 0
29 0
30 0
31 0
32 0
33 0
34 0
35 0
36 0
37 0
38 0
39 0
40 0
41 0
42 0
43 0
44 0
45 1
22.41E c) Program Description
The program will use map
Finally, using the vector, all words will be iterated and their respective counts will be fetched from the map.
Explanation:
22.41E c)Program
/* * 22_41c_wordscount.cpp string handling and unique words & their length counts table */ #include<string> #include<iostream> #include<cstring> #include<vector> #include<map> #include<algorithm>// to convert whole string to uppercase or lowercase using STL. usingnamespace std; intmain() { map
22.41E c) Sample Output
Please enter any text. Enter ZZZ to finish typing:
To be or not To be is an old proverb yet interests many in the modern world
To understand this better a course in modern literature has to be pursued and that too in a deep sense
ZZZ
The unique Words and their counts in the supplied text are as below :
Word Total count -------------------- ------------ to 4 be 3 or 1 not 1 is 1 an 1 old 1 proverb 1 yet 1 interests 1 many 1 in 3 the 1 modern 2 world 1 understand 1 this 1 better 1 a 2 course 1 litratire 1 has 1 pursued 1 and 1 that 1 too 1 deep 1 sense 1
Want to see the full answer?
Check out a sample textbook solutionChapter 22 Solutions
C++ How To Program Plus Mylab Programming With Pearson Etext -- Access Card Package (10th Edition)
- (Conversion) a. Write a C++ program to convert meters to feet. The program should request the starting meter value, the number of conversions to be made, and the increment between metric values. The display should have appropriate headings and list the meters and the corresponding feet value. If the number of iterations is greater than 10, have your program substitute a default increment of 10. Use the relationship that 1 meter = 3.281 feet. b. Run the program written in Exercise 6a on a computer. Verify that your program begins at the correct starting meter value and contains the exact number of conversions specified in your input data. c. Modify the program written in Exercise 6a to request the starting meter value, the ending meter value, and the increment. Instead of the condition checking for a fixed count, the condition checks for the ending meter value. If the number of iterations is greater than 20, have your program substitute a default increment of (ending value - starting value) / 19.arrow_forward(2) ¿Ehä To draw a rectangle with side length of 100 pixel on your graphics screen, use the function square (0,0,100,100); O rectangle (100,100,50,100); rectangle(150,50,250,150); square (100,100,100); O (2) ¿Ehi The statement that copies the value of variable x to the memory cell pointed to by the integer pointer ptr * is ptr = x; O * ptr = &x; O ptr = &x; * ptr = x; Oarrow_forwardPython Programming - (Alpha Tele Num Translator) Assignment Details Introduction: Assignment requires function creation and using string methods. Instructions: The instructions below list the assignment requirements. Many companies use telephone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion:A, B and C = 2D, E and F = 3G, H and I = 4J, K and L = 5M, N and O = 6P, Q, R and S = 7T, U and V = 8W, X, Y and Z = 9Write a program that asks the user to enter a 10-character number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if user enters 555-GET-FOOD, the application should display 555-438-3663. After displaying the translated telephone number, ask the user if they’d like to enter another number. If the user enters…arrow_forward
- Experiment 17 Directories I. Objective After performing this activity, you should be able to 1. Perform operations on directories in assembly language. II. Introduction This experiment covers a number of extended DOS services for disk operations introduced by DOS 2.0 and 3.0. Many of these services are much simpler than the original DOS services are generally recommended. These operations involve the use of an ASCIIZ string to initially identify a drive, path and filename; a file handle; and special error return codes. Int21H Service 39H: Creating a directory Function 39H creates a subdirectory, just like the DOS command MD For example, diname, dh 'C:\TASM',0 mov ah, 39h lea dx dirname int 21h A successful operation creates the subdirectory TASM in drive C. An error sets the carry flag and returns code 03 (path not found or invalid path) or 05 (access denied or directory already exists) in register AX. INTIH Service 3AH: Removing a directory Function 3AH deletes a subdirectory. just…arrow_forwardProblem B Musical Key ConversionThe chromatic scale is a 12-note scale in music in which all notes are evenly spaced: that is, the ratio of the frequency between any two consecutive notes is constant. The notes are typically labeled in the following sequence: A, A#, B, C, C#, D, D#, E, F, F#, G, G# After G#, the labels loop back and start over with A (one octave higher). To convert between musical keys, you can shift all notes in a piece of music a constant number of steps along the scale above. For example, the sequence of notes E, E, F, G, G, F, E, D, C, C, D, E, E, D, D can be converted to another musical key by shifting everything up three steps: E, E, F, G, G, F, E, D, C, C, D, E, E, D, D G, G, G#, A#, A#, G#, G, F, D#, D#, F, G, G, F, F Notice that G was converted to A#, since going three steps up required us to loop off of the top of the scale back to the bottom: G -> G# -> A -> A#. Technically we should note that this would be A# of the next octave up, but we’ll…arrow_forward( Please note that 1000 to 1500 words are mandatory or the answer will be rejected)Please include an introduction and a body and a conclusion for the below question, kindly note that it is mandatory to have a word count of 1000 to 1500 words at least and that the answer should be plagiarism free, please. Question 1- Discuss the use of artificial intelligence, machine learning, and deep learning, as well as their importance in the digital economy. (Use examples and include academic references)arrow_forward
- Revise Exercise 6.28 (Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows:Roll two dice. Each die has six faces representing values 1, 2, ..., and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win.Your program acts as a single player.)to run it 10,000 times and display the number of winning games.arrow_forward9:48 Expert Q&A Done Robot Navigation Self-navigating robots are very useful in the real world. Reducing the risk of human error by having a computer do tasks can increase the safety of transportation: Airplanes have systems that automate takeoffs and landings. Some cars can parallel park themselves. Cargo ships have autopilots. Mazes offer a challenge that can be directly translated to these situations. In this task, you are developing navigation software for a company that makes robotic systems that can direct themselves. As a first-level test of the robot you are developing, you will have it navigate a maze. North West East South Part D When the robot makes a turn, it would be useful to have an operation to perform on d to represent this turn. This is because after making a turn, the new value of d will depend on the old value of d. Complete the table for the new values of dif the robot is turning left or right. Then determine an expression in terms of d that will give the new…arrow_forward2:51 53 B/s Add class comment ASSIGNMENT No. 1: Write a program to calculate the GPA in First semester. 1- The program asks the marks out of 100 in each subject and calculates GPA. 2- It also calculates the percentage marks in the first semester. 3- Example for calculation is given in the assignment Procedure: One by one course code, course title, credit hours will be displayed to user (according to the given display format) and user will keep entering the obtained marks (Only integer marks are allowed, can't be fractional numbers) into that relevant course. On the basis of marks entered, grade wilIl be calculated for each course one by one. Grade and Weight calculation: Marks >= 80, A grade, weight is 4.0 Marks >= 75 and Marks = 70 and Marks = 65 and Marks = 60 and Marks = 55 and Marks = 50 and Marks <= 54, C grade, Weight is 1.0 Marks <= 49, F grade, weight is 0 Grade Point of a course = Credit Hours of that course * obtained Weight of that course Semester GPA = Sum of Credit Hours…arrow_forward
- Exercise 2 - Learning to Trace (to Fix Semantic Errors) This exercise should be done in the "Exercise2" file providea to you. A major part of being able to trace your program to find out what's wrong is first figuring out what the program should be doing, and then looking at the output that the program currently produces to see what it is actually doing. In this way, you can identify what's not right, or, specifically, which part of the code is not doing the right thing and make the necessary adjustments. We want a program which, when run, should be providing the following output: Required Output: Welcome user. This program demonstrates the importance of fixing semantic errors. In addition, it helps you calculate your final mark and the test mark needed to pass with a certain final mark. The prac mark is 75% which is weighted at 0.4 The tut mark is 67% which is weighted at 0.35 The test mark is 68% which is weighted at 0.25 The target final mark is 75% With the current marks, you're…arrow_forwardII) Let A={ab,bc,ba}.In each part ,Identify whether the string belongs to A*. (i)ababab (ii)abc (iii)abba (iv)abbcbaba (v)bcabbab (vi)abbbcbaarrow_forwardSection: Algorithm 7. s] A transport consultant has created a file of information about trips collected from individuals who have participated in a survey. This information includes start_time and end_time variables. One of the items contained in the file is the duration of each trip (that is, end_time – start_time) in minutes. The consultant is trying to work out how to calculate the number of trips with duration greater than 60 minutes (that is, how many trips take longer than an hour) and requires your assistance to develop an algorithm. You can assume that the durations are read one at a time and should only be read until EOF (end of file) is reached. i. ] *Develop a flowchart to show your algorithm, using the diagrammatic conventions described in the lecture. s] What is the likely complexity of your program using big-O notation? ** 11. Clearly point out what the primary parameters are and define your terms.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr