The questions below test the majority of what you are expected to be able to do having completed CSCI 160. It is much longer than a final exam, and is not meant to be completed in a single sitting. It should probably take 8-12 hours to complete all sections of all questions, but it is excellent practice for the final exam and should give you a good indication of your abilities and state of preparation for the exam.
Try to complete the questions without using your notes or computer, just the C++ quick-reference sheet.
Once you have completed a section, go to the lab and check your answers by actually coding and debugging them - the practice is extremely valuable!
Code segment | Output |
int x = 3; x = x + 1; x++; printf("%d\n", x); | |
bool a = true; bool b = false; bool c = a; bool d = !a; if (a) { printf("a is true\n"); } else if (!a) { printf("a is not true\n"); } else { printf("neither is true\n"); } if (a && b) { printf("a and b are true\n"); } if (a || b) { printf("a or b is true\n"); } if ((a && b) || (!c)) { printf("ugh, that was true\n"); } |
Example: Please enter an integer in the range 2-100 4 Please enter your 4 numbers: 3.1 2.0 100.1 50.2 The sum of the values you entered is 155.4 |
Example: Please enter an integer in the range 2-100 4 Please enter your 4 numbers: 3.1 The smallest so far is 3.1 2.0 The smallest so far is 2.0 100.1 The smallest so far is 2.0 50.2 The smallest so far is 2.0 The sum of the values you entered is 155.4 |
int f(int a, int b) { int result = 0; for (int i = 1; i <= b; i++) { result = result + a; } return result; } |
int p(int x, int y) { int result = 1; for (int j = 1; j <= y; j++) { result = f(result, x); } return result; } |
void f1(int &m, int &n) { int t = m; m = n; n = t; } | void f2(int &a, int &b, int &c) { if (a < b) { f1(a,b); } if (b < c) { f1(b,c); } if (a < b) { f1(a,b); } } | void f3(int x, int y, int z) { printf("%d, %d, %d\n", x, y, z); f2(x, y, z); printf("%d, %d, %d\n", x, y, z); } |
#include <cstdio> int x = 1; int f1(int x); void f2(int &x); int main() { printf("A: %d\n", x); int x = 2; printf("B: %d\n", x); x = f1(x); printf("C: %d\n", x); f2(x); printf("D: %d\n", x); } int f1(int x) { x++; printf("E: %d\n", x); return x; } void f2(int &x) { printf("F: %d\n", x); x = 6; for (int x = 4; x <= 4; x++) { printf("G: %d\n", x); } printf("H: %d\n", x); x++; printf("I: %d\n", x); } |
for loop version | while loop version | do-while loop version |
for (int x = 10; x > 5; x--) { printf("."); } printf("\n"); | ||
char inputChar; do { printf("Enter any character\n"); scanf("%c", &inputChar); printf("You entered \'%c\'\n", inputChar); } while (inputChar != 'Q'); | ||
int x = 0; int y = 10; int z = 100; int total = 0; while (x < 5) { while (y <= z) { total = total + (y - x); y = y * 2; } x++; } |
function | call and output |
void f1(int n) { int width = n; while (width > 0) { for (int i = 0; i < width; i++) { printf("x"); } printf("\n"); width--; } } | call: f1(4); |
void f2(int r, int c) { for (int row = 0; row < r; row++) { for (int col = 0; col < c; col++) { if ((row == 0) || (row == r-1)) { printf("-"); } else if ((col == 0) || (col == c-1)) { printf("|"); } else if (col == row) { printf("*"); } else { printf(" "); } } printf("\n"); } } | call: f2(5,5); |
void f3(int n) { for (int width = 1; width <= n; width++) { for (int i = 0; i < width; i++) { printf("y"); } printf("\n"); } } | call: f3(3); |
void f4(int n) { f3(n-1); for (int i = 0; i < n; i++) { printf("*"); } printf("\n"); f1(n-1); } | call: f4(3); |
Add the function to your program from part 4, and have the main routine pass the character array to it, then display the length returned.
For the sections where an algorithm is provided, write an appropriate code segment that implements the algorithm as closely as possible.
For the sections where a code segment is provided, write an algorithm that concisely summarizes/describes what the code is doing.
Algorithm | Code |
void shift(float arr[], int size, float fill) { for (int pos = size-1; pos > 0; pos--) { arr[pos] = arr[pos-1]; } arr[0] = fill; } | |
gcd(x,y) computes the greatest common divisor of x and y (the largest positive integer that evenly divides both) put the larger of x and y is L and the smaller in S repeat until S is 0: store a copy of S divide L by S, putting the remainder in S replace L with the earlier stored copy of S return L | |
// lcm(x, y) computes the least common multiple of x and y, // using the gcd function defined above int lcm(int x, int y) { return x * y / gcd(x,y); } | |
fibonnaci(N) if N is less than 2 return 1 otherwise return fibonnaci(N-1) + fibonacci(N-2) | |
int fib(int N) { int prevFib = 1, currFib = 1; for (int n = 3; n <= N; n++) { int temp = currFib; currFib = currFib + prevFib; prevFib = temp; } return currFib; } |
For the sections where an iterative function is provided, write an equivalent recursive version of the function.
For the sections where a recursive function is provided, write an equivalent iterative function.
Recursive version | Iterative version |
float getPositive() { printf("Please enter a positive number\n"); float val; scanf("%g", &val); if (val <= 0) { printf("%g is not positive, please try again\n", val); val = getPositive(); } return val; } | |
float sum(float arr[], int size) { float total = arr[0]; int pos = 1; while (pos < size) { total = total + arr[pos]; } return total; } | |
// binary search of part of a sorted array int bsearch(float arr[], int lowPos, int hiPos, float target) { int midPos = (lowPos + hiPos) / 2; int result; if (lowPos > hiPos) { result = -1; } else if (arr[midPos] == target) { result = midPos; } else if (arr[midPos] < target) { result = bsearch(arr, midPos+1, hiPos, target); } else { result = bsearch(arr, lowPos, midPos-1, target); } return result; } |
Program | Output |
#include <cstdio> void f(char c); int main() { f('x'); f('H'); f('q'); } void f(char c) { switch (c) { case 'q': printf("quitting!\n"); break; case 'h': printf("sorry, I have no help to give you!\n"); break; default: printf("hmmm, I don\'t understand that\n"); break; } } | |
#include <cstdio> enum CommandType { Quit = 'Q', Help = 'H', Invalid = '*' }; void processCmd(CommandType cmd); int main() { CommandType c = Help; processCmd(c); c = Invalid; processCmd(c); processCmd(Quit); } void processCmd(CommandType cmd) { switch (cmd) { case Quit: printf("quitting!\n"); break; case Help: printf("sorry, I have no help to give you!\n"); break; default: printf("hmmm, I don\'t understand that\n"); break; } } |
Algorithm | function |
linear search of an array of floats, parameters: the array, its size, and the search target for each position in the array if the target matches that array element return the current position if no match was found return -1 | |
bubblesort on an array of floats, parameters: the array, its size repeat size-1 times: for positions 0 through size-2 if the element in the current position is larger than the next element swap their positions in the array | |
selection sort on a null-terminated array of chars, parameters: the array for positions p=0 through size-2 smallest = the element in position p smallestpos = p for positions p+1 through size-1 if the value in the current position is less than smallest, then set smallestpos equal to its position and set smallest equal to that value swap the values in positions p and smallestpos |
The definition we have chosen to use for an individual item is this:
struct StGPA { long StuNum; float GPA; StGPA* next; }To represent an entire list, we will have a struct that keeps track of the front and back items in the list:
struct GPAList { StGPA* front; StGPA* back; };To manipulate the list, we have the following function prototypes:
// given a student number and a gpa, // allocate a StGPA item, fill in its fields, // and return a pointer to it StGPA* create(int stnum, float gpa); // initializes both fields of L to NULL // to represent an empty list void init(GPAList &L); // given a list and an item, insert the item at the back of the list // returning true if successful, false otherwise bool insert(GPAList &L, StGPA* st); // print all the student information currently in the list void print(GPAList L); // release all StGPA items in the list, // then reset L's fields to NULL void release(GPAList &L);
(a) Write a main routine that declares a variable of type GPAList to hold a list of student numbers and gpas, and make appropriate function calls to initialize the list and then insert the following student numbers and gpas (in this order)
555666777 4.1 567890123 2.3 666777888 3.1Add a call to print the list contents, and a call to release all the list contents.
(b) Write an implementation of the create function, the init function, and the print function.
StGPA* create(int stnum, float gpa) | void init(GPAList &L) | void print(GPAList L) |
// given hours in the range 0..23 and minutes in the range 0..59 // if both parameters are in range, this function // displays the time in the format hh:mm // if any of the parameters are out of range, this function // displays the message "invalid time supplied" void displayTime(int hours, int minutes);Your task is to provide a compact list of test calls that could be used to see if the function is working correctly, e.g. displayTime(23,59);, displayTime(-1,-1);, etc.
Provide the list of calls and why those test values were chosen.
#include <cstdio> int main(int argc, char *argv[])