Midterm Self--Check Exercises

(not graded...just for practice!)


CS 161: Introduction to Computer Science 1




Self-Check Exercises


1. Write an algorithm that allows for the input of an integer value, doubles it, subtracts 10, and displays the result.


Display a message telling the purpose of the program

Prompt, read, and echo an integer value

Perform the calculations to double (multiple it by 2)

Perform the calculation to subtract 10 from the conoents of it

Display the result

Display a termination message


2. List three basic data types in C++


         int, float, char


3. When reading in two characters using the extraction operator (>>), does it matter how many blanks (if any) appear before the first character? Or, between the first and second characters? Or, after the second character?


            No, any number of whitespace characters may be typed in – and they are



4. Write a nested if statement to display a message indicating the educational level of a student based on the number of years of schooling (0 is none, 1-6 is elementary, 7-8 is middle school, 9-12 is high school, greater than 12 is college). Display a message to indicate if the data is bad, as well.


            if (level < 0)

                  cout <<”Invalid data has beeen supplied” <<endl;

else if (level == 0)

                  cout <<”No education level” <<endl;

         else if ( level <= 6)

                  cout <<”An elementary education level” <<endl;

         else if (level <= 8)

                  cout <<”A middle school education level” <<endl;

         else if (level <= 12)

                  cout <<”A high school education level” <<endl;

         else    cout <<”A college education level” <<endl;


5. Write if/else statements, given these variables:

     int number;

     char letter;


a)     Display the message telling whether or not number is positive


            if (number >= 0)

                  cout <<”Number is positive” <<endl;

         else    cout <<”Number is negative” <<endl;



b)    Display a message telling whether or not letter follows ‘M’ in the alphabet


            if (letter > ‘M’ && letter <= ‘Z’)

                  cout <<”Yes, it follows the letter ‘M’ in the alphabet” <<endl;

         else    cout <<”No, it does not” <<endl;


c) Display a message telling whether or not number is evenly divisible by three


            if (number % 3 == 0)

                  cout <<”Yes, it is divisible by three <<endl;

         else    cout <<”No, it is not” <<endl;


6. What is the most significant difference between a while and a do-while loop?


            A while loop may execute zero times and a do-while always executes at least once – because the test condition of a while loop is at the beginning of the body and the do-while has the test condition at the end of the body.



7. Write a while loop that prompts a user for a grade between 0 and 100 inclusive and continues to repeat the prompt until a valid entry is provided.


            int grade;

         cout <<”Please enter a grade (0-100)”;

         cin >>grade;

         while (grade < 0  || grade > 100)


                  cout <<”Please enter a grade (0-100)”;

                  cin >>grade;



8. Now, rewrite the answer to question #7 using a do-while loop


            int grade;



                  cout <<”Please enter a grade (0-100)”;

                  cin >>grade;

         } while (grade < 0 || grade > 100);



9. Write a program to sum and print a collection of payroll amounts entered at the terminal until a sentinel value of -1 is entered. Display the sum total at the end.


         float payroll;

         float total = 0;


         do {

                  cout <<”Enter a payroll amount: “;

                  cin >>payroll;

                  if (payroll != -1)

                           total += payroll;

         } while (payroll != -1);


         cout <<”Total payroll is: “ <<total <<endl;


10. What changes would you make to your answer of question #9 if you wanted to calculate the average pay?


            Keep an integer variable – counting the number of payroll values entered, and then divide the total by this value.


11. Write a program to find the largest, smallest, and average of a n whole numbers. You can assume that “n” has already been set by the user.



int largest, smallest;

int value;

float total;


         cout <<”Please enter a whole number: “;

         cin >>value;

         largest = smallest = total = value;


         for (int j = 0; j < n ; ++j) {

                  cout <<”Please enter another whole number: “;

                  cin >> value;

                  total += value;

                  if (value > largest)  largest = value;

                  if (value < smallest) smallest = value;



         cout <<”The largest value is: “ <<largest <<endl;

         cout <<”The smallest value is: “ <<smallest <<endl;

         cout <<”The average is: “ <<total/n <<endl;


12. Write a while loop that displays all the even number from start to end


n     assuming that start is even…--


for (j = start; j <= end; j += 2)

         cout <<j <<endl;


13. Write a program that reads in two integers at a time, and outputs their sum, difference, and product.


int value1, value2;


         cout <<”Please enter two numbers “;

         cin >>value1 >>value2;


         cout <<”The sum of these values is: “ <<value1+value2 <<endl;

         cout <<”The difference is: “ <<value1-value2 <<endl;

         cout <<”The product is: “ <<value1 * value2 <<endl;