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,

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Wednesday, 10/23/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/23/02  QUESTIONS??  Today:  Discussion of HW #3  The const modifier for functions.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS 1400 Pick ups from chapters 2 and 3. #include directive This pre-processing directive causes the textual contents of a named file to be inserted into.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Multiple-Subscripted Array
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS161 Introduction to Computer Science Character Data.
CS150 Introduction to Computer Science 1
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Input & Output: Console
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.
First steps Jordi Cortadella Department of Computer Science.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Data Types Storage Size Domain of all possible values Operations 1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Constants, Data Types and Variables
Enum ,Char Functions& Math Library Functions I.Mona Alshehri
C++ Data Types Simple Structured Address Integral Floating
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
אבני היסוד של תוכנית ב- C++
אבני היסוד של תוכנית ב- C++
CS150 Introduction to Computer Science 1
הרצאה 03 אבני היסוד של תוכנית ב- C
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
do/while Selection Structure
What Actions Do We Have Part 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Presentation transcript:

13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  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?

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?

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

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’…

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

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;

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

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;

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.

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 ’ ); }

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

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

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); }

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); }

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); }