CS 126-2048

Name:______________________                                                      Dr. Sarah Zelikovitz

 

                                   

EXAM 2

 

PART 1: (24 Points)

 

 

Trace the following programs: 

 

Show  the output screen exactly.

 

1.

include<iostream>

#include<fstream>

#include<iomanip>

using namespace std;

void funexample(int, int &, int &);

int main()

{

  int i,j=0;

  int k = 15;

  for (i = 6; i < 10; i++)

  {

       funexample(i,j,k);

      cout<<i<<"  "<<j<<"   "<<k<<endl;

  }

}

void funexample(int a, int & b, int & c)

{

     a = 15;

     b = b + 2;

     c = c + b;

     cout<<a<<endl;

     cout<<b<<setw(7)<<'*'<<c;

}

 

 

2. The file junk.txt contains: 

 

 

y 9

n 8

y 67

n 9

n 20

 

#include<iostream>

#include<fstream>

using namespace std;

 

 

int main()

 

{

     int x;

     char ch1;

     bool found = true;

     int total = 0;

     ifstream infile;

 

     infile.open("junk.txt");

 

     infile>>ch1>>x;

     while (found && x < 15)

     {

         

          total = total + x;

          cout<<"The total so far is "<<total<<endl;

 

          cout<<"going to the next iteration\n";

          if (total > 78) found = true;

         infile>>ch1>>x;

 

     }

     cout<<"End of program";

 

 

 

}

 

 

         

 

3.

 

#include<iostream>

#include<fstream>

using namespace std;

 

 

int main()

 

{

     int x[12];

     int i,j;

     float f; 

 

     for (i = 4; i < 12; i++)

          x[i] = i-1;

     for (i = 0; i < 6; i++)

          if (i % 2) x[i] = 12;

          else x[i] = 0;

 

     for (i = 0; i < 12; i++)

          cout<<x[i]<<endl;

 

     cout<<fixed<<showpoint;

 

// remember that I could have used:        

// cout << setiosflags(ios::fixed);

// cout << setiosflags(ios::showpoint);

 

     f = 78.9034;

     cout<<setprecision(2)<<f;

 

}

    

 

4.

 

#include<iostream>

#include<fstream>

using namespace std;

void stop(int a, int & b);

int g;

int main()

 

{

     int  a=0,b=1,c=0;

     cout<<g<<endl;

     cout<<"Before the function call   ";

     cout<<a<<b<<endl;

     stop(a,b);

     cout<<"After the function call    ";

     cout<<a<<b<<endl;

     cout<<g<<endl;

 

}

void stop(int x, int & y)

{

     cout<<g<<endl;

     g++;

    cout<<"At the start of the function";

     x++;

     y = y + x;

     cout<<"At the end of the function";

     cout<<x<<y<<endl;

}

 

Part II (20 points)

 

For this part you may assume the following declarations:

 

int num[10],i;

float x,y,z,a,b,c;

char address[20];

int twodim[4][5];

 

 

Write a C++ statement (or statements) to do the following:

 

 

1.  Initialize all the elements of num to be 15.

 

 

2.  Set the sixth element of num to be equal to the static_cast<int> of :

 

3. Increment  each element of num.

 

 

4. Print out “true” if  address starts with an upper case letter.

 

 

 

5. Set address equal to “2800 Victory Blvd” (not in the declaration statement).

 

 

 

 

6. Initialize all elements of the two dimensional array twodim to be -1.

 

 

 

 

8. A function called figureout accepts three integer parameters.  The first two parameters are sent by value and the last is sent by reference.  Write the function prototype.

 

 

 

 

 

9. A function called printpretty accepts two character parameters.  It then prints a line that alternates between the two characters.  For example, if it is called with a * and %, it would output

 

 *%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%

 

Assume that 80 characters are printed in total (40 of each one).

 

Write the function prototype and definition (header and body) for this function.  Give an example of a call to the function.

 

 

 

 

 

 

 

 

 

 

 

Part  (8 points)

 

Complete the following program:

 

//This main program figures out the phone bill of a   

//customer.  It asks the customer to input the number of //minutes of day phone use and the minutes  of night //phone use.  It calls a function that returns the final //amount owed and prints it out in $ format (2 places after //the decimal).  The description of the function is in the //comment after the main program.

 

#include<iostream>

#include<iomanip>

using namespace std;

 

float compute_bill(float,float);

 

int main()

{

     float day_time,night_time,billtotal;

 

//read in the amount of time phone was used for day and //night time using cin

 

 

 

//call the function compute_bill

 

 

 

cout<<”the total owed  is: “;

 

 

//print out the total in dollar format

 

 

 

return 0;

 

}

//The function compute bill figures out the charge for

//the phone bill.  Night charges are 10 cents per minute.  //For the first 100 minutes of day time use, charges are 20 //cents per minute; for the rest of day time use, charges //are 15 cents per minute.

 

float compute_bill(float day, float night)

{

//write the code for the function

 

 

 

 

 

 

}

 

 

 

Part IV (33 points):

 

Questions:

 

1. (3 points)When do you need:

 

    a. #include<cstring>

 

    b. #include<fstream>

 

    c. #include<iostream>

 

 

2.  (2) Why might you use the break statement?

 

 

 

           What does the continue statement do?

 

 

 

3.  (2) What is the difference between a variable called by reference and one called by

 value?

 

 

 

     (2) What is the difference between a local and global variable?

 

 

 

 

  1. (3) Convert the following to a switch statement:

 

 

 

if (x == 1)

     cout<<”You have one more chance”;

else if (x ==3 || x ==4)

     cout<<”You lose”;

else if (x == 2)

     cout<<”You win”;

 

5. (4)  Assume the following declarations:

 

char name[9]= “Sally”;

char fullname[15];

int len;

 

What is the value of  name[2]?

 

 

If len =  strlen(name) what is the value of len?

 

 

What is the value of fullname after the statement strcpy(fullname,name) is executed?

 

 

What is the value returned by the call strcmp(name,”Smarty”)?

 

 

 

6.  (2) Show the first two passes through the selection sort when sorting the following array.

 

5        31  8  2  19  26  1  90  0  87 6

 

 

 

 

 

 

9. (3) Show the contents of the array mat after the following code executes.

 

float mat[2][3];

 

int i = 0;

int j = 2;

for (i = 0; i < 2; i++)

  {

     mat[i][j] = j + 4;

}

for (i = 0; i < 2; i++)

  for (j = 0; j < 2; j++)

     mat[i][j] = 15;

 

 

 

 

 

 

 

 

 

 

10. (3) Show the contents of the array num after the following code executes.

 

     int num[9] = {0};

 

    for (i = 3; i < 8; i++)

     {

       num[i] = i;

       num[i - 1] = i + 1;

     }

 

 

 

 

 

 

 

11. (2) What are the values of x and y after these statements execute?

 

 

x = 9 / 15;

 

What happens if x = 9.0/15?

 

y = 9 % 15;

 

12. (4 points) Suppose that I am searching for the number 16 in the following array num.

 

2 8 11 12 14 15 16 18 20

 

 What are the first four comparisons that are made for the SELECTION search?

 

 

 

 

 

 

 What are the first two comparisons made for the BINARY search?

 

 

 

 

 

 

 

13. (3 points)  What is the contents of the array arr after the following function is called?

 

void figure (int arr[])

{

     int i;

     for (i = 0; i < 10; i++)

          arr[i] = pow(2,i);

}

 

 

   Can you think of a more efficient way to do this?

 

 

 

 

Part V (15 points) (Use the blank sheet for this part.)

 

 

Assume that you have a file (you do not know how much data it has) that has the data for a number of cities with the rainfall for those cities.

 

For example, your data file might have:

 

Ottowa      3

Toronto   10

Montreal   8

 

(you may assume that there are no spaces in the names of the cities).

 

Your program should call a function that will determine whether there are restrictions on water use.  It will return the character  ‘y’ if there was less than 4 inches of rain, and ‘n’ otherwise.

 

Your program should calculate the average rainfall in all cities.

 

Your program should output the data in the following manner:

 

 

name of city   rainfall           restrictions in place?

 

Ottowa             3                  yes

Montreal          10                  no

Toronto            8                  yes

 

Average rainfall of 3 cities is  7 inches.

 

 

hint:  You can process each line as you read it, and do not have to store any data in arrays.