170 likes | 275 Views
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
E N D
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 ^ 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 Read in a sentence Determine the point value of the sentence by adding together the value of each letter.
Example 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
Program Listing Part 1 #include <iostream> using std::cin; using std::cout; using std::endl; #include <string> int main() { char alphabet[28] = {" abcdefghijklmnopqrstuvwxyz"}; // point-values assigned to each letter in alphabet intletVal[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);
Program Listing Part 2 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; }
Preprocesor Directives #include <iostream> using std::cin; using std::cout; using std::endl; #include <string> int main() {
Setting up the Table char alphabet[28] = {" abcdefghijklmnopqrstuvwxyz"}; // point-values assigned to each letter in alphabet intletVal[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}; ‘ ’ 0 letVal [0] alphabet[0] ‘a’ 2 letVal[1] alphabet[1] 3 ‘b’ letVal [2] alphabet[2] 5 ‘c’ letVal[3] alphabet[3] 5 ‘z’ letVal [26] alphabet{26] ‘\0’ 0 alphabet [4] letVal [4] 7 ‘d’ . . . . . . myName[27] letVal [27]
Set Up Accumulator & Array for Input int acc = 0; char s[80]; // buffer to hold user input
Read in Sentence ‘ t’ s[0] cout << "enter sentence" << endl; cin.getline(s, 80); //don’t use: cin << s;intlength= strlen(s); ‘h’ s[1] ‘e’ s[2] ‘ ’ s[3] s[0] s[4] ‘c ‘ ‘a’ s[5] ‘\0 ’ ‘t’ ‘\0’ ‘\0’ s[7] s[7] ‘b’ s[79] . . . 6 length . . .
Algorithm Part 1 ‘ t’ s[0] j= 0 k=0 ‘h’ s[1] ‘e’ s[2] ‘ ’ s[3] s[4] ‘c ‘ ‘a’ s[5] 0 ‘ ’ letVal [0] alphabet[0] ‘t’ s[6] ‘a’ 2 letVal[1] alphabet[1] ‘\0’ s[7] 3 ‘b’ letVal [2] alphabet[2] ‘\0 ’ . . . 5 ‘c’ alphabet[3] letVal[3] ‘z’ 5 letVal [26] alphabet{26] ‘\0’ . . . 0 ‘\0’ letVal [4] alphabet [4] ‘d’ 7 ‘\0’ s[79] . . . . . . letVal [27] alphabet[27]
Algorithm Part 2 ‘ t’ s[0] j= 0 ‘h’ s[1] ‘e’ s[2] ‘ ’ s[3] s[4] ‘c ‘ ‘a’ k=20 s[5] 0 ‘ ’ alphabet[0] letVal [0] ‘t’ s[6] 2 ‘a’ letVal[1] alphabet[1] ‘\0’ s[7] ‘b’ 3 letVal [2] alphabet[2] alphabet[20] letVal [20] ‘\0 ’ . . . 5 ‘c’ alphabet[3] letVal[3] . . . . . . ‘\0’ . . . 0 ‘\0’ . . . . . . ‘\0’ s[79] ‘t’ 2 letVal [27] alphabet[27]
Algorithm Part 3 ‘ t’ s[0] k=0 j= 1 ‘h’ s[1] ‘e’ s[2] ‘ ’ s[3] s[4] ‘c ‘ ‘a’ s[5] 0 ‘ ’ letVal [0] alphabet[0] ‘t’ s[6] 2 ‘a’ alphabet[1] letVal[1] ‘\0’ s[7] ‘b’ 3 letVal [2] alphabet[2] letVal[20] alphabet[20] ‘\0 ’ . . . 5 ‘c’ alphabet[3] letVal[3] . . . . . . ‘\0’ . . . 0 ‘\0’ . . . . . . ‘\0’ s[79] ‘t’ .2 alphabet[27] letVal [27]
Loop Set Up 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
Code to Do Table Look Up int j, k; for (j = 0; j < length; j++) { for (k = 0; k < 27; k++) { if (s[j] == alphabet [k]) { acc += letVal[k]; break; } } }
Output cout << "the point value of the sentence = " << acc << endl; return 0; }
Summary • Wrote a program that uses a one dimension to do a table look-up • Learned about parallel arrays