Grades. Notes (by question) Score Num Students Approx Grade A 90s 7 A 80s 20 B 70s 9 C 60s 9 C

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

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

CSCE 206: Structured Programming in C++

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Test 2: CPS Owen Astrachan. November 17, Name: Honor code acknowledgement (signature)

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

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

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

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

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

C++ for Java Programmers

CSE 333 Midterm Exam 7/29/13

Lecture 2 Tao Wang 1

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

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

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

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CS201 Some Important Definitions

BITG 1233: Introduction to C++

Getting started with C++ (Part 2)

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Short Notes of CS201

CSE 333 Midterm Exam July 24, Name UW ID#

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

CS201 - Introduction to Programming Glossary By

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

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C:\Temp\Templates. Download This PDF From The Web Site

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Fundamentals of Programming CS-110. Lecture 2

Absolute C++ Walter Savitch

2 nd Week Lecture Notes

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:

Introduction to Computer Science Midterm 3 Fall, Points

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:

Multiple Choice Questions ( 1 mark)

C++_ MARKS 40 MIN

CSc Introduction to Computing

2. It is possible for a structure variable to be a member of another structure variable.

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

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

LECTURE 02 INTRODUCTION TO C++

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

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

C++ Final Exam 2017/2018

VuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52

Chapter 2. Procedural Programming

Do not start the test until instructed to do so!

FORM 2 (Please put your name and form # on the scantron!!!!)

Object Oriented Design

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

CS 115 Exam 3, Spring 2010

2 2

Creating a C++ Program

Introduction to Programming EC-105. Lecture 2

True or False (12 Points)

Introduction to Programming using C++

Final CSE 131B Spring 2005

EAS 230 Fall 2002 Section B

Final CSE 131B Spring 2004

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

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

COMP322 - Introduction to C++

My First Command-Line Program

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Initializing and Finalizing Objects

Preview 8/28/2018. Review for COSC 120 (File Processing: Reading Data From a File)

CS2255 HOMEWORK #1 Fall 2012

C++ Quick Guide. Advertisements

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

C: How to Program. Week /Mar/05

True or False (15 Points)

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

University of Toronto

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Object-Oriented Programming, Iouliia Skliarova

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

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.

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!

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

LESSON 4. The DATA TYPE char

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

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

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

University of Toronto

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

CS242 COMPUTER PROGRAMMING

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CSE 333 Midterm Exam Sample Solution 7/29/13

Programming. C++ Basics

Chapter 2 - Introduction to C Programming

Transcription:

Grades Score Num Students Approx Grade 100 1 A 90s 7 A 80s 20 B 70s 9 C 60s 9 C Score Num Students Approx Grade 50s 7 C 40s 5 D 30s 3 D 20s 1 F Notes (by question) 1. Use exit to avoid long if-then-else constructs. This is a standard idiom for handling files. 2. Read the question. You are to write a function. Your function operates on a character array. The function should contain no stream IO! The example only shows how your function might be used. Make sure you read the prototype of a function. Note that the prototype of toupper is char toupper (char) and not void toupper (&char). The prototype tells you how to use the function. The call must be s[i] = toupper(s[i]); and not toupper(s[i]); It is better to avoid nested loops when they are not necessary, as here. It easy to mess up the termination conditions. The following code may lead to a segmentation fault (why?): while (s[i]!= \0 ) { if (s[i] == # ) { while (s[i]!= \0 ) { if (islower(s[i])) s[i] = toupper(s[i]); 3. In order to reverse a string, you do not need to know it s length in advance (you can use strlen). In order to reverse an array, you do need to know the length in advance. Arguments that are passed by value do not to be declared as const (although it is not an error). Arguments pass by reference (such as arrays) may be usefully declared const in many cases, like the last question here. 4. To get credit you had to show the intermediate steps of horner s rule. 5. The second question would be more meaningful if I had used const int& f (const int a[]). Many of your answers were overly vague: the function takes a constant integer array. What does that mean? The third question was worth one point. It is discussed in the text, although I did not discuss this in lecture. About one third of you got it right. 6. I did not take off points if you missed the arch from node 1 to node 3. The other edges were worth about 2pts each. 2pts for indicating output correctly. 7. The last of the four questions was subtle. I accepted many answers. 8. Note that data members do not need to be passed as aguments to member functions. That is one of the great things about C++! 1

Command Line Arguments, and File I/O 1. [15 points] In this problem you will write a program (called copy) that copies the contents of one file to another. The file names will be provided as command line arguments. For example, the command copy old.txt new.txt should copy the contents of old.txt to the file new.txt. If the directory already contains a file named new.txt, it is overwritten. You are to define a program (the contents of copy.cpp) that implements this behavior. The program expects exactly two arguments. If the number of arguments is not exactly two, the program should print an error message and exit. Otherwise, the files should be opened and the contents copied from the first to the second. If one of the files cannot be opened, an error message should be printed. You need not #include any header files. ANSWER: #include <fstream.h> main (int argc, char **argv) { if (argc!= 3) { cerr << "usage: " << argv[0] << " infile outfile" << endl; exit(1); ifstream in(argv[1]); if (! in) { cerr << argv[0] << ": unable to open " << argv[1] << " for input" << endl; exit(1); ofstream out(argv[2]); if (! out) { cerr << argv[0] << ": unable to open " << argv[2] << " for input" << endl; exit(1); char c; while (in.get(c)) out << c; // OR // out.put(c); in.close(); out.close(); 2

Strings and Arrays 2. [10 points] Write a function up that takes a string and converts all lowercase characters after the character # (if it occurs) to uppercase, in place. For example, the fragment char s[50] = "Fox #3 is (Happy)"; up(s); cout << s << endl; should print Fox #3 IS (HAPPY). You need not #include anything. You may use the following functions. ANSWER: char toupper (char c); char tolower (char c); void up(char s[]) { int i(0); bool isupper (char c); bool islower (char c); while (s[i]!= # && s[i]!= \0 ) while (s[i]!= \0 ) { if (islower(s[i])) s[i] = toupper(s[i]); 3. [15 points] Give the prototype of the following functions. Your prototype must include the minimal necessary arguments and should be as restrictive as possible (use const when possible). A function sreverse that reverses a string in place. void sreverse (char s[]); A function ireverse that reverses an integer array in place. void ireverse (int a[], int length); A function imember that determines whether an integer is an element of an integer array, returning true or false. bool imember (const int a[], int length, int element); 3

Horner s Rule 4. [5 points] Show all intermediate steps of using Horner s rule to convert the string of bits (binary digits ) 10011 to an integer. Do not write any C++ code just compute the correct integer and show all the steps. 0 2 4 9 18 1 0 0 1 1 1 2 4 8 19 Classes (part I) 5. [12 points] Answer the following: Name three ways that constructors are distinguished from other member functions. Name is same as class No return type given in prototype Automatically invoked when class instance created Cannot be invoked on existing class instance A default constructor is defined, if none defined explictly Can contain an intialization list Consider the member function prototype const int f(const int[]) const;. What are the meanings of the three instances of the keyword const? (Identify them as first, second, third.) first: user cannot alter return value (in this case it changes nothing, since return is by value, rather than by reference) second: function cannot modify array argument third: function cannot modify data members Both structs and classes can have member functions and data members. What difference is there between them in the language definition? By default, struct members are public, class members are private. 4

LOGIN: Finite-State Machines 6. [20 points] Background: Just to refresh your memory, the following finite-state machine (fsm) echoes the A s in a text stream (and does not echo the rest). start A :: echo not A Input AGREEABLE Fabulous Output AA Question: You are to draw a fsm that removes C++-style comments from a text stream. You can pretend that C-style comments do not exist (treat them like normal text). Your fsm must correctly handle strings (in matching double quotes) and strings that contain double quote characters. Recall that a C++ comment begins with the two characters // and continues to the end of the line. A string starts with the character " and ends with the character ", as long as the final " is not preceded by a backslash. Whether the quotation or the comment takes precedence depends on which starts first. For example, your fsm should convert the input into the output: /* a program // by me */ char* x = "hello//\"//buddy\""; // a comment // The \" sequence escapes the subsequent character main () {; // doesn t do "much" /* a program */ char* x = "hello//\"//buddy\""; main () {; You may put your answer on the next page. Clarification: you can assume that the character sequence \" never occurs outside a string (i.e., \" only occurs inside "... ". 5

LOGIN: start other :: print / ; echo other :: echo 0 1 / " :: echo " :: echo other :: echo 3 / 2 other \n " :: print / ; echo \ :: echo 4 any :: echo Separate Compilation and Makefiles 7. [8 points] Consider the following makefile: it : it.o ms.o ; it.h : ms.h it.o : it.cpp it.h ; g++ -c it.cpp ms.o : ms.cpp ms.h ; g++ -c ms.cpp clean : ; rm *.o it Suppose it is up to date. List the commands that would be executed as a result of each of the following, independently. ( touch file has the same effect as changing file.) touch ms.cpp; make it make clean; make it touch it.h; make it touch ms.h; make it g++ -c ms.cpp rm *.o it g++ -c it.cpp g++ -c ms.cpp g++ -c it.cpp g++ -c ms.cpp 6

Classes (part II) 8. [15 points] Consider the following description of a car: Object Type: Car Knows: The amount of gas in its tank The capacity of its gas tank The number of miles per gallon it can travel Can do: Drive a given number of miles (or until the tank is empty) Fill the gas tank Tell the number of miles it can travel (given current amount of gas) When new cars are created, they should have a full tank. Define a class named Car for this design. You need only give the declaration of the class. You need not define any member functions. Be sure to include const declarations where possible. Clarification: When new cars are created, they have a full tank. The creator must specify the MPG and tank size. Use float ot double for these. ANSWER: class Car { public: Car(float mpg, float capacity); // OR // Car(float mpg, float capacity); // : gas_(capacity), mpg_(mpg), capacity_(capacity) // {; void drive(float nummiles); void fill (); float miles_to_empty () const; private: float gas_; const float mpg_; const float capacity_; ; 7