Notes on the 2016 Exam

Size: px
Start display at page:

Download "Notes on the 2016 Exam"

Transcription

1 Notes on the 2016 Exam The stuff inside borders is all that you needed to write. The rest is commentary. Please also read notes on previous years exams especially the 2005 set of notes. Note that up to 2008 each question was worth 20 marks (for a total exam mark out of 80). So the relative marks per piece of solution will be very different. Question 1 This required a sentinel controlled loop. Marks as follows 10 for correct handling of input (the sentinel controlled loop); 10 for correct computing of grades (that s 5 marks for noting the marks less than 30 and for counting the marks less than 40; 2 marks for correctly computing the average mark; 3 marks for correct grade based on average and number of marks less than 30 and 40); 10 for the counting of each grade and the bar chart. 3 for any attempt at a function; either for the bars of the graph or for turning a mark into a letter grade. There were a number of ways to handle the <30 / <40 grades. I have chosen one of them here. Be careful not to output twice that the same student failed (because of a <30 and a low average for example) or to count a failure twice. In my solution I output the grade in one place only and count up that grade at that point only. const int NUM_EXAMS = 12; int main() string student_no, letter_grade; double average, tot; int exam_mark; bool fail; //true if student has failed one <30 or two <40 bool onelessthan40; //true if student has failed at least one <40 int countd = 0, countp1 = 0, countp2 = 0, countp3 = 0, countf = 0; //number of students getting each overall grade cout << fixed << setprecision(2); cin >> student_no ;

2 while (student_no!= "XXX") //initialise variables for this student tot = 0; fail = false; onelessthan40 = false; cout << student_no << "\t"; for (int i=0; i < NUM_EXAMS; i++) cin >> exam_mark; //update variables for computing average tot = tot + exam_mark; //handle fails if (exam_mark < 30) fail = true; else if (exam_mark < 40) if (onelessthan40) fail = true; //if this is the second mark <40 then failed! else onelessthan40 = true; //else not a failed exam //end FOR loop average = tot/num_exams; //Debug message: // cout << endl << "Average:" << average << "\tgrade: "; if (fail) //if one <30 or two <40 letter_grade = "F"; countf++; else if (average < 40) letter_grade = "F"; countf++; //failed on the average mark else if (average < 50) letter_grade = "P3"; countp3++; else if (average < 60) letter_grade = "P2"; countp2++; else if (average < 70) letter_grade = "P1"; countp1++; else letter_grade = "D"; countd++; cout << letter_grade << endl; //next record... cin >> student_no; //end while student_no!= 999 //now print the bar chart cout << " D ";

3 for (int i=1; i<=countd; i++) cout << "*"; cout << endl; cout << "P1 "; for (int i=1; i<=countp1; i++) cout << "*"; cout << endl; cout << "P2 "; for (int i=1; i<=countp2; i++) cout << "*"; cout << endl; cout << "P3 "; for (int i=1; i<=countp3; i++) cout << "*"; cout << endl; cout << " F "; for (int i=1; i<=countf; i++) cout << "*"; cout << endl; return 0; The declaration of the functions you might have used are string letter_grade (double mark); void printnstars (int n); You can find these functions among the code on the web page. Question 2 This question is testing your ability to use functions. Therefore there were marks for designing, declaring, defining, and calling functions properly. Note that none of these functions has any cin or cout statement!! Values come to the function through the parameters and the answer is explicitly returned to the calling program or calling function using return, or values computed by the function are made available to the calling program/function via call-by-reference parameters (&). Always think carefully before putting a cin or cout statement in a function does the function explicitly require reading (i.e. its job is to read things in from the user) or printing (i.e. its job is to display stuff on screen for the user). If not, then get the values your fnction needs from the parameters list, and either return the answer or (if there are multiple answers ) use &s and update those parameters. (a) sort2 10 marks This question checked whether you knew about call-by-reference parameters. There were 3 marks for the &s in the declaration. These say that what is passed to the function is a reference to a place where an integer is

4 stored. The function uses the reference to access the location and update it. Without the & beside x1, x1 = x2 would have no effect on the value of the variable passed as the first parameter to the function the u of the first call in the question, or the w in the second call. void sort2 (int& x1, int& x2) if (x1 > x2) int temp; temp = x1; x1 = x2; x2 = temp; 1 mark for void. 4 marks for the swapping of the numbers, mostly for realizing that you need a temporary variable to swap two numbers. 2 for using IF properly. Note that you don t need an else part if x1 is not greater than x2 then you do nothing at all. (b) This question was largely based on a practical. (i) 6 marks 3 marks for the declaration and the return statement i.e. the machinery of functions. 3 marks for the sums. double distance(double x1, double y1, double x2, double y2) double x = pow((x1-x2),2); double y = pow((y1-y2),2); double result = sqrt(x+y); return result; (ii) 5 marks A lot of marks for very little. You lost one if you had if blah return True; else return False; since this is equivalent to blah. This function returns a boolean value. Fabs returns the absolute value (i.e. negative numbers are switched to positive) of a double. bool veryclose (double s1, double s2) return (fabs(s1-s2) < );

5 (iii) 8 marks 2 marks for the declaration and return statement. 1 for the call to distance function 3 marks for the logic the ifs, (or) and && (and). 2 marks reserved for using veryclose function correctly. It is not correct to just count veryclose pairs No cout statements this function returns an integer to the calling program. The calling program decides what it wants to do with that information. int triangletype(double x1, double y1, double x2, double y2, double x3, double y3) int type; double side1, side2, side3; side1 = distance(x1,y1,x2,y2); side2 = distance(x1,y1,x3,y3); side3 = distance(x2,y2,x3,y3); if (veryclose(side1,side2) && veryclose(side2,side3)) type = 3; else if(veryclose(side1,side2) veryclose(side1,side3) veryclose(side2, side3)) type = 2; else type = 0; return type; Note that you don t need to say if (veryclose(x,y) == True) since that is the same as if (veryclose(x,y)). (iv) 4 marks 1 mark for some way of giving values to the vertices (cin or hardcoded) 1.5 marks for call to triangletype 1.5 for using an if statement to output textual answers, not just 3, 2, or 1. double x1, y1, x2, y2, x3, y3; cout << "Enter x1, y1, x2, y2, x3, y3:"; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; int type = triangletype(x1, y1, x2, y2, x3, y3);

6 cout<<"the vertices ("<<x1<<","<<y1<<"), ("<<x2<<","<<y2<<"), ("<<x3<<","<<y3<<") make up a "; if (type == 3) cout<<"equilateral triangle"<<endl; else if (type == 2) cout<<"isoceles triangle"<<endl; else cout<<"scalene triangle"<<endl; return 0; Question 3 This question was also testing your ability to define and use functions. Many of the marks related to definition, declaration and use of read, print and percentage_gain. Also important to use daynumber and share_price functions correctly. This was an easy question for 33 marks! (a) 3 marks double percentage_gain (int old_value, int new_value) double change = ( * (new_value - old_value)) / old_value; return change; (b) 5 marks, 3 of them for &s void read_details(string& company, int& num, int& day, int& month, int& year) cin >> company >> num >> day >> month >> year; (c) 8 marks (very generous!! ) You were asked to print out value of the shares, not unit rpice. void print_details(string company, int num, int old_price, int new_price) cout << company << <<num cout << << old_price * num; cou << << new_price * num; cout<< << calculate_gain (old_price, new_price) << "%"<<endl; (d) 17 marks total; 1 for declaring and initializing variables and constants; 3 for using daynumber to compute a number for today; 3 for calling read_details; 3 for using daynumber to compute the number for the purchase date; 4 for the two calls to share_price; 3 for calling print_details.

7 //global constants for today's date const int THIS_DAY = 27; const int THIS_MONTH = 5; const int THIS_YEAR = 2016; //functions provided// int daynumber (int day, int month, int year); int share_price (string company, int daynumber); int main () int num_held, d, m, y; int cost_price, current_price; string company; int today = daynumber(this_day,this_month,this_year); read_details (company, num_held, d, m, y); int day_bought = daynumber(d,m,y); cost_price = share_price (company, day_bought); current_price = share_price (company, today); print_details(company, num_held, cost_price, current_price); return 0; Question 4 (a) (i) 4 marks This question does not require fill_up, which is designed to read an unknown number of values. Here the function is given the number of values to read. Note that it is reading words, which are strings (without spaces). Use a for loop because you know how many times to repeat. void readnwords (string a[], int n) int i; for (i=0; i<n; i++) cin >> a[i]; (ii) 6 marks This is a bool function. There are two ways to organise it. If you use a for loop, if you reach a mismatch, return false immediately, effectively abandoning the loop. If the loop completes, no mismatch was found, so return true. Note that the const keywords before the parameters are not required but what they say is that this function does not affect the values in the arrays passed in.

8 bool match (const string a[], const string b[], int size) int i; for (i=0; i<size; i++) if (a[i]!= b[i]) return false; //if we reach here all items matched return true; A while loop more naturally allows you to keep going till you find a mismatch. But you also need to watch out for the end of the array. And after the loop ends, you have to check which condition caused the loop to end the end of the array (i.e. i==size) or a mismatch. So it ends up a bit more complicated. //Alternative form bool match (const string a[], const string b[], int size) int i=0; while (i < size && a[i] == b[i]) i++; if (i == size) return true; else return false; (b) 23 marks Part (a) should have helped you to think about array functions, but you did have to make some changes. The read function for part (b) needs to read into a char array, not a string array (2 marks for making this change). And the match function of part (a) was not directly useful for part (b) (though one or two strong students did make suitable use of it), but might have steered you towards reading answers into an array and then calling a function to take the solution and an actual answer set and compute the score. The problem can be solved without using an array for the answers you would have to read answers one at a time and compare them with the correpsonding entry in the solution array, and score as you read. Marks were awarded as follows: 8 marks for the score function (roughly 3 for its declaration and the return, 2 for the for loop, 3 for the if statement / sums) only 2 marks for read since it was almost identical to Q4(a)(i). 13 marks for main, broken down roughly as 5 marks for the sentinel loop, 2 for declaring arrays, 3 for reading solution and answers (i.e. calls to readanswers), 2 for calling grade function correctly, and 1 for output. Note that if you used no functions, you lost 6 marks. #include <iostream> using namespace std;

9 void readanswers (char a[], int size); /* reads size chars into array a */ /* used to read both solution and actual answers */ int score (const char a[], const char soln[], int size); /* scores exam in array a given that soln contains the right answers */ int main () const int SENTINEL = 999; const int TESTSIZE = 30; int studentid; int grade; char solution[testsize], answers[testsize]; readanswers(solution, TESTSIZE); cin >> studentid; while (studentid!= SENTINEL) readanswers(answers, TESTSIZE); grade = score (answers, solution, TESTSIZE); cout << studentid << " " << grade << "marks" << endl; cin >> studentid; return 0; void readanswers (char a[], int size) /* reads size chars into array a */ /* used to read both solution and actual answers */ int i; for (i=0; i<size; i++) cin >> a[i]; int score (const char a[], const char soln[], int size) /* scores exam in array a given that soln contains the right answers */ int i, grade = 0; for (i=0; i<size; i++) if (a[i] == soln[i]) //right answer grade = grade + 1; else if (a[i]!= 'x') //wrong answer grade = grade -1; //else a[i] is 'x' //no change to grade return grade; If you are going to answer the array question, focus on getting basics right: Make sure that you declare the appropriate array(s) what will you need to put in the array (ints, strings, ), how many will you need (30 in this case, but often you need lots of space only some of which will actually be used).

10 Use a loop to work through the array, whether to read elements into it, print out each element, compare each element to something, add each element, whatever. Once you know the number of elements in the array (30 in this case, but for arbitrary sized arrays the actual size will be established by the reading fill_up function check the lecture notes), most of the array processing will use a for loop for (int i=0; i<size; i++) (but do check that i isn t already being used for something else, that you do want to start at 0, that size is where you have stored the number of elements in the array, and that you do want to process all values up to (size-1).) Know how to use functions that handle arrays. Always pass the array size in (usually as the last parameter). In the function declaration, use [] to indicate that the parameter is an array. When you call the function, just pass the array, without any square brackets. Inside the function you will be referring to the elements of the passed array as a[i] or something similar. Don t forget the basic rules about functions reading and printing functions are void; if their job is to compute a value, they are not void functions, so you need to decide what kind of value they return (score returns an int), and they should return a value (e.g. return grade; ) and when called, the answer needs to be stored or printed (e.g. grade = score(.); ). Other Miscellaneous Comments that I noted while grading in They are all very useful to you if you are repeating 1E3: 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;

11 tot = tot+x; //wrong more stuff on x.//wrong 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. 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: or cout << f(z); int x = f(z); //now use or print x Accessing parameters: With the following declaration of f int f(int x, int y)

12 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; Good luck!

Notes on the 2008 Exam

Notes on the 2008 Exam 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

More information

Notes on the 2009 Exam

Notes on the 2009 Exam Notes on the 2009 Exam A hastily compiled review of this year s exam. Apologies if there are errors. The stuff inside borders is all that you needed to write. The rest is commentary. Please also read notes

More information

Notes on the 2015 Exam

Notes on the 2015 Exam Notes on the 2015 Exam The stuff inside borders is all that you needed to write. The rest is commentary. Please also read notes on previous years exams especially the 2005 set of notes. Note that up to

More information

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

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

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

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout

More information

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011 The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS

More information

CSCE Practice Midterm. Data Types

CSCE Practice Midterm. Data Types CSCE 2004 - Practice Midterm This midterm exam was given in class several years ago. Work each of the following questions on your own. Once you are done, check your answers. For any questions whose answers

More information

Multiple Choice (Questions 1 13) 26 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) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

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

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

More information

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

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010 The American University in Cairo Computer Science & Engineering Department CSCE 106-08 Dr. KHALIL Exam II Spring 2010 Last Name :... ID:... First Name:... Form - I EXAMINATION INSTRUCTIONS * Do not turn

More information

Multiple Choice (Questions 1 13) 26 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) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

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.

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. 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. B. Outputs to the console a floating point number f1 in scientific format

More information

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey Exam 1 CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey I understand that this exam is closed books and closed notes and is to be completed without a calculator, phone, or other computer.

More information

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam,

More information

Ch 6. Functions. Example: function calls function

Ch 6. Functions. Example: function calls function Ch 6. Functions Part 2 CS 1428 Fall 2011 Jill Seaman Lecture 21 1 Example: function calls function void deeper() { cout

More information

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

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009 The American University in Cairo Department of Computer Science & Engineeringt CSCI 106-05&09 Dr. KHALIL Exam-I Fall 2009 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

CSCE Practice Midterm. Data Types

CSCE Practice Midterm. Data Types CSCE 2004 - Practice Midterm This midterm exam was given in class several years ago. Work each of the following questions on your own. Once you are done, check your answers. For any questions whose answers

More information

Lab Instructor : Jean Lai

Lab Instructor : Jean Lai Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.

More information

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

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points) Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow

More information

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED Class Example student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED #include #include using namespace std; class student public:

More information

Chapter Four: Loops II

Chapter Four: Loops II Chapter Four: Loops II Slides by Evan Gallagher & Nikolay Kirov Chapter Goals To understand nested loops To implement programs that read and process data sets To use a computer for simulations Processing

More information

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

More information

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

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver) Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program

More information

University of Dublin

University of Dublin University of Dublin TRINITY COLLEGE Faculty of Enginering & Systems Sciences School of Engineering Junior Freshman Engineering Trinity Term 2014 Computer Engineering I (1E3) Date Location Time Dr L. Hederman

More information

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

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

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

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;

More information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

CS 141, Introduction to Computer Science Fall Midterm Exam

CS 141, Introduction to Computer Science Fall Midterm Exam CS 141, Introduction to Computer Science Fall 2006 Midterm Exam Name: Student ID: 1 (12 points) Data Types Where possible give 3 examples of possible values for each of the following data types. Use proper

More information

Introduction to Programming I COS1511 School of Computing Revision Notes

Introduction to Programming I COS1511 School of Computing Revision Notes Introduction to Programming I COS1511 School of Computing Revision Notes UNISA 2018 1 Introduction Some key basic principles to remember: Apply the BODMAS rules of Mathematics for all calculations; The

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

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

1) What of the following sets of values for A, B, C, and D would cause the string one to be printed? Instructions: This homework assignment focuses primarily on some of the basic syntax and semantics of C++. The answers to the following questions can be determined from Chapters 6 and 7 of the lecture

More information

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); } 1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to

More information

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester Programming Language Control Structures: Repetition (while) Eng. Anis Nazer Second Semester 2017-2018 Repetition statements Control statements change the order which statements are executed Selection :

More information

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise

More information

Unit 7. 'while' Loops

Unit 7. 'while' Loops 1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

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

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following: Computer Department Program: Computer Midterm Exam Date : 19/11/2016 Major: Information & communication technology 1 st Semester Time : 1 hr (10:00 11:00) Course: Introduction to Programming 2016/2017

More information

Introduction to Programming EC-105. Lecture 2

Introduction to Programming EC-105. Lecture 2 Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Week 4 EECS 183 MAXIM ALEKSA. maximal.io Week 4 EECS 183 MAXIM ALEKSA maximal.io Agenda Functions Scope Conditions Boolean Expressions Lab 2 Project 2 Q&A Lectures 15% 36% 19% 8:30am 10:00am with Bill Arthur 10:00am 11:30am with Mary Lou Dorf

More information

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ 0.1 Introduction This is a session to familiarize working with the Visual Studio development environment. It

More information

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)

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) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

The following expression causes a divide by zero error:

The following expression causes a divide by zero error: Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct

More information

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

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

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

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

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved Chapter Four: Loops Slides by Evan Gallagher The Three Loops in C++ C++ has these three looping statements: while for do The while Loop while (condition) { statements } The condition is some kind of test

More information

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

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80 Exam 2 CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson Question Points Score 1 18 2 29 3 18 4 15 Total: 80 I understand that this exam is closed book and closed note and

More information

Review of Important Topics in CS1600. Functions Arrays C-strings

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

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

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage: Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

More information

LAB 4.1 Relational Operators and the if Statement

LAB 4.1 Relational Operators and the if Statement LAB 4.1 Relational Operators and the if Statement // This program tests whether or not an initialized value of num2 // is equal to a value of num1 input by the user. int main( ) int num1, // num1 is not

More information

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

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1 Professors: ML Dorf, Elliot Soloway Wed 9- February- 2011 35 questions * 3 pts each = 105 pts (yes we know there

More information

CMSC 202 Midterm Exam 1 Fall 2015

CMSC 202 Midterm Exam 1 Fall 2015 1. (15 points) There are six logic or syntax errors in the following program; find five of them. Circle each of the five errors you find and write the line number and correction in the space provided below.

More information

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 The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Dr. Khalil Final Exam Fall 2012 Last Name :... ID:... First Name:... Form

More information

Problem Solving: Storyboards for User Interaction

Problem Solving: Storyboards for User Interaction Topic 6 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:

More information

Local and Global Variables

Local and Global Variables Lecture 10 Local and Global Variables Nearly every programming language has a concept of local variable. As long as two functions mind their own data, as it were, they won t interfere with each other.

More information

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31. Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172 The lab will be on pointers In compensation, no class on Friday, Jan. 31. 1 Consider the bubble function one more

More information

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

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully. CS 215 Spring 2018 Lab Exam 1 Review Material: - All material for the course up through the Arrays I slides - Nothing from the slides on Functions, Array Arguments, or Implementing Functions Format: -

More information

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

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

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

Do not turn to the next page until the start of the exam. Introduction to Programming, PIC10A E. Ryu Fall 2017 Midterm Exam Friday, November 3, 2017 50 minutes, 11 questions, 100 points, 8 pages While we don t expect you will need more space than provided, you

More information

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

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No. The American University in Cairo Computer Science & Engineering Department CSCE 106 Instructor: Final Exam Fall 2010 Last Name :... ID:... First Name:... Section No.: EXAMINATION INSTRUCTIONS * Do not

More information

University of Dublin

University of Dublin University of Dublin TRINITY COLLEGE Faculty of Enginering & Systems Sciences School of Engineering Junior Freshman Engineering Trinity Term 2015 Computer Engineering I (1E3) Date Location Time Dr L. Hederman

More information

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

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10; Solving a 2D Maze Let s use a 2D array to represent a maze. Let s start with a 10x10 array of char. The array of char can hold either X for a wall, for a blank, and E for the exit. Initially we can hard-code

More information

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

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6 Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #5 due today Lab #3

More information

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

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given. CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given. All multiple choice questions are equally weighted. You can generally assume that code shown in the questions

More information

Chapter 3 Problem Solving and the Computer

Chapter 3 Problem Solving and the Computer Chapter 3 Problem Solving and the Computer An algorithm is a step-by-step operations that the CPU must execute in order to solve a problem, or to perform that task. A program is the specification of an

More information

Chapter 3. More Flow of Control

Chapter 3. More Flow of Control Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-2 Flow Of Control Flow of control refers to the

More information

WARM UP LESSONS BARE BASICS

WARM UP LESSONS BARE BASICS WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions

More information

Consider the following statements. string str1 = "ABCDEFGHIJKLM"; string str2; After the statement str2 = str1.substr(1,4); executes, the value of str2 is " ". Given the function prototype: float test(int,

More information

do { statements } while (condition);

do { statements } while (condition); Topic 4 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:

More information

CSCE 121 ENGR 112 List of Topics for Exam 1

CSCE 121 ENGR 112 List of Topics for Exam 1 List of Topics for Exam 1 If statements o How is an if statement constructed? o Does every if need an else? Looping o While loop! What does a while loop look like?! How do you ensure you will not have

More information

BITG 1113: POINTER LECTURE 12

BITG 1113: POINTER LECTURE 12 BITG 1113: POINTER LECTURE 12 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the concept of pointer. 2. Write declaration and initialization of a pointer. 3. Do arithmetic

More information

COS 126 General Computer Science Spring Written Exam 1

COS 126 General Computer Science Spring Written Exam 1 COS 126 General Computer Science Spring 2017 Written Exam 1 This exam has 9 questions (including question 0) worth a total of 70 points. You have 50 minutes. Write all answers inside the designated spaces.

More information

Why Is Repetition Needed?

Why Is Repetition Needed? Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable

More information

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1 BEng (Hons) Electronic Engineering Cohort: BEE/10B/FT Resit Examinations for 2016-2017 / Semester 1 MODULE: Programming for Engineers MODULE CODE: PROG1114 Duration: 3 Hours Instructions to Candidates:

More information

cs1114 REVIEW of details test closed laptop period

cs1114 REVIEW of details test closed laptop period python details DOES NOT COVER FUNCTIONS!!! This is a sample of some of the things that you are responsible for do not believe that if you know only the things on this test that they will get an A on any

More information

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 The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Dr. Khalil Final Exam Fall 2013 Last Name :... ID:... First Name:... Form

More information

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

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II Superior University Department of Electrical Engineering CS-115 Computing Fundamentals Experiment No.7 User Defined Functions II Prepared for By: Name: ID: Section: Semester: Total Marks: Obtained Marks:

More information

Operator overloading: extra examples

Operator overloading: extra examples Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading

More information

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Fall 2015 Jill Seaman 1 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

C++ Final Exam 2017/2018

C++ Final Exam 2017/2018 1) All of the following are examples of integral data types EXCEPT. o A Double o B Char o C Short o D Int 2) After the execution of the following code, what will be the value of numb if the input value

More information

BITG 1113: Array (Part 1) LECTURE 8

BITG 1113: Array (Part 1) LECTURE 8 BITG 1113: Array (Part 1) LECTURE 8 1 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional (1 D)

More information

Chapter 1 INTRODUCTION

Chapter 1 INTRODUCTION Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer

More information

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

More information

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay CS 101 Computer Programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building Bombay Lecture 4, Conditional execution of instructions Friday, August

More information

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100 CSC 126 FINAL EXAMINATION Spring 2011 Version A Name (Last, First) Your Instructor Question # Total Possible 1. 10 Total Received 2. 15 3. 15 4. 10 5. 10 6. 10 7. 10 8. 20 TOTAL 100 Name: Sp 11 Page 2

More information

Arrays in C++ Instructor: Andy Abreu

Arrays in C++ Instructor: Andy Abreu Arrays in C++ Instructor: Andy Abreu Reason behind the idea When we are programming, often we have to process a large amount of information. We can do so by creating a lot of variables to keep track of

More information

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSE030 Fall 2012 Final Exam Friday, December 14, PM CSE030 Fall 2012 Final Exam Friday, December 14, 2012 3-6PM Write your name here and at the top of each page! Name: Select your lab session: Tuesdays Thursdays Paper. If you have any questions or need

More information

C++ basics Getting started with, and Data Types.

C++ basics Getting started with, and Data Types. C++ basics Getting started with, and Data Types pm_jat@daiict.ac.in Recap Last Lecture We talked about Variables - Variables, their binding to type, storage etc., Categorization based on storage binding

More information

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 The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Dr. Howaida Ismail Final Exam Spring 2013 Last Name :... ID:... First Name:...

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information