Study recommendations for Quiz 5 - Chapter 5

Similar documents
Study recommendations for the Mid-Term Exam - Chapters 1-5

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Increment and the While. Class 15

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 5: Loops and Files

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Add Subtract Multiply Divide

CSCI 1061U Programming Workshop 2. C++ Basics

Computer Programming : C++

Name Section: M/W T/TH Number Definition Matching (8 Points)

Types of files Command line arguments File input and output functions Binary files Random access

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

C/C++ Programming Lecture 7 Name:

Understanding main() function Input/Output Streams

Reading from files File errors Writing to files

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

COP 2000 Note Framework Chapter 5 - Repetition Page 1

Name SECTION: 12:45 2:20. True or False (12 Points)

Why Is Repetition Needed?

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Name Section: M/W T/TH Number Definition Matching (6 Points)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter 1 Introduction to Computers and C++ Programming

In this chapter you will learn:

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

C++ Programming: From Problem Analysis to Program Design, Third Edition

REPETITION CONTROL STRUCTURE LOGO

Operators. Java operators are classified into three categories:

A SHORT COURSE ON C++

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Lecture 5 Files and Streams

4. Structure of a C++ program

Lecture 7 Tao Wang 1

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

download instant at Introduction to C++

Chapter 4 - Notes Control Structures I (Selection)

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

VARIABLES & ASSIGNMENTS

More Programming Constructs -- Introduction

Introduction to C ++

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Computer Programming I - Unit 5 Lecture page 1 of 14

Loops and Files. of do-while loop

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Unit 7. 'while' Loops

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Scientific Computing

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Introduction to Programming

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Chapter 2: Basic Elements of C++

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Engineering Problem Solving with C++, Etter/Ingber

Lab: Supplying Inputs to Programs

1. In C++, reserved words are the same as predefined identifiers. a. True

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 14 Sequential Access Files

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Chapter 2 Basic Elements of C++

Introduction. Lecture 5 Files and Streams FILE * FILE *

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

QUIZ: What value is stored in a after this

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Control Structures of C++ Programming (2)

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

A First Program - Greeting.cpp

Looping. Arizona State University 1

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Program Organization and Comments

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Programming with C++ as a Second Language

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

BITG 1233: Introduction to C++

Simple File I/O.

Transcription:

Study recommendations for Quiz 5 - Chapter 5 Review Chapter 5 content in: your textbook, the Example Pages, and in the Glossary on the website Review the revised Order of Precedence able (including arithmetic, relational and Boolean operators): () Highest / irst postfix ++ postfix -- ^! prefix ++ prefix -- unary + unary - unary & casts * / % + - < > <= >= ==!= && Lowest / Last Remember the ruth ables for Boolean Operators: (Op = Operand, a relational expression or Boolean value) Op.A Op.B Op.A && Op.B Op.A Op.B Op.A Op.B Op.A! ( Op.A ) REPEIION SRUCURES: Structure: We studied two repetition structures (loops); one known as (a.k.a. pretest) and another known as (a.k.a. posttest). he primary difference between them is in where the test that controls the loop is performed. Leading decision loops test before each pass and are coded in C++ using while (condition) body_statements; railing decision loops test after each pass and are coded in C++ using do body_statements; while (condition); We can design a loop that test somewhere in the middle, but that is consider very poor programming practice. Control: We studied two methods of controlling loop passage and exit. Counting control is based on the testing of values that have been set and altered by the programmer. Programmers know in advance how many times any loop will pass (execute its body) because all values of its control variable are predictable. Sentinel control is based on the testing of external values that have been read into the program. Programmers do not know in advance how many times a loop will pass (execute its body) because the values of its control variable are unpredictable. Boundary Values: he values of variables just prior to the entry into a loop and just after the exit from a loop are called boundary values. Steps inside a loop can be organized so that the value of a variable is changed after it is displayed, so don't expect that variables will always contain the last value that you saw displayed. Rev. 2012-10-26 Page 1 of 5

Counting Loops: All loops have at least four parts: initialization - prior to the loop entry, a first value of the control variable is set body - where the step(s) to be repeated belong increment (or decrement) - where the control variable is increased (or decreased) test - where the control variable is tested to determine whether the loop should pass or exit. he bottom three steps are not always in the order listed above. he order of the steps will effect the boundary values of the control variable. he two examples below show loops that will count from 1 to 9 and display the value of the control variable (also called a counter in a loop). Notice the exit value on N will be 10. N1 N=1; N1 N=1; while (N<=9) do N<=9 cout <<N<<endl; cout <<N<<endl; N = N+1; NN+1 N = N+1; NN+1 N <= 9 while (N<=9); If a loop: (1) uses the Leading Decision structure, (2) uses control, and (3) increments as the last step of its pass, C++ offers a special "automatic loop" statement combining the statements above (on the left) into the single statement: for (N=1; N<=9; N=N+1) cout << N << endl; Note: if the body of the loop has more than one statement to repeat, block it inside of braces. SAMPLE QUESIONS: (AND ANSWERS) Circle any SYNAX (not run-time) ERRORS (there may be more than one) in each of the following C++ code segments. If a segment is valid answer "OK". Assume all variables have been correctly declared. for (I=1, I<=79, I++) cout << "-"; the commas (,) should be semi-colons (;) X=1; while X < 5 cout << X++; missing parentheses around condition Rev. 2012-10-26 Page 2 of 5

Use the following for Statement to answer the next 3 questions (A-C). for (Count=Start; Count<=inish; Count++) cout << Count; (A) Which variable is the "control variable"? Count (B) What does Count++ mean? _Increment Count by 1 after a pass is complete (C) If Start contains a value of -4 and inish contains the value 0, how many times will the loop execute? Answer: 5 lowchart a program with a loop that accumulates whole numbers entered by the user until a -1 is entered and then identifies and displays the sum. hen write the C++ code to the right of it. Start ot 0 /* accumulate.cpp */ #include <iostream> using namespace std; N is not -1 Sum is: ot Number (-1 to stop)? N is not -1 ot ot + N End int main () int N, ot=0; do cout << "Number (-1 to stop)? "); cin >> N; if (N!= -1) ot = ot + N; while (N!= -1); cout << "Sum is: " << ot << endl; return 0; or each question below, circle any and all loop control methods for which the claims are true: (A) his control method is based on values assigned by the programmer, not given by the user. (B) his control method is based on values entered by the user. (C) he number of passes that the loop will perform can always be predetermined when the loop starts. Rev. 2012-10-26 Page 3 of 5

or each question below, circle any and all loop structures for which the claims are true: (A) he test for each repetition is performed after the pass through the loop body. (B) he body will always be executed at least once. (C) his structure is appropriate for controlled loops that use a "prime read". (D) Sentinel controlled loops using this structure need an extra test to guard the body from the value. DISK ILE INPU AND OUPU: Review the last part of Chapter 5 in your textbook that discuss reading and writing of sequential text files on disks. hen study the practice exercise on the following page. It involves a file with a simple name that is located in the same folder as the program. Be aware that special consideration must be given when providing filenames or paths that contain whitespaces or characters that C++ interprets as having special meaning, such as backwards slashes. or example, to open a file named "My ile.txt" entered at the keyboard, you would need to input the string using the getline() function to prevent the whitespace from truncating the portion of the filename starting at the whitespace before assigning it to a string variable, as in: getline (cin, StringVar); // read a string containing whitespaces o associate a file accessible from the disk location "C:\User\JStudent\Data.txt" with a file input stream object named "inile", you would have to escape the backwards slashes (\) as follows: inile.open("c:\\user\\jstudent\\data.txt "); Examine the following C++ source code: /************************************************************ * Program: diskio.cpp - Practice Exercise * * Opens a file in the same folder as the program based on a * * user supplied name (without whitespaces). hen writes ten * * lines of text, closes the file, then reopens the file and * * reads and displays each of the ten lines from the file. * ************************************************************/ Rev. 2012-10-26 Page 4 of 5

#include <iostream> #include <fstream> using namespace std; int main () string ILENAME; // String object to hold a filename system ("cls"); /* Clear the Screen */ cout << "Disk I/O Program\n\n"; cout << "What is the filename (w/o path)? "; cin >> ILENAME; ofstream fileout; fileout.open(ilename.c_str()); // open requires a C-String if (fileout) // Verify successful open cout << ILENAME << " opened successfully for writing.\n\n"; for (int C=1; C<=10; C++) fileout << "Line " << C << endl; fileout.close(); else cout << ILENAME << "\a failed to open.\n"; string OSR; ifstream filein; filein.open(ilename.c_str()); if (filein) cout << ILENAME << " opened successfully for reading.\ncontents:\n"; for (int C=1; C<=10; C++) getline(filein,osr); // read a line of text with whitespaces cout << OSR << endl; filein.close(); else cout << ILENAME << "\a failed to open.\n"; system ("pause"); return 0; Most common coding errors when writing source code for loops: Closing the loop statement too soon with a misplaced semicolon ailing to block a body with multiple statements inside of braces orgetting the difference between the operators = (assignment) and == (equality) Rev. 2012-10-26 Page 5 of 5