computer programming · representing characters •character •for purposes of this course, a...
TRANSCRIPT
IIT Bombay
Computer ProgrammingDr. Deepak B Phatak
Dr. Supratik ChakrabortyDepartment of Computer Science and Engineering
IIT Bombay
Session: Representing Characters, Strings, Booleans
1Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
IIT Bombay
• Architecture of a simple computer
• Representing integers and floating point numbers
2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
Quick Recap of Relevant Topics
IIT Bombay
• A computer’s internal representation of• Characters
• Strings
• Booleans
• C++ declarations of above data types
• Putting things together: Our first simple C++ program
3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
Overview of This Lecture
IIT Bombay
Recap from Earlier Lecture
• Snapshot:
• How do we represent characters/strings using 0’s and 1’s?Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 4
00001111 00011010+
11110111
CPU
Address
DataMai
n M
em
ory
01101101
…
BUS
000010110000100101101111
0111111111101100
11011100
10011110
10011111
10011111 1001010110010111
11011100
IIT Bombay
Representing Characters
• Character• For purposes of this course, a character is a byte
• 28 characters:
00000000 (decimal 0) through 11111111 (decimal 255)
• Each character (A, 0, p, …) is an unsigned 8-bit integer
ASCII encoding: ‘A‘ = 65, ‘B’ = 66, ‘0’ = 48, ‘. ’ = 46, ‘ ‘ = 32
• Characters can be compared and sorted like integers
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 5
Blank space character
IIT Bombay
Characters in C++
• char data type
• unsigned char is an unsigned 8-bit integer
• C++ declaration: char userInput, unsigned char numChild
• Constant characters• Can be specified as unsigned integers in 0 through 255
const char userInput = 89; const char defaultOutput = 0x4e;
• Can be specified as character symbol within ‘ ‘
const char userInput = ‘Y’; const char defaultOutput = ‘N’;
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6
IIT Bombay
Representing Strings
• A string is a sequence (array) of characters terminated by a special end-of-string character
• String “Give input: ” is
• Individual characters in string can be accessed
• string as a whole can also be used• Recall Dumbo’s program: Output “Give input: “;• C++ program: cout << “Give input:”;
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7
G i v e i n p u t : \0
Special end-of-string character
00000000 or ‘\0’
IIT Bombay
Strings in C++
• Can be declared as an array of characters
char myString[12];
Can we store “Give input: ” (12 characters) in myString ?
NO! We need space for ‘\0’ at the end
To store a 12 character string, the array size must be 13 or more
• Can be declared as string data type (preferred choice)
string myOtherString(“Give input: ”);
Not just an array of characters, has several attributes we’ll use later
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8
Name of array Size of the array
IIT Bombay
Constant Strings in C++
• Sequence of characters enclosed in “ “
“Give input: “, “The output is: “, “Hello world!!!”
• const string myMessage(“Hello world!!!”)• Value is “Hello world!!!”
• Cannot be changed during program execution
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9
IIT Bombay
Representing Booleans
• Like integers where only 0 or non-0 values are relevant• 0 means false, non-0 means true
• 37 means true, 103 also means true !!!
• Earlier versions of C++ used int to store booleans
• A separate datatype bool exists in C++ (preferred choice)• Uses a form of int internally to store 0 and non-0 values
• C++ declaration: bool flag;
• Boolean constants in C++: true, false• const bool trueValue = true;
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10
IIT Bombay
Putting It All Together
• C++ program: • Read two numbers from keyboard, add and display on console
int main() {int A, B, C;cout << “Give two numbers: “;cin >> A >> B;C = A + B;cout << “Sum is: “ << C;return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11
Dumbo’s program:Use locations A, B, C;Output “Give two numbers: “;Input A; Input B;C = A + B;Output “Sum is: “; Output C;Report job as done;
IIT Bombay
Summary
• Representation of characters and strings• Declaration in C++
• Representation of booleans• Declaration in C++
• From Dumbo’s program to a C++ program
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12