Solutions

Transcription

Solutions
EE355 Midterm
Spring ’15 ● Redekopp
Name: Solutions
Score: ________/100
Short Answer and Multiple Choice [20 pts.]
1) In C, when written in an expression, the name of an array evaluates to:
_it's start address (p/c. Pointer)_________
2) True / False: The expression ‘new int’ will return an ‘int’
3) The expression (false==false==false) yields a bool value of ______ (true / false)
4) The expression ‘new double*[50]’ will return what specific C type: __double**__________
5) Given that an array declared as: int NUMS[30], if the start address is at 400 then what is the
index of the int at address 480?
___20 or NUMS[20]_________
6) Tommy wants to ensure an argument he passes is not modified by the function he calls. He
should pass it by __value____________ (one-word please)?
7) An arbitrary function may have __________ (more than one / only one / zero) written return
statements. It is possible to execute ____________ (more than one / only one / zero) of them.
8) When should consider allocating an array on the heap if its size is unknown until runtime or
________ (provide another reason to allocate on the heap)
If it should live beyond the end of the function (live indefinitely), etc.
9) Assume int x=2; The expression, x++ % 2 – 2*++x / 4.0, evaluates to what value?
-2 or -2.0
10) Which of the following actions could cause a segmentation fault (program crash) [circle all that
apply]?
a) Passing a pointer to a function
b) Dereferencing a NULL pointer
c) Accessing an array using an out-of-bounds index
d) Calling delete on a pointer to a dynamically allocated object
11) What Linux command would you use to change the directories to be 2 levels above your current
directory.
cd ../..
12) Binary search can run on _______ (both sorted and unsorted arrays, sorted arrays only,
unsorted arrays only) and runs in O(___) ( 1 / log n / n / n2)
13) [8 pts.] Use nested loops (and no arrays) to write C++ code that will produce the following
output to the monitor exactly as shown. Note: Tabs ('\t') separate each item on a line.
100 99
98
97
96
200 198 196 194 192
300 297 294 291 288
400 396 392 388 384
See if it works but it must use nested loops. Here is a reference solution
for(int i = 1; i <= 4; i++){
for(int j=0; j <5; j++){
cout << 100*i – i*j << ‘\t’;
}
cout << endl;
}
14) [10 pts.] Analyze the following program with a recursive function and show what will be output
by the various cout statements.
int f1(int *data, int n){
if(n < 0)
return 0;
cout << data[n] << endl;
if(data[n] > 0){
return f1(data, n-1) + data[n];
}
else{
return f1(data, n-2) + data[n];
}
}
int main()
{
int data[8] = {-5, -7, -2, 3, 1, -4, 6, 8};
cout << "Sum is " << f1(data,7) << endl;
return 0;
}
8
6
-4
3
-2
-5
Sum is 6
// For positive number it should take 1 step backward, negative take 2 steps backward
15) [12 pts.] Write a function that takes a 1D array of doubles that actually represents 2-dimensional
data similar to your PA2 maze and returns an array of the maximum values in each column.
The 2 dimension sizes are given by NR (number of rows) and NC (number of columns). The 1D
array holds all of the elements of a single row in contiguous order (all elements or row 0 come
before elements of row 1 in the array). Complete the return type & type in main() as well.
// This is a 1D array with 12
//
2x6 or 3x4 or 1x12
double data[] = {93.5, 80.2,
91.2, 100.0,
34.5, 78.2,
elements that can be thought of as
79.1, 85.6,
61.5, 89.3,
96.5, 87.2};
_double*____________ maxscores(double* scores, int NR, int NC){
{
double* max = new double[NC];
for(int c=0; c < NC; c++){
max[c] = 0.0;
for(int r=0; r < NR; r++){
if(scores[r*NC+c] > max[c]){
max[c] = scores[r*NC+c];
}
}
}
return max;
}
int main() {
int NR = 3, NC = 4;
___double*______________ max = maxscores(data, NR, NC);
for(int i=0; i < NC; i++){
cout << max[i] << " ";
}
cout << endl;
return 0;
}
// Result for the call in main above should print
// 93.5 100 96.5 89.3
16) [12 pts.] Examine the code below (line numbers are provided for easy referencing). Of the
options listed below, circle the ones that are POSSIBLE OUTPUTS of the program. Do not
circle outputs that are impossible for the program to produce.
Possible outputs (circle all that apply):
ABCD
ABD
ADE
ADEF
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
int x, y;
cin >> x >> y;
if( x > y ){
cout << "A";
if( y > 100){
cout << "B";
}
else if( x < 75 ){
cout << "C";
}
}
if( y < 75 ){
cout << "D";
if( x == 20){
cout << "E";
}
else if( y == 50){
cout << "F";
}
}
CD
CDEF
ACE
ACDF
If B or C print, then A has to print.
Similarly if E or F prints then D has to
This rules out:
CD, CDEF, ACE
ABCD and ABD can't happen because y
> 100 (B) and thus D can't happen.
ADE and ADEF can't happen because x >
75 (no C) so x can't be 20 (E)
17) [12 pts.] Analyze the following code and indicate what will be printed by the program in the box
below.
void f1(double r){
r += 1.0;
cout << r << endl;
}
void f2(double *s, int t){
*(s+2) -= t;
f1(s[0]);
s++;
cout << *s << endl;
*s = 6.0;
}
We pass s+1 (pointer to s[1] to f2. Thus
when f2 accesses (s+2) we are really
accessing s[3] from the main.
Similarly when we pass s[0] in f2 we are
really passing s[1] from main
When we increment s and we are now
pointing at s[2] from main and set it to 6.0
int main() {
double s[4] = {1.5, 2.0, 3.25, 4.0};
f2(s+1, 3);
cout << s[0] << " " << s[1] << " " << s[2] << " " << s[3] << endl;
return 0;
}
3
3.25
1.5 2
6
1
18) [12 pts.] Write a function to output the text version of some command line numeric strings. That
is, for each command line arguments which will always be an arbitrary length string of digits,
output the word version of each digit in the order it appears. We have defined an array “digits” to
help you.
For an execution of the program such as:
$ ./prog1 754 263
The desired output is:
seven five four
two six three
#include <iostream>
#include <cstring>
using namespace std;
const char* digits[] = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
int main(int argc, char* argv[]){
for(int i=1; i < argc; i++){
int len = strlen(argv[i]);
for(int j=0; j < len; j++){
cout << digits[argv[i][j]-'0'] << " ";
}
cout << endl;
}
return 0;
}
19) [14 pts] Write a program that receives a filename on the command line AND an integer on the
command line which represents how many integer values are in the text file specified by filename.
Open the text file, read in the integers in the file, and output the sum of the first and last integer,
second and second-to-last, third and third-to-last integer, etc. If the number of integers in the file
is odd, you should add the middle element to itself. Your program should:


Check whether the file opened successfully and return -1 if unsuccessful
Output the sums of the appropriate pairs of numbers. An example is shown in the box
below
You may declare other variables, call functions, etc. to make your code work.
We have provided the code that attempts to open the file for you.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char* argv[]){
ifstream myfile(argv[1]);
int i=0, len;
if(myfile.fail()){
return 1;
}
len = atoi(argv[2]);
int *data = new int[len];
int x;
while( myfile >> x){
data[i++] = x;
}
myfile.close();
for(int j=0; j < (len+1)/2; j++){
cout << data[j] + data[len-1-j] << " ";
}
cout << endl;
delete [] data;
return 0;
}
If the file data.txt contains:
1 5 2 3 6
And the program is run as:
$ ./prog data.txt 5
You should output:
7 8 4