Lab 7 strings as arrays of chars. #include #include #include using namespace std; int printMenu( ); int hexSymbolValue( char c ); int readBinValue( bool.

Slides:



Advertisements
Similar presentations
Strings.
Advertisements

AU/MITM/1.6 By Mohammed A. Saleh 1. Introducing the string Class  Instead of using a character array to hold a string, you can use a type string variable.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
Overview creating your own functions calling your own functions.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS Nov 2006 C-strings.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Input & Output: Console
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Functions Why we use functions C library functions Creating our own functions.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Sahar Mosleh California State University San MarcosPage 1 Character String.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Strings, Slide Fundamental Programming Strings.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Section 3 Review Mr. Crone.
לולאות קרן כליף.
C Stuff CS 2308.
Compound Assignment Operators in C++
Using Free Functions Chapter 3 Computing Fundamentals with C++
Starting Out with C++: From Control Structures through Objects
If Statements.
Engineering Problem Solving with C++, Etter
Object-Oriented Programming (OOP) Lecture No. 33
Arithmetic Operations
Fundamental Programming
(Dreaded) Quiz 2 Next Monday.
Presentation transcript:

Lab 7 strings as arrays of chars

#include #include #include using namespace std; int printMenu( ); int hexSymbolValue( char c ); int readBinValue( bool vect[ ] ); int readHexValue( char vect[ ] ); int binaryToDecimal( bool vect[ ], int size ); int hexidecimalToDecimal( char vect[ ], int size ); const int maxInputLength = 255; int main() { ……… Let’s concentrate on these first

functions int printMenu( ); - takes no parameters (nothing in the parenthesis) - returns an int to the caller int hexSymbolValue( char c ); - takes a char parameter - returns an int to the caller - clearly, this converts a hex char(0-9 and A-F) to an integer (0-15)

int printMenu() { cout > str; if (strcmp(str,"0")== 0) { return 0; } else if (strcmp(str,"1")== 0) { return 1; } else if (strcmp(str,"2")== 0) { return 2; } else { cout << "Invalid input." << endl; } } strcmp, for char arrays then, let’s focus on how strcmp works first, this is a new way to define a string

arrays of chars? string Str = “Hello”; or char *Str = {“Hello”}; or char Str[5] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’}; // this one offers the most utility // because it allows Str[ x ], strcmp, strlen // and cin !

strcmp can only compare strings, even if they are one character long returns a zero if strings match e.g. strcmp( str, “2” ) compares the contents of str with “2” and is == 0 if they match if ( strcmp(str,“2") == 0 ) { return 2; // returns an int 2 back to main }

int hexSymbolValue( char c ) { switch ( c ) { case '0' : return 0; case '1' : return 1; case '2' : return 2; case '3' : return 3; case '4' : return 4; case '5' : return 5; case '6' : return 6; case '7' : return 7; case '8' : return 8; case '9' : return 9; case 'A' : return 10; case 'B' : return 11; case 'C' : return 12; case 'D' : return 13; case 'E' : return 14; case 'F' : return 15; } // end switch } // end function function…. receives char, returns int

can be used like this: say you have a char…. char myChar = ‘A’; cout << hexSymbolValue( myChar ); will print 10

main int main() { while (true) { int choice = printMenu(); switch (choice) { case 1 : …etc…

calc decimal from binary case 1 : { bool myArray[ maxInputLength ]; // arbitrarily large int myArraySize; myArraySize = readBinValue( myArray ); int result = binaryToDecimal( myArray, myArraySize ); cout << "result = " << result << endl; break; } first, get an array of bools from the user then, calculate the decimal equivalent

myArraySize = readBinValue( myArray ); my Array is an array of bools, which is empty before the call, but filled with ones and zeros after myArray before: [ ] myArray after:[1, 0, 1, 0] myArraySize is thus 4. within the function readBinaryValue, vect[ ] is used to hold myArray.

int readBinValue( bool vect[ ] ) { int actualSize; while(true) { cout > str; for (int i=0; i<strlen(str); i++) { if (str[i]=='0') vect[i] = false; else if (str[i]=='1') vect[i] = true; else { cout << "Invalid input." << endl; ok = false; continue; } } // end for if (!ok) continue; actualSize = strlen(str); break; } // end while (true) return actualSize; } this is supplied for you, but look how it works …

returns size, but also in main: myArraySize = readBinValue( myArray ); in the function int readBinValue( bool vect[ ] ) BOTH myArraySize and myArray are CHANGED BY THE FUNCTION!

calc decimal from binary case 1 : { bool myArray[ maxInputLength ]; // arbitrarily large int myArraySize; myArraySize = readBinValue( myArray ); int result = 0; // define and init result result = binaryToDecimal( myArray, myArraySize ); cout << "result = " << result << endl; break; } first, get an array of bools from the user then, calculate the decimal equivalent

result = binaryToDecimal( myArray, myArraySize ); think of it as: result = binaryToDecimal( [ 1, 0, 1, 0 ], 4 ); within the function binaryToDecimal, vect[ ] is used to hold myArray. therefore, result = vect[0] * vect[1] * vect[2] * vect[3] * 2 0

int binaryToDecimal( bool vect[ ], int size ) { int result = 0; for (int x=0; x<size; x++) // x = 0, 1, 2, 3 { result = result + ( vect[x] * (int)pow(2,size-1-x) ); } // pow(2 to the 3, 2, 1, 0 ) return result; }

result = result + ( vect[x] * (int)pow(2,size-1-x) ); since pow returns a double, it must be cast to an int (int) takes the double from pow, and truncates it to an integer for (int x=0; x<size; x++) // x = 0, 1, 2, 3 { result = result + ( vect[x] * (int)pow(2,size-1-x) ); } // pow(2 to the 3, 2, 1, 0 )

calc decimal from hex case 2 : { char myArray[maxInputLength]; int myArraySize; myArraySize = readHexValue( myArray ); int result = 0; // define and init result int result = hexidecimalToDecimal(myArray, myArraySize ); cout << "result = " << result << endl; break; } first, get an array of CHARs from the user then, calculate the decimal equivalent

int readHexValue(char vect[ ] ) { int actualSize; while (true) { cout > str; bool ok = true; for (int i=0; i ='0') && (str[i] ='A')&&(str[i]<='F'))) { vect[ I ] = str[ I ]; } else { cout << "Invalid input." << endl; ok = false; break; } } if (!ok) continue; actualSize = strlen(str); break; } return actualSize; } this is supplied for you, but look how it works …

myArraySize = readHexValue( myArray ); my Array is an array of chars, which is empty before the call, but filled with characters after myArray before: [ ] myArray after:[‘A’, ‘E’, ‘F’, ‘6’] myArraySize is thus 4. within the function readHexValue, vect[ ] is used to hold myArray.

int hexidecimalToDecimal( char vect[], int size ) { int result = 0; for (int x=0; x<size; x++) { result = result + ( hexSymbolValue( vect[x] ) * (int)pow(16, size-1-x) ); } return result; } needs ints not chars