Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable

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

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

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.

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

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

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

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

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

Definition Matching (10 Points)

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

I/O Streams and Standard I/O Devices (cont d.)

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

Why Is Repetition Needed?

CS242 COMPUTER PROGRAMMING

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

Week 3: File I/O and Formatting 3.7 Formatting Output

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

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

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Name Section: M/W or T/TH. True or False (14 Points)

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

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Do not start the test until instructed to do so!

Lab Instructor : Jean Lai

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

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

CS2141 Software Development using C/C++ C++ Basics

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data

Lecture 3 The character, string data Types Files

What we will learn about this week:

Summary of basic C++-commands

C++ Final Exam 2017/2018

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Review Summer CSC1322 Adv Programming in C++ Professor: Zhang

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

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


Chapter 12. Streams and File I/O

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

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

A First Program - Greeting.cpp

Introduction to Programming EC-105. Lecture 2

Iterative Constructs

A SHORT COURSE ON C++

CMSC 202 Midterm Exam 1 Fall 2015

What will happen if the user presses <enter> after every input? Will all work like below? Enter the values of x, y, z: Result: The sum is 17.

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

Fundamentals of Programming CS-110. Lecture 2

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

True or False (14 Points)

Study Guide for Test 2

WARM UP LESSONS BARE BASICS

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

Programming. C++ Basics

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

C++ Input/Output Chapter 4 Topics

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Introduction to C ++

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Spring 2002 Page 1 of 8 READ THIS NOW!

BITG 1233: Introduction to C++

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Chapter 3 - Notes Input/Output

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Name. CPTR246 Spring '17 (100 total points) Exam 2

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

Programming Language. Functions. Eng. Anis Nazer First Semester

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Getting started with C++ (Part 2)

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE Instructor: Final Exam Fall 2014

CSCE Practice Midterm. Data Types

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 1 INTRODUCTION

Subprograms. Introduction to Programming (in C++) Subprograms: procedures and functions. Subprograms. Subprograms. Functions are defined as follows:

Do not turn to the next page until the start of the exam.

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

The C++ Language. Arizona State University 1

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

File I/O Christian Schumacher, Info1 D-MAVT 2013

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

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

CS 7A Final Exam Solutions - Spring Final Exam 5/24/16

BITG 1113: Array (Part 1) LECTURE 8

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

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

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Lab # 02. Basic Elements of C++ _ Part1

True or False (12 Points)

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Transcription:

EXERCISE ANSWERS Chapter 7 Exam Preparation Exercises 1 Function call The mechanism that transfers control to the body of a function Argument list A mechanism by which functions communicate with each other; the list of variables and/or expressions following the name of a function that determines how the function and the calling code communicate with each other Parameterless function A function that has no parameters; a function whose parameter list is empty Parameter A variable declared in a function heading Argument A variable or expression listed in a call to a function Local variable A variable declared within a block and not accessible outside of that block 2 void Test( int, int, int ); // Function prototype int main() // Function heading int a; // Local variable int b; // Local variable int c; // Local variable Function Function Test(a, c, b); // Function call definition body ^ ^ ^ Arguments Test(b, a, c); // Function call ^ ^ ^ Arguments Function void Test( int d, // Parameter heading int e, // Parameter int f ) // Parameter Function int g; // Local variable definition Function body int h; // Local variable 3 First Call to Test Second Call to Test Parameter Argument Parameter Argument 1 d a 1 d b 2 e c 2 e a 3 f b 3 f c 5 True 6 True 9 widgets and clunkers refer to the same memory location, so after the assignment statement both widgets and clunkers have the value 77 12 In function Test, the variables equal 5 20 In the main function after the first call, the variables equal 5 14 In function Test, the variables equal 5 20 In the main function after the second call, the variables equal 15 5

13 1 cout << "Exercise "; 2 DoThis(number1, number2); 5 cout << number1 << << number2 << endl; 3 cin >> value3 >> value1; 4 value2 = value1 + 10; 14 In the main function just before execution of the return statement, number1 contains 15, number2 contains 25, and value3 does not exist (it is local to the DoThis function) Programming Warm up Exercises 1 void PrintMax( /* in */ int number1, /* in */ int number2 ) 3 void GetMeanOf( /* in */ int howmany, /* out */ float& avg ) // howmany > 0 // avg == average device of howmany values read from standard input int loopcount; // Loop control variable float sum; // Sum of the input values float inputvalue; // One input value sum = 00; loopcount = 1; while (loopcount <= howmany) cin >> inputvalue; sum = sum + inputvalue; loopcount++; avg = sum / float(howmany); 4 void Halve( /* inout */ int& firstnumber, /* inout */ int& secondnumber ) firstnumber = firstnumber / 2; secondnumber = secondnumber / 2; 6 a Halve(alpha, beta); where alpha and beta are int variables b The function call is not valid Because Halve uses reference parameters, the arguments must be variable names 2

9 #include <iostream> using namespace std; void Rotate( int&, int&, int&, int& ); int main() int int1; int int2; int int3; int int4; // First input value // Second input value // Third input value // Fourth input value cout << "Enter four values: "; cin >> int1 >> int2 >> int3 >> int4; cout << "Before: " << int1 << << int2 << << int3 << << int4 << endl; Rotate(int1, int2, int3, int4); cout << "After: " << int1 << << int2 << << int3 << << int4 << endl; return 0; //****************************************************************** void Rotate( /* inout */ int& firstvalue, /* inout */ int& secondvalue, /* inout */ int& thirdvalue, /* inout */ int& fourthvalue ) // All parameters are assigned // firstvalue == secondvalue@entry // && secondvalue == thirdvalue@entry // && thirdvalue == fourthvalue@entry // && fourthvalue == firstvalue@entry int temp; // Temporary holding variable // Save value of first parameter temp = firstvalue; // Shift values of next three parameters firstvalue = secondvalue; secondvalue = thirdvalue; thirdvalue = fourthvalue; // Replace value of final parameter with saved value fourthvalue = temp; 10 In the following code, the expression ch >= A && ch <= Z works properly using the ASCII character set but not EBCDIC Chapter 8 discusses a solution to this difficulty void CountUpper( /* out */ int& upcount ) // Reading marker for standard input stream is at beginning of // a line terminated by \n // upcount == number of uppercase letters in the input line

char ch; // An input character upcount = 0; while (ch!= \n ) if (ch >= A && ch <= Z ) upcount++; 11 void AddTime( /* inout */ int& hours, /* inout */ int& minutes, /* in */ int elapsedtime ) // hours >= 0 && 0 <= minutes <= 59 // && elapsedtime >= 0 // hours and minutes represent the time obtained by adding // elapsedtime to the starting time (hours@entry and minutes@entry) minutes = minutes + elapsedtime; hours = hours + minutes / 60; minutes = minutes % 60; 12 void GetNonBlank( /* out */ char& ch ) // At least one nonblank character exists in the // remaining input stream // Any blank input characters have been skipped // && ch == first nonblank character while (ch == ) 13 void SkipToBlank( /* out */ char& ch ) // At least one blank exists in the remaining input stream // Any nonblank input characters have been skipped // && ch == first blank character while (ch!= ) 14 void SkipToBlank( /* out */ char& ch, /* out */ int& skipped ) // At least one blank exists in the remaining input stream // Any nonblank input characters have been skipped // && ch == first blank character 4

// && skipped == number of nonblank characters skipped Chapter 7 Functions skipped = 0; while (ch!= ) skipped++; Case Study Follow Up 1 void CreateBar( /* in */ float deptsales, // Total sales for dept /* out */ string& bar ) // Bar of asterisks // 00 <= deptsales <= 250000 // Parameter "bar" contains one * for each $500 in sales, // with remainders over $250 rounded up // && No stars have been stored for sales <= $250 bar = ""; // Start with empty string while (deptsales > 2500) bar = bar + * ; // Store * for each $500 deptsales = deptsales 5000; 4 To check for sales greater than $25,000, we need to modify the PrintData function Change the precondition that 00 <= deptsales <= 250000 to the following: 00 <= deptsales Next, add the following to the postcondition: // && IF deptsales@entry > 250000 // Stars have been printed only to the $25,000 mark, // followed by a! To change the function, we declare a local variable named over25000 of type bool The revised function body is: bool over25000; // True if deptsales > 250000 over25000 = false; if (deptsales > 2500000) over25000 = true; deptsales = 250000; cout << setw(12) << "Dept " << deptid << endl; cout << setw(3) << storenum << " "; while (deptsales > 2500) cout << * ; deptsales = deptsales 5000; if (over25000) cout <<! ; cout << endl; 5 //******************************************************************

// This program checks the two data files for the Graph program // The errors that it looks for are: // 1 Different department IDs // 2 Different numbers of departments //****************************************************************** #include <iostream> #include <fstream> #include <string> using namespace std; int main() int deptid1; // Department ID for Store 1 int deptid2; // Department ID for Store 2 int numdays1; // Number of days for Store 1 int numdays2; // Number of days for Store 2 int day; // Loop control variable float sales; // One day s sales bool baddata; // File error flag string filename; // User entered file name ifstream store1; // File for Store 1 ifstream store2; // File for Store 2 cout << "Enter the name of the data file for Store 1: "; cin >> filename; store1open(filenamec_str()); cout << "Enter the name of the data file for Store 2: "; cin >> filename; store2open(filenamec_str()); baddata = false; // Assume data has no errors store1 >> deptid1 >> numdays1; // Priming read store2 >> deptid2 >> numdays2; while (store1 && store2 &&!baddata) if (deptid1!= deptid2) cout << "** Error: Department IDs are different" << endl; baddata = true; else // Department IDs are equivalent Continue // Read in (and ignore) sales for Store 1 day = 1; while (day <= numdays1) store1 >> sales; day++; // Read in (and ignore) sales for Store 2 day = 1; while (day <= numdays2) store2 >> sales; day++; store1 >> deptid1 >> numdays1; store2 >> deptid2 >> numdays2; if (!baddata && (store1 store2)) cout << "** Error: Files have unequal numbers of departments" << endl; return 0; 6