CSC 126                                    FINAL EXAMINATION                  FINAL Fall 2008 A


Name (last, First) ____________________________   __________________________







Question #

Total Possible

Total Received


































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.




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


____1. gigabyte


____2. compiler


____3. secondary storage


____4. CPU


____5. syntax


____6. main memory


____7. algorithm


____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!" 


















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



int w = 7;

while (w <= 5)

  w = w – 2;

cout << w << endl;















int  a,b;

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


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

       cout << “**” << b;

    cout << ‘&’ << endl;  















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;











alpha  = 4;



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


case 2: alpha = alpha + 10;


case 4: alpha++;


case 5: alpha = 2 * alpha +1;


case 6: alpha = alpha - 3;


default: alpha--;



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



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









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;








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";


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]? ______________________



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



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



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



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.


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.






Example call:




Function definition (header and body):


#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;





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?




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







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