The following is a typical execution run of this program:

Similar documents
Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

PIC 10A. Final Review: Part I

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

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 13) 26 Points Select all correct answers (multiple correct answers are possible)

Getting started with C++ (Part 2)

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

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

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

Lab 15 Review of Arrays, Array of Objects and Vector Dr. John Abraham, Professor

A SHORT COURSE ON C++

Definition Matching (10 Points)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

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

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

ET 181. Purpose: The purpose of this lab is to become familiar with using arrays in C++.

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

BITG 1233: Introduction to C++

Ch 6. Functions. Example: function calls function

Programming Language. Functions. Eng. Anis Nazer First Semester

CPT101- Principles of Programming

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

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

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

Objects and streams and files CS427: Elements of Software Engineering

Chapter 15 - C++ As A "Better C"

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

CSci 1113 Final. Name: Student ID:

PROGRAMMING EXAMPLE: Checking Account Balance

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

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

CS2141 Software Development using C/C++ Stream I/O

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

Summary of basic C++-commands

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

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

A First Program - Greeting.cpp

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

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

Fundamentals of Programming CS-110. Lecture 2

Review Questions II KEY

PIC 10A. Final Review

WARM UP LESSONS BARE BASICS

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

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

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

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Introduction to C ++

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

CS2255 HOMEWORK #1 Fall 2012

PIC 10A. Review for Midterm I

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Input/Output: Streams

Local and Global Variables

EP578 Computing for Physicists

CMPS 221 Sample Final

Introduction to C++ (Extensions to C)

Introduction to Programming EC-105. Lecture 2

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

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

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

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

C Legacy Code Topics. Objectives. In this appendix you ll:

TEST 1 CS 1410 Intro. To Computer Science Name KEY. October 13, 2010 Fall 2010

C++ Quick Guide. Advertisements

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

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!

Lab Instructor : Jean Lai

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

2 nd Week Lecture Notes

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

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:

C++ Programming Lecture 10 File Processing

CMSC 202 Midterm Exam 1 Fall 2015

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

COMP322 - Introduction to C++

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Chapter 3 - Notes Input/Output

Computer Programming : C++

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

C++_ MARKS 40 MIN

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

Problem Solving: Storyboards for User Interaction

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

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

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.

Circle all of the following which would make sense as the function prototype.

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

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

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

CSCE 110 PROGRAMMING FUNDAMENTALS

C++ for Python Programmers

CS 1044 Project 2 Spring 2003

Expressions, Input, Output and Data Type Conversions

Outline. First Quiz Results. Exercise Five Goals. Question Three. Questions One and Two. Exercise five if statements February 28, 2006

Transcription:

et181:endterm test, fall 2010 (200 points) name: Closed notes, open book, two hour test. You may use any graded program of your choice. There are four sections on eight pages. Each section is worth 50 points. Section A (50 points) The following program constructs a spherical Ball object based on the diameter d entered from the keyboard, and then determines its volume, surface area, and major circumference from the following three formulas: volume = π*d 3 /6 area = π*d 2 circum = π*d #include <iostream> using namespace std; float const PI = (355.0/113.0); class Ball{ private: float diam; // diameter of the ball public: Ball(float d0 = 1) { // Constructor diam = d0; cout << "ball diameter = " << diam << endl; void find(float& vol, float& area, float& circum); // evaluate volume, surface area, major circumference ; // testing the class Ball: int main() { float diam, vol, area, circum; cout << "enter ball diameter:\n"; cin >> diam; // In this box write the two statements that // 1. creates a Ball object and // 2. invokes the class method 'find' cout << " volume is " << vol << " cubic units\n"; cout << " surface area is " << area << " sq. units\n"; cout << " major circumference is " << circum << " units\n"; return 0; The following is a typical execution run of this program: enter ball diameter:10 ball diameter = 10 volume is 523.599 cubic units surface area is 314.159 sq. units major circumference is 31.4159 units 1

In the following box complete the definition of the method find according to the following formulas: volume = π*d 3 /6 area = π*d 2 circum = π*d Notice from the method prototype in the class Ball that since the method has to assign values to three arguments, they are passed by reference. The function should be complete, including commented pre- and post- conditions 2

Section B (50 points) A leap year is a year that is evenly divisible by 4, other than a century year (such as 1900). However a century year that is evenly divisible by 400 (such as 2000) is also a leap year. Complete the following program by writing the boolean method leap of the class Date in the box below. Method leap returns the value true if the private variable year is a leap year and false if it is not. #include <iostream> #include <cmath> using namespace std; class Date{ private: int year; public: Date(int yr = 2008) { // Constructor year = yr; cout << "year " << year << endl; //############################################## // method to determine if year is a leap year bool leap(); //############################################## ; int main() { int year; // the year in question cout << "enter a year (all four digits):\n"; cin >> year; Date my_year(year); if(my_year.leap()) { cout << " is a leap year\n; else cout << "is NOT a leap year\n"; return 0; 3

Hint: The conditional expression should check that either (year is evenly divisible by 4) and (not (evenly divisible by 100)) or that (year is evenly divisible by 400). Note that "evenly divisible" means divisible without any remainder. This is conveniently checked using the mod (%) operator. Note the logical operators are: and, or and not, and the relevant relational operators are == (equal) and!= (not equal). The function should be complete, and should include pre and post conditions. 4

Sections C and D (100 points) In a lab experiment we wish to determine the value of an unknown resistor R by measuring a number of voltage-current pairs. The voltage V(volts) is linearly related to the current I(amps) by Ohm's law, as shown below. Using the Least Squares approach we can use the n experiments to reduce the uncertainty and obtain the 'best fit' value of R, as follows: V Linear Least Squares fit of the data Vj I j V = R*I I leads to: R = n-1 j = 0 n-1 j = 0 (V j *I j ) (I j *I j ) Notice that when n = 1 then the Least Squares summation reduces to Ohm's law as shown on the graph. The experimental VI data is saved on a file which can be read into two arrays and then analyzed as above. The main program shown below has two function prototypes: getvi to get the data from the file to the volts[] and amps[] arrays, and findr to evaluate the least squares formula shown above. In the following two sections we wish to complete the program by writing both function definitions. // Evaluate the resistance from voltage/current experiments #include <iostream> #include <iomanip> #include <fstream> #include <cassert> using namespace std; void getvi(float volts[], float amps[], int& n, int max); // get VI data from file into volts, amps arrays float findr(float volts[], float amps[], int n); // determine resistance by Least Squares int main() { const int SIZE = 15; float volts[size], amps[size]; float resistance; int n = 0; getvi(volts, amps, n, SIZE); if(n > 0) { cout << fixed << showpoint << setprecision(2); cout << " volts amps\n"; for(int i = 0; i < n; i++) cout << setw(7)<< volts[i]<< setw(7)<< amps[i] << endl; resistance = findr(volts, amps, n); cout<< "resistance is approx. "<< resistance << " ohms\n"; else cout << "no data in file\n"; return 0; // the function definitions getvi and findr go here. 5

Section C (50 points) Assume that the experimental data required for the program above has been saved in two columns on a text file named VI.data as follows: 89.40 2.20 119.40 2.70 194.50 4.61 23.30 0.65 134.80 3.10 63.40 1.50 213.70 5.20 127.80 3.00 first column: voltage (volts) second column: current (amps) voltage/current data of a resistor Using this data file, the output resulting from execution of this program follows: volts amps 89.40 2.20 119.40 2.70 194.50 4.61 23.30 0.65 134.80 3.10 63.40 1.50 213.70 5.20 127.80 3.00 resistance is approx. 42.08 ohms In this section we wish to develop the function getvi, designed to get the values from the data file into the volts and amps arrays, as well as determine the number of data values (n) read from the file. 6

In the box below, write the C++ function getvi, which gets the data from the VI.data file to the volts and amps arrays and determines the number of data items read. Be sure not to overfill the arrays, and to have the computer respond suitably if the file could not be opened. No comments are required. The following represent typical file statements and functions that you may wish to use in your function: ifstream infile("my.data"); declares a file variable infile for getting data and associates infile with a disk file named my.data for input note that if the file could not be opened then infile will have the boolean value false thus we can check for opening of a file using the assert function or an if statement. assert(infile); a void function that aborts the program with a message if its argument is false, i.e. if the file could not be opened. infile >> x >> y; reading numerical data from the file into two declared variables x, and y. These could be either ordinary scalar variables or array elements. Statement will return the value false if the file does not contain numerical data, or if we have reached the end-of-file, thus this expression (without the trailing semicolon) can be used as the condition in an if statement or a while loop, as needed. Note that the while loop has the form: while(condition) {statements Thus the group of statements are repeatedly executed until the condition becomes false. infile.close(); closes the file after use. 7

Section D (50 points) In the box below write the C++ coding of a function named findr which evaluates the above Least Squares approximation to the data. It should return a float value (representing the resistance R) and has three arguments. The first two arguments are the float arrays volts and amps (representing V and I respectively), and the third argument is the integer n which represents the number of experiments. The Least Squares formula is repeated for your convenience: V Linear Least Squares fit of the data Vj I j V = R*I I leads to: R = n-1 j = 0 n-1 j = 0 (V j *I j ) (I j *I j ) The function should protect itself against a zero value of n by means of a displayed message and not allowing a division by zero to occur within the function. In this case the function should return the value zero. No comments are required. Hint: Evaluate the two summation terms (amps i 2 ), (volts i * amps i ) separately in a for loop, and then apply them to the formula before returning the result. Do not forget to initialize each summation, and to trap the exception condition (n equals zero). 8