Test 1: CPS 08 Owen Astrachan February 17, 1995 Name: Honor code acknowledgement (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Extra TOTAL: value 8 pts. 12 pts. 10 pts. 10 pts. 12 pts. 6 pts. 52 pts. grade This test has 9 pages, be sure your test has them all. Do NOT spend too much time on one question remember that this class lasts 50 minutes. 1
PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context of computer science, programming, and C/C++. 1. compiler (a) A program that compiles a textual list of all functions used in a program indexed by the line numbers on which the functions appear. (b) A program that translates a high-level language (e.g., C++) into a low-level language directly executable by a computer. (c) A program used to check the semantic content of a C++ program to determine if it will function as the programmer intends. 2. member function 3. ftp (a) A function that is part of a class, that can be applied to variables (objects) of the class, e.g., Length for the the class String. (b) A function that belongs to a library of functions, e.g., sin and sqrt as part of <math.h>. (c) A function whose prototype appears before the body of the function is actually dened. (a) A Unix command that's used to move from one directory to another. (b) A program used to set up an environment in Unix so that it works according to a user's preferences. (c) A program and protocol used to transfer les from one computing system to another. 4. reference parameter (a) A parameter that appears in a function header (or prototype) as opposed to in a function call. (b) A parameter that appears in a function call as opposed to a function header (or prototype). (c) A parameter that permits values to be \passed back" when a function is called. PROBLEM 2 : (Output: 12 points) Indicate the output of each of the cout statements below. Assume each statement is part of a program that compiles and runs. You can show your reasoning for partial credit. int count = 13; double value = 3.5; String word = "lease"; cout << count + 15/6 * 2 << endl; cout << (7.0 + 13)/8 << endl; cout << count + 13 % 2 * value << endl; cout << "p" + word << endl; cout << word[3] << endl; cout << 3.5*3.14159*2.718/0 << endl; 2
PROBLEM 3 : (A Capital Idea: 10 points) A sample run of a program generating quizzes about countries and capitals is shown below. How many questions : 3 What is the capital of Venezuala? Caracas Correct What is the capital of Kenya? Mombasa No, the capital is Nairobi What country has Reykjavik as its capital? Iceland Correct You got 2 out of 3 right output Assume that the function GeoQuiz whose header is given below is accessible by including the header le "geoquiz.h". void GeoQuiz(String & country, String & capital); postcondition: sets country and capital to a random country and its capital e.g., "United States", "Washington D.C." You may call GeoQuiz, you should NOT write GeoQuiz. Complete the program below so that it gives the kind of quiz shown above in the sample run. Each question should either print a country and ask for the capital or print the capital and ask for the country by simulating a \coin ip" (this can be done using a Dice variable). You can put all the code in main. Use the next page if you need more room. #include "dice.h" #include <iostream.h> #include "geoquiz.h" main() int numq; cout << "How many questions : "; cin >> numq; continued on next page 3
GeoQuiz(country,capital); if (die.roll() == 1) cout << "What is the capital of " << country << "? "; if (answer == capital) cout << "Correct" << endl; else else cout << "What country has " << capital << " as its capital? "; cout << "You got " << numright << " out of " << numq << " right" << endl; 4
PROBLEM 4 : (A Perfect World : 10 points) Anumber is called a perfect number if it is equal to the sum of its proper divisors (not including the number itself). For example, 6 and 28 are perfect because 6 = 1+2+3and28=1+2+4+7+14. The partial program below is designed to print all perfect numbers between 6 and a number entered by the user. If run with the input 30 it will print 6 and 28 (each on a separate line). Only the prototype for IsPerfect is given, you are to write the body of the function below main. In writing IsPerfect do NOT worry about the eciency of the function, just its correctness. Include brief pre and post-conditions for IsPerfect. #include <iostream.h> int IsPerfect(int num); main() int num; int limit; cout << "enter upper limit :"; cin >> limit; for(num=6; num <= limit; num++) if ( IsPerfect(num) ) cout << num << endl; 5
PROBLEM 5 : (A Course is a Course of course of course : 12 points) A class AceCourse is designed to allow C++ programs to get information about academic courses at Duke using the designated ACES number. The header le "acecourse.h" is reproduced below. As a sample of its use, the code fragment below AceCourse course(139772); if (course.isvalid()) 139772 is a valid course # cout << course.dept() << ", " << course.title() << ", "; cout << course.instructor() << ", " << course.area() << endl; cout << "enrollment ceiling = " << course.limit() << endl; generates the output below. ART, Art and Cyberculture, K. Stiles, AL enrollment ceiling = 15 Note: all ACES course numbers are six-digit numbers between 100,000 and 160,000. declaration for class AceCourse --- used to get/set information about courses at Duke using ACES call numbers AceCourse(int acenum) -- construct object having ACES call # e.g., AceCourse course(139772); defines course to correspond to Art 49S, Art and Cyberculture String Dept(), Title(), Instructor(), Area() -- return department, title, instructor, and area of knowledge int Limit() -- returns maximum number of students in course void Meeting(String & day, int & period) -- returns day course meets (either "M-W-F" or "T-Th" and period course meets (1-9) void SetNumber(int acenum) -- change course number to acenum (passed as parameter e.g., course.setnumber(144560); makes course CPS 08.01 bool IsValid(); -- returns true if course number used in constructor/setnumber corresponds to valid Duke Course e.g., after course.setnumber(1); course.isvalid() returns false since 1 isn't a valid ACES # 6
class AceCourse public: AceCourse(int acenum); construct course from ACES number String Dept(); dept of course String Title(); title of course String Instructor(); teacher of course String Area(); area of knowledge int Limit(); max # of students void Meeting(String &,int &); time course meets: MWF/Th, 1-9 void SetNumber(int acenum); set course to have ACES # acenum bool IsValid(); true if course is ok, false if not private: stuff here ; Part A: (3 points) The ACES number for Political Science 110 is 129244. Write a code segment that prints the instructor for the course and the day and period that the course meets. You MUST use AceCourse member functions to do this, you CANNOT write cout << "P. Gronke" << endl; to print the instructor, for example. 7
Part B: (9 points) Write the function KnowledgeArea whose header is given below. The function should print the title of every course whose area of knowledge is the value specied by the parameter distrib. A count of the number of courses should be printed too. For example, the call KnowledgeArea("CZ"); might generate output similar to the following:... Health, Healing, History American History/Social Theory The Atlantic Slave Trade # of courses with CZ designation = 152 Complete the function below. void KnowledgeArea(String distrib) postcondition: titles of all courses with area of knowledge = distrib printed with a count of the number of such courses 8
PROBLEM 6 : (Extra Credit: 6 points) This problem is NOT required An Armstrong number is any number of n digits such that the number is equal to the sum of each digit raised to the n th power. Formally: anumber i = d 1 d 2 :::d n is an armstrong number if i = nx (d j ) n j=1 Example: 153=1 3 +5 3 +3 3 =1+125+27 Example: 9=9 1 =9 Write the function IsArmstrong that returns true if num is an Armstrong number and false otherwise. bool IsArmstrong(int num) precondition: 0 < num postcondition: returns true if num is an Armstrong number otherwise returns false 9