CSC126

Sample  QUIZ:  (Review for Quiz # 4)                                                                                 

 

 

1. Trace the following program:

 

#include<iostream>

#include<fstream>

#include<iomanip>

using namespace std;

void comparefloat(float, int &, int &);

int main()

{

  int i,j=0,k=0;

  float f1 = 56.68;

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

  {

      comparefloat(f1,j,k);

     cout<<j<<"  "<<k<<"   "<<f1<<endl;

      f1 = f1 -0.20;

  }

 

 

}

void comparefloat(float number, int & a, int & b)

{

     number++;

    

     if (number - static_cast<int>(number) > 0.5)

          a = a + 2;

    else

          b = b + 2;

 

     cout<<fixed<<showpoint<<setprecision(1)<<number<<endl;

     cout<<a<<setfill('*')<<setw(10)<<b<<endl;

}

 

57.7

2*********0

2  0   56.7

57.5

2*********2

2  2   56.5

57.3

2*********4

2  4   56.3

 

2. Write the declaration of an array called temperatures that is of type double and holds 30 values.

 

double temperatures[30];

 

    How many bytes does this array take up in memory?

 

240

 

    How can we initialize this array so that all the temperatures start off at 32 degrees?

int i;

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

     temperatures[i] = 32;

 

 

3. What is printed by the following program segment?

 

 

 

 int num[10];

 int i;

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

      num[i] = i - 1;

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

      if (i % 3)

           num[i] = num[i + 1];

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

      cout<<num[i]<<endl;

 

-1

1

2

2

4

5

5

7

8

8

What would go wrong if the array has 11 elements instead of 10 elements?

the statement num[i] = num[i + 1] would be executed for i = 10.  This would cause an error since the statement would be:

 

num[10] = num[11] and there does not exist a num[11]

 

4. Write the declaration of an enumerated type called year that can hold the values:  freshman,sophomore,junior,senior. (DID NOT COVER THIS YEAR)

 

 

  Declare two variable to be of type year.

 

 

  Write an if statement that compares the two variables and prints out “years are in order if the first is smaller than the second.

 

 

5. A function fillpowers accepts an array, an integer that refers to the number of elements in the array, and a last integer parameter.  It fills the array by raising the number to consecutive powers.  (The first spot of the array get the number raised to the 0th power, the next spot gets the number raised to the 1st power, etc)

 

Here is the function prototype:

 

 

void fillpowers(int[],int,int);

 

   Here is a piece of the main code, including the call to the function:

 

#include<iostream>

using namespace std;

const ELEMENTS = 10;

int main()

{

 

 int num[ELEMENTS];

 fillpowers(num,ELEMENTS,5);

}

 

Write the function definition (header and body).

 

 

void fillpowers(int a[], int x, int y)

{

      int i;

      a[0] = 1;

      for (i = 1; i < x; i++)

      {

            a[i] = a[i - 1] * y;

            cout << a[i] << endl;

      }

}