Write a program that uses a one dimension to do a table look-up Learn about parallel arrays
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.
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 Point value of sentence = = 31
#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);
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; }
#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}; ‘ ’ ‘a’ ‘b’ ‘c’ ‘d’ alphabet[0] alphabet[1] alphabet[2] alphabet[3] alphabet [4]... myName[27] ‘z’ ‘\0’ alphabet{26] letVal [0] letVal[1] letVal [2] letVal[3] letVal [4]... letVal [27] 5 0 letVal [26]
int acc = 0; char s[80]; // buffer to hold user input
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
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] letVal [0] letVal[1] letVal [2] letVal[3] letVal [4]... letVal [27] 5 0 letVal [26] j= 0 k=0
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] letVal [0] letVal[1] letVal [2] letVal[3] 2 letVal [27]... 0 letVal [20] j= 0 k=20
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] letVal [0] letVal[1] letVal [2] letVal[3].2 letVal [27]... 0 letVal [20] j= 1 k=0
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
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; }
Wrote a program that uses a one dimension to do a table look-up Learned about parallel arrays