Notes on the 2008 Exam

Similar documents
Notes on the 2015 Exam

Notes on the 2009 Exam

Notes on the 2016 Exam

University of Dublin

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

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

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

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

PIC 10A. Final Review: Part I

do { statements } while (condition);

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

CSCE Practice Midterm. Data Types

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

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

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

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

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

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

Arrays in Functions!

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

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

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

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

Introduction to C ++

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

University of Dublin

Programming Language. Functions. Eng. Anis Nazer First Semester

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Looping. Arizona State University 1

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

CSCE 206: Structured Programming in C++

CSCE Practice Midterm. Data Types

Chapter 3 - Functions

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

Unit 7. 'while' Loops

PIC 10A. Review for Midterm I

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!

The following expression causes a divide by zero error:

PIC 10A. Final Review

Getting started with C++ (Part 2)

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

Lab Instructor : Jean Lai

READ THIS NOW! Do not start the test until instructed to do so!

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

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Problem Solving: Storyboards for User Interaction

A SHORT COURSE ON C++

Run Time Environment

Introduction to Programming using C++

CSCE 121 ENGR 112 List of Topics for Exam 1

CS 007A Midterm 1 Practice Chapters 1 5

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Controls Structure for Repetition

C++ Programming Lecture 1 Software Engineering Group

Fundamentals of Programming CS-110. Lecture 2

C/C++ Programming Lecture 7 Name:

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

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

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Engineering Problem Solving with C++, Etter/Ingber

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Ch 6. Functions. Example: function calls function

Why Is Repetition Needed?

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Chapter Four: Loops II

C++ Support Classes (Data and Variables)

Local and Global Variables

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Project 1. due date Sunday July 8, 2018, 12:00 noon

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Arrays in C++ Instructor: Andy Abreu

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

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

The following is a typical execution run of this program:

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

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

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Chapter 2 - Control Structures

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

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Introduction to Programming I COS1511 School of Computing Revision Notes

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

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

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

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

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

Introduction to Programming EC-105. Lecture 2

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

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

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Transcription:

Notes on the 2008 Exam A hastily compiled review of this year s exam. Apologies if there are errors. Please also read notes on previous years exams especially the first set of notes. Question 1 Question is similar to Practical 6. But the inner loop requires a FOR to read 8 subj/mark pairs, (instead of the inner sentinel controlled while in practical 6). The key points here are correct use of a while loop to read data till a sentinel (student_no == 999 ) is reached (6 marks).; there is a standard template for this. use of a FOR loop (or equivalent) to read the 8 pairs. (4 marks) 2 marks for noting fails (using a Boolean variable fail ) and printing the subject code (i.e. the if (mark_per_exam < 40) bit) printing F if any subject was failed if (fail) (3 marks) remaining marks for initializations, the else if s for printing grades, computing and printing the average, #include<iostream> #include<string> #include<iomanip> using namespace std; const int NUM_EXAMS = 8; int main() string student_no, letter_grade, exam_code; double average, tot; int mark_per_exam; bool fail; //true if i've found a failed exam cout << fixed << setprecision(2); cin >> student_no ; while (student_no!= "999") //initialise variables for this student tot = 0; fail = false;

cout << student_no << "\tfailed: "; for (int i=0; i < NUM_EXAMS; i++) cin >> exam_code >> mark_per_exam; //update variables for computing average tot = tot + mark_per_exam; //handle fails if (mark_per_exam < 40) fail = true; cout << exam_code << " "; //end FOR loop average = tot/num_exams; cout << endl << "Average:" << average << "\tgrade: "; if (fail) //if any exam was failed letter_grade = "F"; else if (average >= 70) letter_grade = "D"; else if (average >= 60) letter_grade = "P1"; else if (average >= 50) letter_grade = "P2"; else if (average >= 40) letter_grade = "P3"; cout << letter_grade << endl << endl; cin >> student_no; //end while student_no!= 999 return 0; The code file is available and the sample data. To run it save both files from the web page, and at the Terminal type: make Q108./Q108 < Q108data.txt Question 2

NB This solution is lifted from Solution notes for Exam 2005 where there are extensive helpful comments. Note that this year, we didn t do this in a practical but we talked about it under Lecture Topic 10. (a) 3 marks for any 3 of the following main points I was looking for: functions extract commonality, abstract and simplify, hide detail functions break up a problem naturally allow delegation of parts of the problem to members of a team facilitate error-checking and step-by-step development allow re-use of code that has been developed for related problems (b) 3 marks Parameters are the means by which a main program communicates values to a function. Call-by-value parameters are when it is the values of the variables that are passed to the function. For example f (x) when x is 6 causes the value 6 to be given to the function f. The function cannot change the value of x. Call-by-reference parameters causes a variable, that is a place in which a value can be stored, to be passed to the function, and the function can then change the value in that variable. Call-by-reference is achieved by placing a & after the data type specifier for the parameter in question. For example void f (int& x) means that the thing passed in as x is a reference to a value that the function can update. (c) 7 marks 1 mark for the declaration; 2 for the if statement; 4 for some loop that achieved the required effect. See notes from 2005. int gcd (int n1, int n2) int i; //make i be the smnaller of the two numbers if (n1 < n2) i = n1; else i = n2; while (!((n1%i == 0) && (n2%i == 0))) //while i does not divide evenly into both n1 and n2 i--; //now i divides into both n1 and n2 return i; (d) 7 marks

3 marks for the declaration, especially the &s; 2 for calling gcd properly; 2 for updating n and d but not trying to return them. void simplify_fraction (int& n, int& d) int g = gcd (n,d); n = n / g; d = d / g; return; Question 3 Based on Practical 8 but required you to use a function, and required you to stop when approx squared was close to n, not when successive approxs were close to each other. See Q308.cpp on the web for a complete solution. (a) 12 marks roughly 5 marks for the bits that work out if our current approx is good enough; 1 of these reserved for using a function; marks taken off for using the other test for close_enough since that meant you learned off the practical solution instead of solving the question posed here. 4 marks for improving the approx using Newton s formula; 1 of these reserved for using a function. 3 for loop control and proper management of the variable. Note that nothing in part (a) of this question is concerned with init being n/2, precision being 0.0001, terminating when n = 0, or with reading in n or writing out the answer. These all come into play in part (b), being about how we want to use newtonsqrt this particular time. Another user of newtonsqrt might want to use it to compute sqrts of numbers obtained elsewhere, might want to pass the sqrts to someone else, might be happy with a precision of 0.001 or even 0.1 if they were dealing in very big numbers, and might have a better handle on what was a good initial approximation!! In general no function should cin or cout unless specifically designed to do so!! E.g. read_array or print_results functions. double newtonsqrt(double n, double init, double precision) double approx = init; while (!(close_enough (approx, n, precision))) approx = better_approx (approx,n); return approx; double better_approx (double x, double n) return (n/x + x)/2; bool close_enough (double x, double n, double precision) //return true if absolute value of (x*x - n) is v small

return (fabs (x*x - n) < precision); Without the helper functions it would look like this 5 lines of code would give you 9-10 of the 12 marks for part (a) double newtonsqrt(double n, double init, double precision) double approx = init; while (fabs (approx*approx n) > precision) approx = (n/approx + approx)/2; return approx; (b) 8 marks 4 marks for calling newtonsqrt properly, including using n/2 as initial n, and either printing the value, or assigning it to a variable and then printing it. Note that newtonsprt(n, n/2, 0.0001); cout << approx; is wrong because you don t save the value returned by newtonsqrt anywhere. 3 mark for the sentinel controlled loop; had to stop at n=0, not n<0; 1 mark for declarations and output. int main () double n; double approx; cout << "Enter a number whose square root you require:"; cin >> n; while (n!= 0) approx = newtonsqrt (n, n/2, 0.00001); //n/2 is an arbitrary initial approximation. //0.00001 is the value for precision cout << "The sq root of " << n; cout << " is approx " << approx << endl; cout << "Enter a number whose square root you require; 0 to terminate:"; cin >> n; return 0;

Question 4 This question was way too easy. But those who are repeating probably didn t get it!! Note however that the recursion question will be a bit harder in the supplemental exam. (a) 4 marks A recursive function is one which calls itself. (b) (i) 2 marks for each f(2,4) is 16. f(10,3) is 1000. Only one mark if you failed to carry the computations to their conclusion. If you were at any of the lectures remember how we went down a row of students till the base case was reached and then ran back along compiling the answer. In this case the students in the line would have to multiply the answer by a (either 2 or 10) as it came back up. (ii) 4 marks The power function, a to the power of b, for integers. (c) 8 marks int fact (int n) if (n == 0) return 1; else return (n * fact (n-1)); Max 3 marks if there was no recursive call. The key to recursive functions involving integers is to identify the base case, and then identify how to go from the solution for n-1 to a solution for n. In this case you multiply the answer for n-1 by n: that is n! is n times n-1!. Question 5 This was the arrays question. It was a simplification of the ideas in Practical 17. Simpler in that there was no need to compute a running average. A total score per course and a count of scores per course allows us to compute the average at the end. Also we are guaranteed that there will not be more codes than 50 so loop control is simpler. 3 marks for declarations of arrays and their capacity. 4 roughly for the search function or its equivalent. 3 for the sentinel controlled loop (note that overall you could get a lot of marks in this exam for just getting sentinel controlled loops right!!!) 5 for identifying the need to, and adding new slots (the if (i == -1) part) 2 for adding to tot and incrementing count 3 for the printing FOR loop.

The key concept is that the arrays represent the output in preparation rather than the input. So they are summary data what codes have appeared, how many times and what has been the total score given. When we read a code we need to find out have we seen it before (search(code, codes, num_codes)). If we haven t we add a new slot to the codes array, and corresponding slots to the count and tots array, initialized to 0. (Alternatively, like in the Practical 17 solution, set new slots to the input values, and have an else to handle the case where the code existed already.) int search (string s, string sarray[], int size); //returns the position of s in sarray, or -1 if not found const int CAPACITY = 50; const string SENTINEL = "XXX"; int main () //set up three parallel arrays; string codes [CAPACITY]; int counts [CAPACITY]; int tots [CAPACITY]; string code; int score; int num_codes = 0; cin >> code; while (code!= SENTINEL) //find code in the array or add it int i = search (code, codes, num_codes); if (i == -1) //code was not found in the array i=num_codes;//i is where the next code should go codes[i] = code;//store the new code tots[i] = 0; //initialise for now counts[i] = 0; num_codes++; //update num_codes //whether the code was new or not we are //now ready to add the score into slot i cin >> score; tots[i] = tots[i] + score; counts[i]++; //read the next code cin >> code;

//report results cout << " Code Number Average\n"; for (int i=0; i < num_codes; i++) cout << setw(5) << codes[i] << setw(5) << counts[i]; cout << fixed << setprecision(2) << setw(9); cout << tots[i]*1.0/counts[i] << endl;// the 1.0 forces the division to be noninteger return 0; int search (string s, string sarray[], int size) //returns the position of s in sarray, or -1 if not found for (int i=0; i<size; i++) if (sarray[i] == s) return i; //if we get here we didn't find s return -1; Other Miscellaneous Comments that I noted while grading: Sentinel Controlled Loops Template is cin >> x; while (x!= ) *** cin > x;

Where the *** are you put all processing of the current valid x. The processing of x must all be inside the loop. I had many examples of cin >> x; while (x!= ) cin > x; tot = tot+x; more stuff on x. But those last two lines must be inside the loop, before the cin > x statement. Obviously instead of x, you might have studentno or code or n or whatever it is that you are reading that will eventually be the sentinel instead of a valid value. Many people used the above template (with ***s in it or in it!!!!) every time they thought a while loop was needed. The template is ONLY for SENTINEL CONTROLLED input. Look carefully above for other kinds of while loops in this exam: gcd and newtonsqrt. Functions Many of these comments or similar appear in Notes for 2005 exam. Local variables and accessing a function s value: If a function looks like this int f(int x) int ans; ans = x; return ans; you can t do this z = 4; f(z); cout << ans; // wrong ans here is not related to f s ans and expect it to print the value of f(z) ans only has meaning inside f. Even if you declared ans in the calling program, that would be a separate variable from the one f uses, which is a local variable whose scope in the function f. The calling program has no access to f s ans. The correct way to call f and use the value it returns is: cout << f(z);

or int x = f(z); //now use or print x Accessing parameters: With the following declaration of f int f(int x, int y) the body of f has access to variables called x and y, and the values in there are the ones provided by the calling function. Therefore, f should not redeclare these variables read values for these variable initialize these variables So more than likely these are all wrong: int f(int x, int y) int x, y; //creates new local variables x and y which //effectively hides the ones passed in as parameters int f(int x, int y) cin >> x >> y; //if it succeeds in reading data it will replace // the values the calling function provided. int f(int x, int y) y=0; //if the calling function provides f with a value for y //f should use it!! Not replace it. And, unrelated to parameters, but the f declared above should not print any values, but must return its answer. So NOT int f(int x, int y) cout << (x*y)/2; return 0; but probably something like int f(int x, int y) return (x*y)/2;

IFs inside loops inside functions: This is a more subtle problem than the ones above. It came up in gcd and in the search function for Q5. Start by distinguishing between the two functions: and int f1(int n) for (int i=0; i<n; i++) if g(i) return i; return -1; int f2(int n) int ans=-1; for (int i=0; i<n; i++) if g(i) ans = i; return ans; Both functions will return an i for which g(i) is true, and both will return -1 if g(i) is not true for any i. However f1 will return the first i for which g(i) is true it returns immediately it finds such an i. Whereas f2 will return the last i for which g(i) is true it always goes through all the i-s from 0 to n-1. If you don t see that, try this one. How many times will the loop repeat? What values cold possibly be returned? int f (int n) for (int i=0; i<n; i++) if g(i) return i; else return -1; Because both branches of the IF statement return something, the first time we reach the IF statement the function will return something and the loop and f will be exited. Either f will return 0 (if g(0) is true) or it will return -1. The function will never get a chance to try i=1. Good luck!