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.