Computer Science 151 -- Programming Fundamentals
Midterm Exam

Name:__________________________

SSN:___________________________
 

The number of points for each problem is listed in curly braces ({points}) after each problem.

Hint:  Don't make things to difficult for yourself, if you are spending a great deal of time on a problem, than chances are that you have not noticed a KEY point of the problem, and READ the problem before attempting to solve it.  If you answer a similar question to the one asked, you will not be given credit.
 

Use the following program for questions 1-4:

#include <iostream>
#include <string>
using namespace std;

void input(string & name, int & num, int & id);
void output(string name, int num, int id);

int main() {
  string userName = "Nobody";
  int idNumber = 0;
  int employeeSalary = 0;
  int employeeNumber = 0;

  // get the user's input for the variables...
  input(userName, employeeNumber, idNumber, employeeSalary);

  // output what was read in.
  output(userName, employeeNumber, idNumber, employeeSalary);

  return 0;
}
 
 
 
 
 
 

void input(string & name, int & num, int & id) {
  char ch;
  // Read in each entry, if this is a newline, ignore the variable
  // (leave unchanged).

     // We need to remember that since we are not using cin >>,
  // we need to flush the stream.
  cout << "Name: " << flush;
  // since we have to watch out for the one character signal,
  // that we don't want to change a value, the newline, read one
  // character...  only read more if the value is not a newline.
  ch = cin.get();
  if (ch != '\n') {
    // if the value is not a newline, then put the character back
    // so we can read it as the full value for name.
    cin.putback(ch);
    getline(cin, name)
  }
  // because getline gets the newline, we do not need to worry
  // about stray newlines.

  cout << "Employee Number: ";
  ch = cin.get();
  if (ch != '\n') {
    cin.putback(ch);
    cin >> num;
    cin.get(); // >> leaves '\n's on the input stream,
               // so get rid of it.
  }

  cout << "Employee Id: ";
  ch = cin.get();
  if (ch != '\n') {
    cin.putback(ch);
    cin >> id;
    cin.get(); // >> leaves '\n's on the input stream,
               // so get rid of it.
}

void output(string name, int num, int id) {
  cout << "The user's name was: " << userName << endl;
  cout << "The user's number is: " << employeeNumber << endl;
  cout << "The id number the company uses to track the employee is: "
       << idNumber << endl;
  cout << "The employee's salary is: $" << employeeSalary << endl;
}
 

  1. Tell me what is wrong with the above program.  Assume that the spec states that the program should take a name, salary, employee tracking number, and an employee id number, taking newlines as meaning that the user does not want to change the default values, and output that information.

  2. (Will it compile, if no, why not.  Tell me everything that is wrong with the program, how it does not meet the spec, or has syntax errors...anything).  To ensure that I know what you are talking about, you should also indicate on the program where the errors are.  {20}
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  3. Fix the above program...  After repair, it should implement the spec, and compile. You should write only those parts that need to be changed, or are needed to show me where the changes need to be made. {20}

  4.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

  5. Draw a flow chart for the above program (assume that the program does what it is supposed to, and compiles).  This should indicate not only that a function was called, but what the function does as well.  {15}

  6.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

  7. Write an algorithm for the above program. (An algorithm is a translation to english of what the program is doing, but may include pseudo-code, or even code to ensure complete understanding). {15}

  8.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

  9. Rewrite all of the following lines of text (assuming that you are working on a UNIX terminal, as we did in class, and assume that non-visible means that you see no character on the screen, though the cursor may be moved by the non-visible character) such that all typical non-visible characters are shown: {5}

  10.  

     
     
     
     
     
     
     
     
     

    Name: David Samson
    ID: Tom
    Title: Programmer
    Salary: $45000

    Would you like to modify this entry?
    yes

    Name:
    ID:
    Title:
    Salary: $36000
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

  11. Write a program to sum all of the numbers [1,50], this is set notation meaning the range including 1, the numbers between 1 and 50 and 50.  It should return the sum to the caller. {10}

  12.  

     

    #include <iostream>
    #include <string>
    using namespace std;

    int main() {
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

      return 0;
    }
     
     

  13. Write an algorithm to sum some arbitrary collection of numbers, as this is just an algorithm remember that you do not have to know where you are getting the numbers from, only that there will be a collection of numbers.  { 15 }

  14.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

  15. Refine the above algorithm so that it uses psuedo-code, i.e. english that looks similar to code that translates into 5 lines or less of code, per english line.  (you may use symbols, and even code fragments, or almost code fragments, as done in class)  This should look more like code than the algorithm above. { 5 extra credit }

  16.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

  17. Write a function that takes two arguments, a starting number and an ending number and sums all numbers beginning with the starting number and ending with the ending number, and returns the sum. {10 extra credit}

  18.