table look-up using one dimensional arrays lesson xx

17
Table Look-Up Using One Dimensional Arrays Lesson xx

Upload: claudia-gay

Post on 02-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

Table Look-Up Using One Dimensional Arrays Lesson xx. Objective. Write a program that uses a one dimension to do a table look-up Learn about parallel arrays. Program Description. Given the following table of point values for each letter of the alphabet - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

Table Look-Up Using One Dimensional Arrays Lesson xx

In this presentation, well talk about using a one-dimensional array to do a table lookup.1Objective Write a program that uses a one dimension to do a table look-upLearn about parallel arrays

We are going write a program that uses a one dimensional array to do a table look-up and learn about parallel arrays.

2Program DescriptionGiven the following table of point values for each letter of the alphabet

^ 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 z0, 2, 3, 5, 7, 6, 5, 4,5, 7, 8, 5, 4, 5, 3, 5, 6, 5, 9, 1, 2, 3, 2, 4, 7 ,8 ,5

Read in a sentence

Determine the point value of the sentence by adding together the value of each letter.

Here is the complete program description. Given the following table of point values for each letter of the alphabet as shown : Read in a sentence. Determine the point value of the sentence by adding together the value of each letter.

3ExampleTable^ 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 z0, 2, 3, 5, 7, 6, 5, 4,5, 7, 8, 5, 4, 5, 3, 5, 6, 5, 9, 1, 2, 3, 2, 4, 7, 8 ,5

Sentenceh i j o e5 7 8 5 6Point value of sentence = 5 + 7 + 8 + 5 + 6 = 31

Here is an example of how we solve this problem manually. Shown in the 1st box are the point values of each letter. In the 2nd box, we have a sample sentence, hi joe. At the bottom of the second box we have the point value for each letter of the sentence. h is worth 5 points, i is worth 7 points, a space is worth 0 points and etc. In the 3rd box, we determine the point value of the sentence by adding all the point values together.4Program Listing Part 1#include using std::cin; using std::cout; using std::endl; #include int main() { char alphabet[28] = {" abcdefghijklmnopqrstuvwxyz"}; // point-values assigned to each letter in alphabet int letVal[28] = {0, 2, 3, 5, 7, 6, 5, 4, 5, 7, 8, 5, 4, 5, 3, 5, 6, 5, 9, 1, 2, 3, 2, 4, 7 ,8 ,5}; int acc = 0; char s[80]; // buffer to hold user input cout