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

Similar documents
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.

Getting started with C++ (Part 2)

Ch 6. Functions. Example: function calls function

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

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

REVIEW. The C++ Programming Language. CS 151 Review #2

Due Date: See Blackboard

Introduction to C++ (Extensions to C)

CSCE Practice Midterm. Data Types

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

WARM UP LESSONS BARE BASICS

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

CMSC 202 Midterm Exam 1 Fall 2015

Fundamentals of Programming CS-110. Lecture 2

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

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

BITG 1233: Introduction to C++

System Design and Programming II

Name. Classes and Objects 1. We re going to develop a class to help out Café Below is the definition for a class called

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences Computer Science Department

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

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

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

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

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

Introduction to Programming EC-105. Lecture 2

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.

do { statements } while (condition);

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

PIC 10A. Review for Midterm I

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Introduction to the C++ Programming Language

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 (!

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

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

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

Chapter 4 - Notes Control Structures I (Selection)

CSCE Practice Midterm. Data Types

LAB 4.1 Relational Operators and the if Statement

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

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

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Computer Programming : C++

CSCI 102 Fall 2010 Exam #1

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

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

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

Chapter 3 Problem Solving and the Computer

Computer Programming

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

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

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

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

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

Sample Code: OUTPUT Daily Highs & Lows

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

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

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Example 3. #include <iostream> using namespace std; int main()

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

Input And Output of C++

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

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

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

Chapter Four: Loops II

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

Assignment 3: Inheritance

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1

University of Toronto

Review Questions for Final Exam

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 3

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

A First Program - Greeting.cpp

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Module 7 b. -Namespaces -Exceptions handling

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Topics. Functions. Functions

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

CS 141, Introduction to Computer Science Fall Midterm Exam

1st Midterm Exam: Solution COEN 243: Programming Methodology I

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

CSCE 206: Structured Programming in C++

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

CS 115 Exam 3, Spring 2010

CSC 309/404 Section 901/910 Spring 2017 Midterm Exam Due: May 7 (Sun) 2015, 11:59 pm

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

True or False (12 Points)

Linked List using a Sentinel

LAB 7.1 Working with One-Dimensional Arrays

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

Lab01: C++ Expressions ES036a: Programming Fundamentals Fall 2007

Why Is Repetition Needed?

Transcription:

CS 117 Programming II, Spring 2018 Dr. Ghriga Midterm Exam Estimated Time: 2 hours March 21, 2018 DUE DATE: March 28, 2018 at 12:00 PM INSTRUCTIONS: Do all exercises for a total of 100 points. You are permitted to use all resources: CS117 zybook or equivalent, class notes, practice programs, Programming environments Code::Blocks or Xcode, internet resources, etc. The estimated time for the exam is 2 hours. You may dedicate as much time as you deem appropriate to answer all questions. The work you submit must reflect your individual work. The extra credit question worth 10 points at the end of Exercise 5 is optional. Good luck. SUBMITTING YOUR WORK: Write your answers to each exercise, precisely and concisely, on paper. Make sure that your name is noted on the first page. There is no need to rewrite the exam questions on your answer sheets. Electronic submissions will NOT be accepted unless specified otherwise in the exercise question. SUGGESTED APPROACH FOR THIS EXAM: 1) Use your notes and programming environment of choice, if necessary, to develop your solutions. 2) Compile your answers and results on paper. You may use Notepad and print. Your goal is to share your answers and solutions with me as reliably as you can. 3) The extra credit question requires that you submit your program version 2.0 by email. 4) Hand in your solutions to the exam on March 28 (Wednesday) at 12 PM. 5) Questions- send me an email at: mohammed.ghriga@liu.edu Exercise 1 [10 points] What is the output of the following program? Explain your results and why certain elements of the string did or did not change. #include <iostream> #include <string> using namespace std; void happy(char a); void grumpy(char a, char& b);

int main() string s = Hello World! ; happy(s[1]); cout << "string s is now:" << s << endl; grumpy(s[2], s[3]); cout << "string s is now: " << s << endl; return 0; void happy(char a) a = a + 1; void grumpy(char a, char& b) a ++; b++; Exercise 2 [15 points] We would like to use the lessons learned in Exercise 1 to write a function to decompose a given float number x into an integer part ipart and a factional part fpart. For example, if x is 100.75 then the ipart is 100 and the fractional part is 0.75. We can easily obtain the integer part and the fractional part of a float number using this simple algorithm: 1) convert number x to an integer using static_cast<int> and assign the result to ipart 2) fpart = x - ipart Propose a function prototype and its implementation to decompose a float number x (input parameter) and returns its integer part and fractional part as output parameters. Exercise 3 [25 points] a) Write the code for the following function prototype: // is_vowel(c) returns true if character c is a vowel and false otherwise. bool is_vowel(char c);

NOTE: The letters to be considered vowels in your program: a, e, i, o, u. Make sure that your function recognizes the uppercase vowel letters as well. b) Write the code for the following function prototype: // count_vowels(s) returns the number of vowels in string s. int count_vowels(string s); Exercise 4[25 points] Design two C++ classes Rectangle and Circle to represent the geometric shapes Rectangle and Circle, respectively. The class should have the appropriate data and function members. In addition, the classes should have const function members that return the perimeter and area of the geometric shape. Next, add 2 appropriate constructors, a destructor, and their code for each of class. Recall that the destructors in this case will be empty statement blocks. Assume that the default measurement for length, width, and radius is 0. Exercise 5 [25 points] Consider the modular code for a simple area calculator version 1.0 that computes the area of geometry shapes: circles. The program does the following: 1. It displays the simple area calculator menu choices (choices 1, 2, and 3 to quit): Simple Area Calculator Menu ---------------------------------------- 1. Calculate the area of a circle 2. Calculate the area of a rectangle: now under development 3. Quit to exit the program 2. It gets and validates the user s selected menu choice 3. It processes the user menu selection. If the user enters 1, the program asks for the radius of the circle and then displays its area. If the user enters 2, the program displays that this feature is under development by CS 117 students: the program output appears as follows:

This section is under development by CS 117 students... If the user enters 3, the program ends. The program does not accept a selection outside of the range 1-3.It does not accept negative values for a circle s radius and the rectangle s length or width when this new feature is added. 4. These steps are repeated until the user makes a choice to quit the program. The C++ program for the simple area calculator v1.0 follows. // SIMPLE AREA CALCULATOR v1.0 #include <iostream> #include <cstdlib> #include <iomanip> using namespace std; // display the area calculator menu // Simple Area Calculator Menu // ----------------------------------- // 1. Calculate the area of a circle // 2. Calculate the area of a rectangle: now under development // 3. Quit to exit the program void display_menu(); // function get_choice reads, validates and returns // the user's choice int get_menu_choice(); // function circle returns the area of a circle with radius r double circle(double r); // function is_positive returns false if k is strictly negative and true otherwise bool is_positive(double k);

int main() int choice; do display_menu(); choice = get_menu_choice(); double area = 0; // holds the area of a geometry shape cout << fixed << showpoint << setprecision(5); switch (choice) case 1: double radius; // radius of a circle cout << "Enter radius of circle: "; cin >> radius; while (!is_positive(radius)) cout << "Enter a non-negative radius: "; cin >> radius; area = circle(radius); cout << "Area of circle is: " << area << endl; break; case 2: cout << "This section is under development" << endl; cout << "by CS 117 students..." << endl; break; default:break; while (choice!= 3); cout << "Good bye...now. "<< endl; return 0;

void display_menu() cout << endl; cout << "Simple Surface Area Claculator Menu" << endl; cout << "-----------------------------------" << endl; cout << " 1. Calculate the area of a circle" << endl; cout << " 2. Calculate the area of a rectangle: now under development" << endl; cout << " 3. Quit to exit the program" << endl; cout << endl; int get_menu_choice() int choice; cout << "Enter your selection (1, 2, or 3): "; cin >> choice; while(((choice < 1) (choice > 3)) && (!cin.fail())) cout << "Try again (1, 2, or 3): "; cin >> choice; if (cin.fail()) cout << "Error: exiting now... " << endl; exit(1); return choice; double circle(double r) const double pi = 3.14159; double area = pi * r * r; return (area); bool is_positive(double k)

if (k < 0) return false; return true; A typical output of the program is: The simple area calculator has become a tool of choice for elementary school students, inside and outside the classroom. You have been asked to complete adding the new feature to compute the areas of rectangles, and thus, produce simple area calculator version 2.0. Do the following: Question 5.1 Write the function prototype and function definition of function rectangle which takes the length and width of a rectangle and returns the area of the rectangle. Limit your answer to providing a function prototype and its code.

Question 5.2 Review and revise the implementation of the functions display_menu to reflect the actual simple area calculator v2.0 menu. Limit your answer to providing the revised code for this function. Question 5.3 Revise the main program which should ask the user for the length and width of a rectangle, compute and display the area of the rectangle. Do not accept negative values for the rectangle s length or width when this new feature is added. Limit your answer to providing the sections that require revisions in your program. Question 5.4 Implement the shapes as classes and propose the revisions to the main function in the program. Use the classes you designed in the previous exercise. Question 5.5 (Extra- Credit- Additional 10 points) Put together all the revisions you have introduced in the previous question and submit electronically a working program Simple Area Calculator v2.0.