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 (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_forwardC Proggraming Found in the Shuffle (Strings) An ordinary deck of playing cards contains 52 cards, each of which has a suit and a value. Most card games require that a deck be shuffled at the beginning of a play. However, sometimes the deck isn't shuffled well enough, and there are arrangements of cards in the deck which may cause a bias in the play. Two such arrangements are a same-suit sequence and an ascending sequence. A same-suit sequence is simply a sequence of consecutive cards in the deck with the same suit. An ascending sequence is a sequence of consecutive cards in the deck that follow one another in increasing value, with Ace following King and preceding two. Thus, 2S, 5S, KS, 3S AS is a same-suit sequence of length five, and 9C, 10D, JC, QS, KH, AC, 2D is an ascending sequence of length seven, and 2H 3H 4H 5H 6H is both a same-suit and ascending sequence of length five. The Problem Given a deck of cards, determine the length of the longest same-suit sequence and the…arrow_forwardExams.cpp) Suppose a teacher weights the fourexams he gives 10%, 25%, 30%, and 35%. Write a programthat reads ten sets of four grades, prints the weightedaverage of each set, and prints the unweighted average ofeach test. The number of students should be in a globalconstant.arrow_forward
- Please dont copy from chegg it is wrong, do your own work JAVA Project 18.20 (Maze Traversal Using Recursive Backtracking) The grid of #s and dots (.) in Fig. 18.23 is a two-dimensional array representation of a maze. The #s represent the walls of the maze, and the dots represent locations in the possible paths through the maze. A move can be made only to a location in the array that contains a dot. Write the recursive method (mazeTraversal) to walk through mazes like the one in Fig. 18.23. The method should receive as arguments a 12-by-12 character array representing the maze and the current location in the maze (the first time this method is called, the current location should be the entry point of the maze). As mazeTraversal attempts to locate the exit, it should place the character x in each square in the path. There's a simple algorithm for walking through a maze that guarantees finding the exit (assuming there's an exit). If there's no exit, you'll arrive at the starting location…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_forwardProgram 4 - CCC '07 J1 In the story Goldilocks and the Three Bears, each bear had a bowl of porridge to eat while sitting at his/her favourite chair. What the story didn't tell us is that Goldilocks moved the bowls around on the table, so the bowls were not at the right seats anymore. The bowls can be sorted by weight with the lightest bowl being the Baby Bear's bowl, the medium bowl being the Mama Bear's bowl and the heaviest bowl being the Papa Bear's bowl. Write a program that reads in three weights and prints out the weight of Mama Bear's bowl. You may assume all weights are positive integers less than 100. Sample Input 10 8arrow_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_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_forwardRevise 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_forward
- jkvhj,nmarrow_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_forward1. [LO 1, LO 2, LO 3 & LO 4, Treasure Hunter Description You are in front of a cave that has treasures. The cave can be represented in a grid which has rows numbered from 1 to , and columns numbered from 1 to . For this problem, define (x,y ) as a tile that is in the the-x row and the y-row. There is a character in each tile, which indicates the type of that tile. Tiles can be floors, walls, or treasures which are represented respectively by the characters '.' (period), '#' (hashmark), and '*' (asterisk). You can go through the floor tiles and treasures, but you can't get past the wall tiles.Initially, you are in a tile (??, Sy). You want to visit all the treasure squares, and retrieve the treasure. When you visit a treasure tile, the treasure is instantly retrieved, and the tile turns into a floor.In a move, if you are on a tile (?,y), then you can move to the right tile above (?-1,y), right (?,y + 1), down (? + 1, ), and left (?, 1) of the current tile. The tile you visit must…arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr