Question

write a program in C++ Suppose that the alphabets are given the following values: a=1, b=2,...

write a program in C++

Suppose that the alphabets are given the following values: a=1, b=2, c=3, ..., z=26.

Using this rule, we define the value of a word as the sum of the values of its letters. Thus, the value of the word cat is c+a+t = 3+1+20 = 24.

Furthermore, the value of the phrase "a cat" is a+c+a+t=1+3+1+20=25.

The value of a sentence, paragraph, and any larger unit of writing is similarly defined.

When we compute such values, we ignore any non-alphabetic characters. We also ignore upper and lower case differences; thus, “A Cat?” has the same value as “a caT.”

#include

using namespace std;

int main() {

// define the variables int c=3, a=1, t=20;

// compute the value int value = a+c+a+t;

// print out the result cout << value << endl;

return 0;

}

This project involves a passage from John Milton’s Paradise Lost:

Likening his Maker to the grazed ox, Jehovah, who in one night, when he passed From Egypt marching, equalled with one stroke Both her first-born and all her bleating gods. Given the stated rules, this passage has a value.

Let us define modified passage as a passage where all the letters of a person’s first name are taken out from the main passage.

For a student named Jane Doe (first name is Jane), if she starts of with Paradise Lost passage,

her modified passage will be: Likig his Mkr to th grzd ox, hovh, who i o ight, wh h pssd From gypt mrchig, qulld with o strok Both hr first-bor d ll hr bltig gods.

(notice: there are no occurrences of the letters j, a, n, and e above)

For Jane, the value of her modified passage is: 1276 Computing this value manually can be difficult and error-prone — especially, when the number of letters is large.

A better approach is to write a program that can do it for us.

Your program should only print the answer (a number).

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

Thanks for the question, here is the simple C++ program.

=====================================================================

#include<iostream>

#include<string>

#include<cctype>

using namespace std;

int main(){

               

                string passage ="Likig his Mkr to th grzd ox, hovh, who i o ight, wh h pssd From gypt mrchig, qulld with o strok Both hr first-bor d ll hr bltig gods.";

               

                int total =0;

                for(int i= 0; i<passage.length(); i++) {

                               

                                char letter = passage.at(i);

                                letter = tolower(letter);

                               

                                if('a'<=letter && letter<='z')

                                total += 1+ letter - 'a';

                }

               

                cout<<total<<endl; // should print 1276

               

}

=====================================================================

thanks !

Add a comment
Know the answer?
Add Answer to:
write a program in C++ Suppose that the alphabets are given the following values: a=1, b=2,...
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
  • Problems: 1. Write a program to define the following variables, assign them values, and print their...

    Problems: 1. Write a program to define the following variables, assign them values, and print their values on screen: Integer x = 20, Float y = 40.45 Double z = 91.51e+5 Char w = A • • For the integer variable x, you need to print it in decimal notations, octal notations and hexadecimal notation. For the double variable y, you need to print it in double notations and scientific notation. 2. Write a program with the following three parts:...

  • please write in C++ Write a program that uses a structure to store the following inventory...

    please write in C++ Write a program that uses a structure to store the following inventory data in a file: The data can be either read from a text file or the keyboard Item name (string) Quantity on hand(int) Wholesale cost(double) Retail Cost(double) The program should have a menu that allows the user to perform the following tasks: Add new records to the file Display any record in the file User will provide the name of the item or the...

  • Write a C language program that will prompt for 12 different resistor values, entered one at...

    Write a C language program that will prompt for 12 different resistor values, entered one at a time via the keyboard, expressed in Ohms. Valid values are 0.01 Ohm up to 10,000,000,000 Ohms. Write each entry to a text file on the disk thus: Resistor 1 Ohmic value = 2200. Update the resistor number for each of the 12 entries. Do not repeat any resistance value. Write a second C language program to read the 12 entries from the text...

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

  • Need help in C (a) Write a C program to read in a line of text...

    Need help in C (a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a letter is considered the same as the uppercase. To make viewing easy, the frequencies should be presented using a bar chart as follows. You can assume that the input contains only spaces, lowercase letters, uppercase letters and the newline character (i.e. the Enter key). Enter a line of text: Letter ZZz...

  • Write a PIC18Fxx2 assembly program that takes the variable A and save into B in inverted the 110010102, then the program finishes with B 010100112-The 1. bit order. For example, if A variable A s...

    Write a PIC18Fxx2 assembly program that takes the variable A and save into B in inverted the 110010102, then the program finishes with B 010100112-The 1. bit order. For example, if A variable A shall preserve the original value at the end of the program execution. The program shall be implemented using any of the loop and shift techniques discussed in class. 2. Write a PIC18Fxx2 assembly program that makes the calculus of the first eigth (8) terms of the...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

  • 1.Write an assembly language program that corresponds to the following C++ program: #include <iostream> using namespace...

    1.Write an assembly language program that corresponds to the following C++ program: #include <iostream> using namespace std; const int amount = 20000; int num; int sum; int main () { cin >> num; sum = numb + amount; cout << "sum = " ,<< sum << endl; return 0; } 2. Test the program in the previous question twice. The first time, enter a value for num to make the sum within the allowed range for the Pep/8 computer. The...

  • Given below is a partially completed C program, which you will use as a start to...

    Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count - @; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; return count; Task 1: (10 pts) Show the design of the function compareAndCount() by writing a pseudocode. To...

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