Presentation is loading. Please wait.

Presentation is loading. Please wait.

13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29, 2003.  Tutor will be in the lab on Tuesday evening,

Similar presentations


Presentation on theme: "13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29, 2003.  Tutor will be in the lab on Tuesday evening,"— Presentation transcript:

1 13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29, 2003.  Tutor will be in the lab on Tuesday evening, from 7 – 9 pm.  Last time, we talked about using files.  What are the steps needed to read/write data from/to files?

2 23/15/2016CS150 Introduction to Computer Science 1 Example  Write a program to read five integer numbers from a file, and calculate the sum.  Whenever you pass an ifstream or ofstream variable to a function, you must pass it by reference (i.e. &).  Why?

3 33/15/2016CS150 Introduction to Computer Science 1 Ch. 7 Data Types  We’ve seen:  ints: typically 32 bits, can be 16  floats: typically 32 bits  chars: typically 8 bits  New:  short: typically 16 bits  long: typically 64 bits  double: typically 64 bits  System dependent  Look at climit and cfloat on p. 350

4 43/15/2016CS150 Introduction to Computer Science 1 Characters  Characters stored in one byte  Represented by ASCII value  American Standard Code for Information Interchange  Characters are stored as an 8 bit int  Relationships  ‘0’ < ‘1’ < ‘2’ < ‘3’ < ‘4’ < ‘5’ < ‘6’ < ‘7’ < ‘8’ < ‘9’  ‘a’ < ‘b’ < ‘c’ < ‘d’ < ‘e’ < ‘f’ < ‘g’ < ‘h’ < ‘i’ <…  ‘A’ < ‘B’ < ‘C’ < ‘D’ < ‘E’ < ‘F’ < ‘G’ < ‘H’ < ‘I’ < ‘J’ < ‘K’…

5 53/15/2016CS150 Introduction to Computer Science 1 ASCII Values  0 – 256  Some ASCII values correspond to invisible characters (sounds)  The ones that are of interest to us are: ‘0’ is 48 ‘A’ is 65 ‘a’ is 97

6 63/15/2016CS150 Introduction to Computer Science 1 What is the Output? char ch1, ch2; ch1 = '0'; ch2 = 'C'; cout << setw(4) << ch1 << setw(4) << ch2 << endl; cout << setw(4) << (int) ch1 << setw(4) << (int) ch2 << endl;

7 73/15/2016CS150 Introduction to Computer Science 1 What is the Output? float x,y; int i; y = 12.345; i = (int) y; x = (int) (y * 10) / 10.0; cout << setw(10) << i << setw(10) << x;

8 83/15/2016CS150 Introduction to Computer Science 1 What is the Output?  Segment #1 int chval; for (chval = 50; chval <= 52; chval++) cout << (char) chval << endl;  Segment #2 int i; char ch; ch = 'a'; for (i = (int) ch; i <= (int) ch + 2; i++) cout << (char) i << endl;

9 93/15/2016CS150 Introduction to Computer Science 1 Program  The printable characters have ASCII values in the range of 32 to 126. Write a C++ program segment that will print the printable character and its associated ASCII value 8 values per line.

10 103/15/2016CS150 Introduction to Computer Science 1 Char Operations  Useful operations: islower, toupper, isdigit, islower, isspace, isupper, tolower #include void todigit(char, &int); … void todigit(char ch, &int num) { if (isdigit(ch)) num = int(ch) - int ( ‘ 0 ’ ); }

11 113/15/2016CS150 Introduction to Computer Science 1 Char I/O Operations  Using cin, can we read in all possible chars?  We need some other operations

12 123/15/2016CS150 Introduction to Computer Science 1 Program #include int main() { char ch; int count; count = 0; cin.get(ch); while (!cin.eof()) { count++; cin.get(ch); } cout << "Number of characters = " << count << endl; }

13 133/15/2016CS150 Introduction to Computer Science 1 Program #include int main() { char ch; cin.get(ch); while (!cin.eof()) { cout.put(ch); cin.get(ch); }

14 143/15/2016CS150 Introduction to Computer Science 1 Program #include int main() { const char newline = '\n'; char ch; int count; cin.get(ch); while (!cin.eof()) { while (ch != newline && !cin.eof()) { cout.put(ch); cin.get(ch); } cout.put(newline); cin.get(ch); }

15 153/15/2016CS150 Introduction to Computer Science 1 Program #include int main() { const char newline = '\n'; char ch; int count; cin.get(ch); while (ch != newline && !cin.eof()) { cout.put(ch); cin.get(ch); } cout.put(newline); }


Download ppt "13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29, 2003.  Tutor will be in the lab on Tuesday evening,"

Similar presentations


Ads by Google