Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.

Similar presentations


Presentation on theme: " Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays."— Presentation transcript:

1

2

3  Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays

4 1.Given 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 z 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 2.Read in a sentence 2.Determine the point value of the sentence by adding together the value of each letter.

5 Table ^ 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 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 Sentence h i j o e 5 7 8 5 6 Point value of sentence = 5 + 7 + 8 + 5 + 6 = 31

6 #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 << "enter sentence" << endl; cin.getline(s, 80); int length = strlen(s);

7 int j, k; for (j = 0; j < length; j++) { for (k = 0; k < 27; k++) { if (s[j] == alphabet [k]) { acc += letVal[k]; break; } } } cout << "the point value of the sentence = " << acc << endl; return 0; }

8 #include using std::cin; using std::cout; using std::endl; #include int main() {

9 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}; ‘ ’ ‘a’ ‘b’ ‘c’ ‘d’ alphabet[0] alphabet[1] alphabet[2] alphabet[3] alphabet [4]... myName[27] ‘z’ ‘\0’ alphabet{26] 0 2 3 5 7 letVal [0] letVal[1] letVal [2] letVal[3] letVal [4]... letVal [27] 5 0 letVal [26]

10 int acc = 0; char s[80]; // buffer to hold user input

11 cout << "enter sentence" << endl; cin.getline(s, 80); //don’t use: cin << s; int length = strlen(s); s[0] s[1] s[2] s[3] s[4] s[5] s[7] ‘ t’ ‘h’ ‘e’ ‘ ’ ‘c ‘ ‘a’ ‘t’ ‘\0’ ‘b’ s[0] s[79]... length 6

12 s[0] s[1] s[2] s[3] s[4] s[5] s[7] ‘ t’ ‘h’ ‘e’ ‘ ’ ‘c ‘ ‘a’ ‘t’ ‘\0’ s[6] s[79]... ‘ ’ ‘a’ ‘b’ ‘c’ ‘d’ alphabet[0] alphabet[1] alphabet[2] alphabet[3] alphabet [4]... alphabet[27] ‘z’ ‘\0’ alphabet{26] 0 2 3 5 7 letVal [0] letVal[1] letVal [2] letVal[3] letVal [4]... letVal [27] 5 0 letVal [26] j= 0 k=0

13 s[0] s[1] s[2] s[3] s[4] s[5] s[7] ‘ t’ ‘h’ ‘e’ ‘ ’ ‘c ‘ ‘a’ ‘t’ ‘\0’ s[6] s[79]... ‘ ’ ‘a’ ‘b’ ‘c’... alphabet[0] alphabet[1] alphabet[2] alphabet[3] ‘t’ alphabet[27]... ‘\0’ alphabet[20] 0 2 3 5... letVal [0] letVal[1] letVal [2] letVal[3] 2 letVal [27]... 0 letVal [20] j= 0 k=20

14 s[0] s[1] s[2] s[3] s[4] s[5] s[7] ‘ t’ ‘h’ ‘e’ ‘ ’ ‘c ‘ ‘a’ ‘t’ ‘\0’ s[6] s[79]... ‘ ’ ‘a’ ‘b’ ‘c’... alphabet[0] alphabet[1] alphabet[2] alphabet[3] ‘t’ alphabet[27]... ‘\0’ alphabet[20] 0 2 3 5... letVal [0] letVal[1] letVal [2] letVal[3].2 letVal [27]... 0 letVal [20] j= 1 k=0

15 j loop keeps track of which letter you are processing in the sentence k keeps track of which letter of the alphabet you are on

16 int j, k; for (j = 0; j < length; j++) { for (k = 0; k < 27; k++) { if (s[j] == alphabet [k]) { acc += letVal[k]; break; } } }

17 cout << "the point value of the sentence = " << acc << endl; return 0; }

18  Wrote a program that uses a one dimension to do a table look-up  Learned about parallel arrays


Download ppt " Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays."

Similar presentations


Ads by Google