CSC15 Exam Review Set:
Exam format will be similar to the quiz and pseudoquiz.
Exam focus areas:
Difference between statements and expressions
Variable declarations, use of variables
if and if-else constructs, including nested if-else
Functions :
functions that return values and functions that do not
How functions are called
How parameter passing works
How and why variables declared inside a function are local to the function
Classes and objects
Functions inside classes (how they share variables)
How objects are created and used.
Difference between private and public (both technical and philosophical)
Exam will NOT cover:
while loops
boolean expressions using && and ||.
The most important things to study are:
A. Lecture notes
B. Sample programs
C. Homeworks from the book
D. programming assignments
E. Lecture notes again
Practice Problems:
YOU SHOULD WRITE OUT ON PAPER ALL FUNCTIONS FIRST. (Remember, you'll be
asked to write code on paper during the exam!)
1. complete the following program, following instructions in comments:
#include
int main()
{
// declare integer variables x and y.
// prompt user, and take input for variable x.
// assign y to 2*x.
// if x is between -10 and +10, double the value of x (change x)
// otherwise, (using a nested if statement), test if the value of
// x is negative (< -10) or positive. If it's negative, set x to
// to -10 and print a message "x has been set to -10", otherwise, set
// x to 10 and print "x has been set to +10".
// Print out the final values of x and y, indicating which is which
return 0;
}
2. Write the following functions, then complete the main function below.
Remember: determine what parameter and what return value these functions
need to have first. These functions are all "global" - that is, they
are not part of any class. And so that means they don't share variables
with any other functions (all their variables are local).
#include
// write a function f that takes an integer x and return an integer
// representing 3*x + 1.
// write a function s that takes an integer x and returns no value.
// the function's code should first change the value of x to x+1, then
// print out the new value of x. e.g., s(3) should print 4.
// write a function g that takes an integer n and a character. The
// character is expected to be 'd' or 's'. If it's 'd', return 2*n,
// otherwise, if it's 's' return n*n. If it's a letter that's not 'd' or
// 's', return 0.
// write a function allthree that takes in three 'bool' parameters and returns
// true if all three boolean values are true, and returns false otherwise.
// For example, allthree(3<4, 5==2+3, 6>1) should return true.
// complete the following main function:
int main()
{
// declare variables x, and y, and assign them 3 and 4 respectively
// declare a bool variable b;
// assign y to the result of f applied to x;
// call function s on x; -- note this is a statement, since s returns void
// print out the value of x. Why did it print 3? Wasn't it changed to
// 4 inside the function s?! You need to understand what's happening
// here (local variables).
// assign to x the result of calling g on y and 's'.
// call the allthree function on the following boolean test values:
// x is greater than 2
// y is greater x
// y is less than 15
// assign the value returned by the function call to b.
// Print out the final values of x, y, and b, indicating which is which.
return 0;
}
3. Review the object-orientation examples, in particular the bank account
program, the object-oriented minutes-and-seconds program, and lab6, the
spareChange program. Remember that functions defined inside a class
can share variables that are also defined inside a class - they can have
"non-local" variables.
Define a class to represent a circle. The properties or variables defining
a circle should include the x and y coordinates of the center point, and
a value representing the radius (this is all that's necessary to define a
circle mathematically). All variables should be declared as doubles.
These variables should be declared in the private section of the class.
The public section of the class should contain the following:
// a. the constructor. remember the rules for writing the constructor:
// it must have the same name as the class, no return value is specified,
// not even void. And it should initialize the variables of the class.
circle(int x0, int y0, int r0)
{ ... }
// b. a function move that takes two parameters and adds them to the
// x and y center coordinates respectively. This will shift the
// center of the circle. Now think: does this function need to
// return a value? NO!
void move(int dx, int dy)
{ ... }
// c. a function diameter that returns the diameter of the circle
(2*radius). Now think: does this function require a parameter?
// d. a function that returns the circumference of the circle
// (diameter * 3.1415927). This function should call the diameter
// function you defined above to calculate the diameter.
// e. a function stretchcircle that takes a double value representing the
// circumference of a new circle. This function is similar to resize
// expect you're given not the new radius but the new circumference.
// You have to figure out how to set the radius value of the class.
// A little bit of high school algebra is needed here.
/* Thing to remember: many of the functions defined inside of the class
will be accessing/manipulating the variables of the class (x,y,radius).
so these functions do not take in these values as parameters. They
only need to take in parameters representing additional information they
will need to complete what it needs to do.
*/
// After the definition of the class, write a main function that
// should declare at least two circle objects, then call the various
// functions you defined above on these objects. It should include cout
// and maybe cin statements to get and display test data.