CSC 126                                    FINAL EXAMINATION                  FINAL Fall 2008 A

 

Name (last, First) ____________________________   __________________________

 

Instructor_________________________________________________

 

 

 

 

Question #

Total Possible

Total Received

1.

8

 

2.

6

 

3.

20

 

4.

7

 

5.

10

 

6.

6

 

7.

12

 

8.

8

 

9.

8

 

10.

15

 

TOTAL

100

 


For each of the following exam questions, place your answers on the question paper.  There will be no scrap paper given.  If you need scrap, two blank pages are provided at the back of this exam paper.  Your name must appear at the top of each page, including the scrap pages in the back.

NO CALCULATORS WILL BE PERMITTED!!

 

 

1. (8 points) Match the term in COLUMN A with the phrase in COLUMN B which defines the term.  Write the letter of the phrase in the blank space to the left of the term.  Note that as there are more phrases than terms, not every phrase matches a term,

 

COLUMN A                                         COLUMN B

 

__D__1. gigabyte

 

__H__2. compiler

 

__F__3. secondary storage

 

__J__4. CPU

 

__B__5. syntax

 

__E__6. main memory

 

__K__7. algorithm

 

__L_8. ROM

 

 

a. The machine language version of the high level language program.

 

b. The set of rules for formulating grammatically correct language statements.

 

c. The process of writing a piece of software.

 

d. 1,073,741,824 bytes

 

e. Volatile memory where your programs and data are stored while you use the computer.

 

f.   Non-volatile memory for data and programs.

 

g. Program which directs and controls the overall operation of the computer

 

h. A program which translates instructions written in a high-level language into the equivalent machine language

 

i. . Computer instructions written by the user which have not been compiled yet.

 

j. Hardware which performs control functions and arithmetic and  logical operations

 

k. A step by step problem solving process in which a solution is arrived in a finite amount of time.

 

l. Contains the fundamental instructions that initially control the computer.  These instructions cannot be lost or changed by the computer user.

 

 

2.  (6 points) Write the C++ statements (but not the entire program) which will print the excerpt from the following Lewis Carroll poem exactly as it appears below.  The indented lines are indented one tab position.  Don’t worry about the spelling (Lewis Carroll invented words for this poem)   but watch the punctuation marks very carefully!

 

"Beware the Jabberwock, my son!

The jaws that bite, the claws that catch!

Beware the Jubjub bird, and shun

The frumious Bandersnatch!" 

 

 

 

cout << "\"Beware the Jabberwock, my son!"<<endl;

cout << "The jaws that bite, the claws that catch!"<<endl;

cout << "Beware the Jubjub bird, and shun"<<endl;

cout << "The frumious Bandersnatch!\"";

 

 

 

 

 

 

 

 

 

 

 

 

 

3. (20 points) What output will be displayed from each of the following four program segments?

 

a)

int w = 7;

while (w <= 5)

  w = w – 2;

cout << w << endl;

 

 

7           note that this does not enter the loop because the initial condition is false

 

 

 

 

 

 

 

 

 

 

 

 

 

b)

int  a,b;

for ( a = 3; a >= 1; a -- )

{

    for ( b = 1; b < a; b ++ )

       cout << “**” << b;

    cout << ‘&’ << endl;  

}

 

 

 

 

**1**2&

**1&

&

 

 

 

 

 

 

 

c)

int x;

double y;

 

x = 10 % 3 * 2 - 3 / 2 + 1 ;

 

y = (9 / 2.0 + 1.5 + 3 )* 2 / 6 + 1;

 

cout << x <<'\t'<< y<<endl;

 

 

 

2       4

 

 

 

 

 


d)

alpha  = 4;

switch(alpha)

{

case 1:  cout<< ”Nothing at all ! \n”;

break;

case 2: alpha = alpha + 10;

     break;

case 4: alpha++;

break;

case 5: alpha = 2 * alpha +1;

break;

case 6: alpha = alpha - 3;

     break;

default: alpha--;

}

 

cout<<”After the Switch Instruction, Alpha is\t”<<alpha<<endl;

 

 

After the Switch Instruction, Alpha is  5


4. a) (4 points) Consider the following list:

 

list = {24, 20, 10, 75, 70, 18, 60, 35};

 

Suppose that the list is sorted from lowest to highest using the selection sort algorithm. What is the resulting list after two iterations of selection sort?

 

Write your answer in the space provided.

 

Iteration 1: 

 

 

 

 

 

 

 

 

 

 

 

Iteration 2:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b) (3 points) Which search algorithm would best be used when trying to find a person by their last name  in an alphabetically sorted telephone book?  Why?


5. a)  (5 points) Declare an array that holds 100 floating point numbers.  Write the code that will set all elements of the array to be equal to -1.0

 

 

double numbers[100];

int i;

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

numbers[i] = -1.0;

 

 

 

 

 

 

 

b) (5 points) Given the following declaration:

 

               int nums [2] [4];

Draw a diagram of nums showing the number of rows and columns.  Draw the picture of the filled in array after the following code executes.

 

 

 

 

 

 

 

 

 

 

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

      for (int j = 0; j < 4; j++)

           nums [i] [j] = j + 10;

 

10

11

12

13

10

11

12

13

 

 

 

 

 

6. (6 points) Change the following if – else statement to a switch statement.

 

if (day == 1)

      cout<<"Where is Yesterday ?\n" ;

else if (day == 0)

      cout<< "Tomorrow is near .......\n";

else if (day == 3 || day == 2)

      cout<< "Today is here !\n";

 

switch(day)

{

case 1: cout<<"Where is Yesterday ?\n" ;

           break;

case 0: cout<< "Tomorrow is near .......\n";

           break;

case 3:

case 2: cout<< "Today is here !\n";

}
7.
Questions a – d are 2 points each,

 

Suppose you have strings: 

                                char wordA[ ] = {“HAPPY”};

                                char wordB[ ] = {“HOLIDAYS”};

                                char wordC[20];

                                int n1, n2;

 

 

a.              What is the value of  wordB[1]? _______O_______________

 

 

b.                        n1 = strlen (wordA);  What value does n1 receive? ________5

 

 

c.                 n2 = strcmp (wordB, wordA); What is the value of n2?_____1____________

 

 

d.                        After strcpy (wordC, wordA) what is in wordC?______HAPPY__________

 

 

e. (4 points) Suppose there was a wordD declared similar to wordA or wordB above. 

Further assume that the initial value stored is not known to you. 

Write the code fragment that finds the number of occurrences of the letter ‘A’ in wordD.  DO NOT WRITE AN ENTIRE PROGRAM.

 

EXAMPLE:  If wordD stored the word ABRACADABRA, your code would display the following output:

 

There are 5 A’s in ABRACADABRA.

int i=0;

int count=0;

 

while (wordD[i] != '\0')

{

       if (wordD[i] == 'A')

            count++;

       i++;

}

cout << "There are "<<count << " As in the word\n";


8. (8 points) A triangular number is the sum of the n natural numbers from 1 to n.

 For example the triangular number for 3 is 1 + 2 + 3 = 6

The triangular number for 7 is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28

 

Write a value-returning function called triangular, that accepts an integer n and returns its triangular number.

 

For example, if the function is passed the values 7, the function returns the value 28. 

 

Write the prototype for this function, and write the function definition (header and body).  Also give an example of how the function would be called from the main program.

 

Prototype:

 

 

int triangular(int);

 

Example call:

int x;

 

 x = triangular(5);

 cout << x<<endl;

 

 

 

 

Function definition (header and body):

int triangular(int a)

{

int i;

int sum=0;

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

       sum += i;

 

return sum;

}
9,

#include <iostream>

using namespace std;

 

const int num = 27;

void calc(int, int, int&);

 

int main( )

{

  int firstnum, secnum, product;

  double trouble = 12.34;

 

  cout<<"Enter two numbers: ";

  cin >> firstnum >> secnum;

 

  calc(firstnum, secnum, product);

  cout<<"The product of the number is: " << product << endl;

 

  return 0;

}

 

void calc(int num1, int num2, int& prod)

{

  int addit = 0;

 

  addit = addit + num;

  prod = num1 * num2;

  cout<<"Addit is equal to: "<<addit<<endl;

  return;

}

 

 

 

a)  (3 points) A CSC 126 student added the following function call to the program. 

 

calc(firstnum, trouble, secnum, sum, product);

 

The compiler flagged this statement with an error message.  Why?

 

The function calc should only have three parameters, not five.

 

 

b) (5 points) What is printed by this program if the user enters the values 5 and 7?

 

 

 

Enter two numbers: 5 7

Addit is equal to: 27

The product of the number is: 35

 


 

10, (15 points)  The new reality series, America’s Best Bargain challenges contestants to find discounted items while living in the Staten Island Mall 

 

The show’s producers need a computer program to keep track of contestant’s progress.  Your program will read from a data file and calculate how much each contestant saved on the items they purchase. It will then find the total number of items purchased and the average amount saved per item.

 

Each line of the file “Bargain.txt”, contains the contestant number, the number of bargain items found, and amount the contestant saved per item.  A few lines in the file are shown:

 

JIM         10                    14.50  

MARY   3                      116.25

JASON 100                  12.00

.

.

The first line means that JIM found ten items and saved $14.50 on each item.

The second line means that MARY found 3 items and saved $116.25 on each item.

 

Since we don’t know how many contestants will be competing each week (some get voted out of the mall), we do not know how many lines there are in the file.

 

Write a C++ program (put your code on the next page) that will do the following:

 

i)  Open the file and read in the data.

 

ii)            Call a value returning function that returns  a float value indicating the total saved by each contestant (items found * amount saved)

 

iii)          Compute the total number of items purchased by all contestants.

 

iv)          Compute the average amount saved by all contestants (remember that there are different numbers of contestants each week).

 

v)    Print the output so that it is organized as below.  (Of course there will be more lines, because this is shown only for the lines above). Note the dollar amounts are printed to two decimal places.  All printing is done in the main function.

 

Contestants     Items    Amount    Total Saved

---------------------------------------------------------

JIM             10        14.50         145.00

MARY            3        116.25         348.75

JASON           100       12.00        1200.00

The average amount saved is:  $564.58

The total number of items is:  113

Press any key to continue

 

PLEASE NOTE THAT IN VISUAL STUDIO THE END OF FILE FLAG IS TRUE ONLY AFTER TRYING TO READ AFTER THE LAST ITEM. THEREFORE, THE PROGRAM BELOW READS THE LAST LINE TWICE.  TO FIX THIS YOU CAN PUT THE READ STATEMENT IN TWICE, ONCE BEFORE THE LOOP AND ONCE INSIDE AT THE END OF THE LOOP, INSTEAD OF ONLY ONCE AT THE START OF THE LOOP.

 

#include <iostream>

#include<fstream>

#include<string>

#include<iomanip>

using namespace std;

 

//function prototype

 

double mult(int, double);

 

//main program

 

int main( )

{

  ifstream infile;

  string name;

  int item;

  double amt;                     //holds the amt saved per item

  double saveAmt;                     //holds the total saved for this person

  double TsaveAmt = 0;         //hold the total saved for everyone

  int count = 0;                      //to count number of lines in the file

  int Titem = 0;               //hold the total items

 

  //open the file and check if it opens

  infile.open("bargains.txt");

 

  if (!infile)

  {

        cout << "Error opening file!";

        return 0;

  }

  //set up output so that doubles are to two decimal places

 

  cout << fixed << setprecision(2);

 

  //put headers onto the output screen

 

  cout << setw(10)<<"Contestant"<< setw(8) << "ITEMS"

        <<setw(10)<<"AMT"<<setw(15)<<"TOTAL SAVED"<<endl;

 

  while (!infile.eof())

  {

        infile >> name >> item >> amt;

 

 

        saveAmt = mult(item, amt);

        cout << setw(10)<<name<< setw(8) <<item

        <<setw(10)<<amt<<setw(15)<<saveAmt<<endl;

        TsaveAmt += saveAmt;

        count++;

        Titem +=item;

              }

 

  cout << "Total items bought with savings: "<<Titem << endl;

  cout << "Avg saved is " << TsaveAmt/count;

 

 

}

 

double mult(int a, double b)

{

  return a * b;

}