Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSC 2100: Introduction to Problem Solving and Computer Programming Fall 2011, Test 1, September 23, 2011 Time: 55 minutes 100 Points Name ______________________ 1. [10 Points] Convert the following while loop to a for loop int x = 5; while ( x < 50 ) { cout << x; x++; } for (x = 5; x < 50; x++) cout << x; 2. [10 Points] Convert a following for loop to a while loop for (x = 50; x > 5; x--) cout << x; x = 5; while ( x > 50) { cout << x; x--; } 3. [10 Points]What would be the output of the following code segment? int balance = 29; while ( 5 ) { if (balance < 9) Break; balance = balance – 9; } cout << balance; 2 4. [10 Points] Given the following function definition void calc (int a, int& b) { int c; c = a + 2; a = a * 3; b = c + a; } What is the output of the following code fragment that invokes calc? (All variables are of type int) x = 1; y = 2; z = 3; calc(x, y); cout << x << " " << y << " " << z << endl; 1 6 3 5. [10 Points] Write program to display miles to kilometer conversion table (1 miles = 1.609 kilometer) as shown below for up to 200 miles. You must use for loop for this program. Miles Kilometer 1 1.609 2 3.218 ... …….. #include <iostream> using namespace std; int main() { int mile; cout << setw(10) << “Mile” << setw(10) << “Kilometer”; cout << fixed << setprecision(3); for (mile = 1; mile <201; < mile ++) cout << setw(10) << Mile << setw(10) << mile*1.609; } 6. [10 Points] Write a program that will read an unspecified numbers of integers from keyboard, determine how many even and how many odd numbers have been read. The program should also compute the average of the integers read. The program should display the number of odd integers, the number of even integers; and the average. Your program should stop when user enters 0. #include <iostream> using namespace std; int main() { int num, even=0, odd=0; double total=0, avg=0; while(true) { cout << “Enter an integer, 0 to quit:”; cin >> num; if(num == 0) break; else if(num % 2 == 0) even++; else odd++; total = total + num; } if((even+odd) != 0) avg = total / (even + odd); cout << “Even: “ << even << “ odd: “ << odd << “ Average: “ << avg << endl; } 7. [10 Points] Write a function which will receive an integer as input and will print a inverse triangle pattern. The input indicates how many lines will there be in the inverse triangle. For example if 7 is passed to the function it will print the following pattern. 1234567 123456 12345 1234 123 12 1 void inverseTriangle (int x) { int i; j; for( i = 0; i < x; i++) { for( j = 1; j <= (x - i); j++) cout << j << “ “; cout << endl; } 8. [10 Points] Write a function that computes the sum of the digits of an integer. The prototype of the function is int sumDigit(int). For example, sumDigit(521) returns 5+2+1 = 8. Hint: use % operator to extract digits and the / operator to remove the extracted digit. Use a loop to repeatedly extract and remove the digits until all the digits are extracted. int sumDigit (int x) { int answer = 0; while (x !=0) { answer = answer + x % 10; x = x/10; } return answer; } \