1 / 17

Table Look-Up Using One Dimensional Arrays Lesson xx

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

claudia-gay
Download Presentation

Table Look-Up Using One Dimensional Arrays Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Table Look-Up Using One Dimensional Arrays Lesson xx

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

  3. 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.

  4. 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

  5. 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);

  6. 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; }

  7. Preprocesor Directives #include <iostream> using std::cin; using std::cout; using std::endl; #include <string> int main() {

  8. 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]

  9. Set Up Accumulator & Array for Input int acc = 0;   char s[80]; // buffer to hold user input

  10. 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 . . .

  11. 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]

  12. 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]

  13. 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]

  14. 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

  15. 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; }  } }

  16. Output cout << "the point value of the sentence = "     << acc << endl;   return 0; }

  17. Summary • Wrote a program that uses a one dimension to do a table look-up • Learned about parallel arrays

More Related