Introduction to Computer Science:
Quiz 6
    Sewickley Academy, 2000-2001

See Course Home Page.

This is a programming quiz.  You must use Visual C++ for this quiz.  This quiz is closed-note:  you may not use anything but your mind and a compiler (starting with a new project).

This quiz has 3 questions.

1. Write a program with the following behavior.  Note that your main() function must call the functions min(), max(), middleNumber(), and power(), which you must define.

Enter 3 numbers:  3 9 -2

The square of the largest number, 9,  is:  81

The smallest number, -2, raised to the middle number, 3, is: -8

Goodbye!

2.  Starting with the program provided below, write the absoluteValue() function so that the program has the following behavior:
This program computes the sum of the
absolute value of all the numbers
in a range.

Enter the lo and hi values of the range: -3 5

The sum of the absolute value of
the numbers from -3 to 5 is 21.

Hit 'Enter' to exit.

Here is the incomplete program:
#include <stdio.h>
#include <iostream.h>

// NOTE:  PUT YOUR absoluteValue() FUNCTION HERE!!!

void main()
{
 int counter, lo, hi, sum;

 cout << "This program computes the sum of the" << endl
   << "absolute value of all the numbers" << endl
   << "in a range." << endl << endl;
 cout << "Enter the lo and hi values of the range: ";

 cin >> lo;
 cin >> hi;

 sum = 0;
 counter = lo;
 while (counter <= hi)
 {
  sum = sum + absoluteValue(counter);
  counter = counter + 1;
 }

 cout << endl << "The sum of the absolute value of " << endl
   << "the numbers from " << lo << " to " << hi
   << " is " << sum << "." << endl << endl;

 cout << endl << "Hit 'Enter' to exit." << endl;
 getchar();
}

3.  Modify the main() function for the program in problem 2 so that it works more gracefully when hi is less than lo.  That is, make it have the following behavior (look carefully at the input numbers, and note that hi is -3, and lo is 5 in this example):
This program computes the sum of the
absolute value of all the numbers
in a range.

Enter the lo and hi values of the range: 5 -3

The sum of the absolute value of
the numbers from -3 to 5 is 21.

Hit 'Enter' to exit.

See Course Home Page.