Schedule‎ > ‎

Midterm 2 Review Supplements

Instructions

• Take out a piece of paper and put your name on the paper.
• Your work will be reviewed and graded, so make it presentable.
• We will complete as many of the following sections as we have time during class.
• The rest of the sections are to be completed at home and are due the day of the test

Review Topics

^ top

^ top

3. On your paper, write a solution to the following problems:
1. Of the following function signatures, the two that are not allowed in the same scope are ________.
1. `int add(int a, int b)`
2. `double add(int a, double b)`
3. `double add(double a, double b`
4. `double add(int x, int y)`
2. The reason the two function signature of the above are not allowed in the same scope is that ________.
1. function names are the same
2. parameter types are different
3. parameter types are the same
4. return types are different
3. For the following function call, the function definition that is invoked is ________.
```fun(98, 99);
```
1. `void fun(int n, double m)`
2. `void fun(double n, int m)`
3. `void fun(int n, int m)`
4. `void fun(double n, double m)`
4. Exchange papers with another student in the class for grading.
5. When grading, mark the number correct and the number of questions next to the problem heading, like: 2 / 3. (1m)

^ top

Reference Parameters

2. In Lesson 8B review the Reference Parameters section of the Summary.
3. On your paper, write a function named `swap()` that takes two `int` reference parameters and exchanges the values.
4. Exchange papers with another student in the class for grading.
5. When grading, mark the score as 5 and mark off 1 for each score not printed, like: 4 / 5. Verify the ampersand (&) is present for both parameters. (1m)

Test Yourself

1. True or false: When an argument is passed to a reference parameter, the actual variable is sent to the function.
2. True or false: when a variable is used as a call-by-reference argument, the value of the variable is never changed by the function call.
3. True or false: you cannot pass a literal value to a call-by-reference parameter.
4. True or false: it is always better to use call by reference because of the efficiency gains.
5. Call-by-reference is more efficient than call-by-value because ________.
1. less data is transferred for complex types
2. memory is a list addresses, which is easier for the computer to process
3. we can pass literal values to the function
4. we can pass constants to the function
6. True or false: call-by-reference places more restrictions on arguments to functions, such as prohibiting literal values as arguments.

^ top

Random Numbers

2. In Lesson 9A review the Random Numbers Summary.
3. On your paper, write a solution to the following problems:
1. A number whose value cannot be predicted ahead of time is known as a ________ number.
2. The function `random(10)` returns a number between ________ and ________.
3. The function that sets the starting location, or seed, for `random()` is ________.
4. To produce a new random seed for a program starts use the code: ________.
5. True or false: you must reseed the random number generator every time before calling `random()`.
6. The code to produce a random number between 0 and 5 is ________.
7. The code to produce a random number between 1 and 10 is ________.
8. The code to produce a random number between -10 and 10 is ________.
4. Exchange papers with another student in the class for grading.
5. When grading, mark the number correct and the number of questions next to the problem heading, like: 7 / 8. (1m)

^ top

Arrays and Vectors

2. In Lesson 9A & 9B review the Summary.
3. On your paper, declare a an array containing these values in order: 7, 14, 42, 7, 29.
4. Now write code to insert all the array elements into a vector, which you also must declare.
5. Finally, write code to display all elements of the vector to the Serial Monitor in order.
6. Exchange papers with another student in the class for grading.
7. When grading, mark the score as 5 and mark off 1 for each value not printed, like: 4 / 5. (1m)

Test Yourself

1. The first element in any array or vector has an index number of ________.
1. -1
2. 0
3. 1
4. 2
2. True or false: arrays cannot be resized.
3. True or false: we use arrays because they are more convenient than vectors.
4. True or false: the easiest place to insert an element is at the end of a vector.
5. True or false: to find a value in a vector, start at one end and search each slot until you find the value or reach the end.
6. After executing the following code snippet, what value is stored in element 0?
```vector<int> vectseries(10);
for (int i = 0; i < vectseries.size(); i++) {
vectseries[i] = i + 1;
}
```
1. 0
2. 1
3. 2
4. 3
7. What is wrong with the following code?
```vector<int> scores;
scores[0] = 90;
```
1. Nothing.
2. Cannot declare vectors without a size argument because the vector would be useless.
3. The first slot of a vector must contain the number 0.
4. Assigning a value outside the bounds of the vector.  (`can only use scores[0] = 90; if vector size indicated on declaration)`

^ top

Searching and Sorting Algorithms

2. In Lesson 10A review the Sorting and Sorting Algorithms Summary.
3. Be prepared to answer questions about Insertion, Bubble and Selection Sort.
4. You will need to write pseudocode for Insertion/Bubble/Selection Sort.
5. On your paper, write a function to sort the following vector (choose one of the above sort methods):
```vector<String> names;
```

Assume that the vector has some number of elements assigned.

6. Exchange papers with another student in the class for grading.
7. When grading, mark the score as 5 and mark off 1 for each value not printed, like: 4 / 5. (1m)

Test Yourself

1. The purpose of sorting is to ________.
1. arrange exam papers for grading
2. arrange items in an order
3. find an paper in a pile
4. move papers from one pile to another
2. Why is there more than one sorting algorithm?

Break and Continue

1.  What does the break statement do?

2.  What does the continue statement do?

3.  What does a return statement do and how does it differ from the break/continue?

Arduino Libraries

1.  What is the purpose of an Arduino Library?

2.  Do Arduino Libraries have to be written as a class?

3.  Give one example of a library that we have used in this course.

Pointers

a.  Review the Pointers summary here:  Pointer slides
The Summary is in the last couple of slides.

int a = 5;
int* myPointer = &a;
1.    Given the declarations above, write a statement using myPointer to change the value of a to 9
2.    Declare a pointer to a char.
3.    Declare a pointer to a double.
4.    What is one reason to use pointers?
5.    Given the function below:  What prints out?

void addIt( int *num1, int  num2) {
*num1  = *num1 + num2;
}

void setup(  ) {
Serial.begin(9600);
}

Classes

1.  Write definitions for the following:  classes, objects, constructors, default constructor, methods, interface, encapsulation.

2.  What is the purpose of the .h file,  what goes into it?

3.  What is the purpose of the .cpp file,  what goes into it?

4.  What is the purpose of the .ino file,  what goes into it?

5.  Code a class.  You will write code for all 3 files,  animal.h,  animal.cpp and animal.ino.

a.  Write a class Animal.

b.  Give your class some variables like number of legs, color, etc.

c.   Add a couple of constructors (including the default constructor).