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

Slides:



Advertisements
Similar presentations
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Arrays.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
 Review structures  Program to demonstrate a structure containing a pointer.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Chapter 02 (Part III) Introduction to C++ Programming.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Chapter 15 Strings as Character Arrays
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
for( 起始條件 ; 判斷式 ; 條件運算 ){ // 迴圈內容 } while( 判斷式 ){ // 迴圈內容 } do{ // 迴圈內容 } while( 判斷式 ) ;
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Example 21 #include<iostream.h> int main() { char Letter = 0;
ㅎㅎ Fifth step for Learning C++ Programming Homework 1 Homework 2
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Popping Items Off a Stack Using a Function Lesson xx
Chapter 1.2 Introduction to C++ Programming
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Two-Dimensional Arrays Lesson xx
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Chapter 2 Elementary Programming
One-Dimensional Array Introduction Lesson xx
Null-Terminated Character Arrays
File I/O with Records Lesson xx
Popping Items Off a Stack Lesson xx
Value returning Functions
Strings A collection of characters taken as a set:
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
String What it is Why it’s useful
ㅎㅎ Fifth step for Learning C++ Programming Pointers Homework solution
Arrays Arrays A few types Structures of related data items
C++ Programming Lecture 20 Strings
ㅎㅎ Fifth step for Learning C++ Programming Homework 1 Homework 2
Presentation transcript:

 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