![]() |
CSC 215 |
|
Sample midterm questions |
Listed below are questions similar to those I will put on the midterm. Sample solutions will be posted Thursday.
double average(double a, double b)that returns the average of a and b.
int minimum(int i, int j)that returns the smaller of i and j.
double convert(double Britishpounds)where Britishpounds contains an amount in British pounds sterling; the function should return what that amount is in US dollars. The conversion rate is 0.66 pounds per dollar.
int sum(int i, int n)that returns the sum of the integers from i to n.
int getMonth()that prints the prompt "Please enter a month: " and then accepts only a value between 1 and 12. It should work by looping until the user enters a legal value. It then returns the user's answer.
// // // #include <iostream.h> int square(int n) { int answer; answer = n * n; return answer; } int main() { int index; int limit = 5; int answer = 0; for (index = 1; index <= limit; index++) { cout << index << ", " << square(index) << endl; answer = answer + square(index); } cout << answer << endl; return 0; }
// // // #include <iostream.h> int main() { int value = 32; while (value > 1) { cout << value << endl; value = value / 2; } return 0; }