EECS 280 Midterm || Detailed Solutions Offered.
Types of testing correct answers Unit testing, system testing, Regression testing
Unit Testing correct answers - Test smaller, less complex, easier to understand units
- One piece at a time (e.g., a function)
System testing correct answers - Test entire project (code base)
- Do this after unit testing
- Regression testing correct answers - Automatically run all unit and system tests after a code
change
Steps to test: correct answers 1. Understand the specification
2. Write tests
3. Check the results
Arrays correct answers An array is a fixed-sized, indexed, homogeneous aggregate type (a
collection of items, all of the same type.)
declare arrat correct answers int bar[4]; // Array, int bar[4] = {1, 2, 3, 4}; // Static Initializer
accessing array elements correct answers - You can access the contents of an array using an
"index". The index of the first array element is zero, the
next is one, and so on.
- Each individual element is used just like a regular int, so all of the following are legal:
1 // array = {1, 2, 3, 4}
2 array[1] = 6;
3 // array = {1, 6, 3, 4}
4 ++array[1];
5 // array = {1, 7, 3, 4}
6 array[0] = array[1];
7 // array = {7, 7, 3, 4}
Passing arrays to functions correct answers C++ arrays can be passed as arguments to a
function.
, - Suppose we wanted to write a function to add up the contents of an array and we want it to
work with any
size array.
- Here's what the declaration of such a function might look like:
5
Programming and Introductory Data Structures 3.4 Thinking about memory
1 int sum(inta[], unsigned int size);
2 // REQUIRES: there are at least size elements in a[]
3 // EFFECTS: returns the sum of the first size elements of a[]
- Important things to notice:
1. The declaration of the array argument does not specify the length of the array. That allows
the function
to work for any length.
2. sum() needs to know how long the array actually is (or at least, how many elements to
"sum up"), so
the second argument does this.
3. An unsigned intis used for size since it cant be negative. A regular intcould be used, but
then the
REQUIRES clause would need to catch cases of negative sizes. Its better to write a complete
function.
- Unlike most types we've seen so far, C++ arrays are notpassed by value. They are passed by
reference.
declare a pointer to int correct answers int *foo
bar is a pointer to int correct answers 1 int foo;
2 int *bar;
3 bar = &foo;
4 foo= 1;
- The symbol "&" means "address-of". So, this statement says that "bar is a pointer to an
integer, initialized
Types of testing correct answers Unit testing, system testing, Regression testing
Unit Testing correct answers - Test smaller, less complex, easier to understand units
- One piece at a time (e.g., a function)
System testing correct answers - Test entire project (code base)
- Do this after unit testing
- Regression testing correct answers - Automatically run all unit and system tests after a code
change
Steps to test: correct answers 1. Understand the specification
2. Write tests
3. Check the results
Arrays correct answers An array is a fixed-sized, indexed, homogeneous aggregate type (a
collection of items, all of the same type.)
declare arrat correct answers int bar[4]; // Array, int bar[4] = {1, 2, 3, 4}; // Static Initializer
accessing array elements correct answers - You can access the contents of an array using an
"index". The index of the first array element is zero, the
next is one, and so on.
- Each individual element is used just like a regular int, so all of the following are legal:
1 // array = {1, 2, 3, 4}
2 array[1] = 6;
3 // array = {1, 6, 3, 4}
4 ++array[1];
5 // array = {1, 7, 3, 4}
6 array[0] = array[1];
7 // array = {7, 7, 3, 4}
Passing arrays to functions correct answers C++ arrays can be passed as arguments to a
function.
, - Suppose we wanted to write a function to add up the contents of an array and we want it to
work with any
size array.
- Here's what the declaration of such a function might look like:
5
Programming and Introductory Data Structures 3.4 Thinking about memory
1 int sum(inta[], unsigned int size);
2 // REQUIRES: there are at least size elements in a[]
3 // EFFECTS: returns the sum of the first size elements of a[]
- Important things to notice:
1. The declaration of the array argument does not specify the length of the array. That allows
the function
to work for any length.
2. sum() needs to know how long the array actually is (or at least, how many elements to
"sum up"), so
the second argument does this.
3. An unsigned intis used for size since it cant be negative. A regular intcould be used, but
then the
REQUIRES clause would need to catch cases of negative sizes. Its better to write a complete
function.
- Unlike most types we've seen so far, C++ arrays are notpassed by value. They are passed by
reference.
declare a pointer to int correct answers int *foo
bar is a pointer to int correct answers 1 int foo;
2 int *bar;
3 bar = &foo;
4 foo= 1;
- The symbol "&" means "address-of". So, this statement says that "bar is a pointer to an
integer, initialized