CS1 Midterm 2Study Guide

  1. Go over all examples from class and homework.
  2. You'll have to trace through code and correct errors, like quiz 2
  3. Know DeMorgan's Laws, and be able to apply them
  4. Know good programming practices like proper indenting, using constants instead magic numbers, making good variables names, using appropriate comments, writing good test cases, etc.
  5. What does passing by value and passing by reference mean?
  6. Be able to identify and write a function declaration/prototype, definition and call.
  7. What is produced by the following output?

#includeiostream

using namespace std;

voidfigureMeOut(int& x, int y, int& z) {

cout < x < " " < y < " " < z < endl;

x = 1;

y = 2;

z = 3;

cout < x < " " < y < " " < z < endl;

}

int main( ) {

int a=10, b=20, c=30;

figureMeOut(a, b, c);

cout < a < " " < b < " " < c < endl;

}

  1. What is produced by the following output?

bool contains(string s, char c) // This code is suspect

{

for (int k = s.size()-1; k >= 0; k--)

{

if (s[k] == c)

return true;

else

return false;

}

}

int main() {

if (contains("xyz", 'x'))

cout < "x"endl;

elsecout < "no x" < endl;

if (contains("xyz", 'y'))

cout < "y"endl;

elsecout < "no y" < endl;;

}

  1. Write code, using a loop, that produces the following output:

5 4 3 2 1 0

  1. Write code, using a loop, that produces the following output:

12 14 16 18 20

  1. Write code, using a 2 nested loops, that produces the following output:

1 1111

2 222

3 33

4 4

5

  1. Write code, using a 2 nested loops, that produces the following output:

1

2

3

4

5
Free Response:

  1. Be able to write the functions from Project 3.
  2. Write a function, countEvens(int a[], int n), that return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
  1. Write a function, centeredAverage(int a[], int n), that returnsthe "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.

centeredAverage({1, 2, 3, 4, 100}, 5) → 3
centeredAverage({1, 1, 5, 5, 10, 8, 7}, 7) → 5
centeredAverage({-10, -4, -2, -4, -2, 0}, 6) → -3

  1. Write a function, either24(int a[], int n), that returns true if the array contains a 2 next to a 2 or a 4 next to a 4, but not both.

either24({1, 2, 2}, 3) → true
either24({4, 4, 1}, 3) → true
either24({4, 4, 1, 2, 2}, 5) → false

  1. Write a function, sum67(int a[], int n), that returns the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.

sum67({1, 2, 2}, 3) → 5
sum67({1, 2, 2, 6, 99, 99, 7}, 7) → 5
sum67({1, 1, 6, 7, 2}, 5) → 4

  1. Write a function, getLongestRun(int a[], int n), that returns the starting index in the array of a run of maximum size. A run is defined as the repeated occurrence of the same value in two or more consecutive positions in the array. If there are no runs return -1.

getLongestRun ({1, 2, 2}, 3) → 1
getLongestRun ({1, 2, 2, 99, 99, 99, 7}, 7) → 3
getLongestRun ({1, 11, 6, 7, 2}, 5) → -1

  1. Write a function, isEverywhere(int a[], int size, int n), We'll say that a value is "everywhere" in an array if for every pair of adjacent elements in the array, at least one of the pair is the value n. Return true if the given value is everywhere in the array.
    isEverywhere({1, 2, 1, 3}, 4, 1) → true
    isEverywhere({1, 2, 1, 3}, 4, 2) → false
    isEverywhere({1, 2, 1, 3, 4}, 5, 1) → false