Introduction to Computers and Programming: Section1

Midterm 1

October 24, 2000

Instructor: Avi Rosenfeld

1. (25 points) Write a program to read any 5 digit number.  Your program must display the number with the values of the 2nd digit flipped with that of the 4th.  For example, if the user enters the number 12345, the program should display 14325.

2. (25 points) Write a program to input 100 (and exactly 100) numbers, and display the highest and lowest value among the numbers entered.

3. (20 points) Write a program to draw a right triangle out of stars of any size.  The program should prompt the user for a size for the triangle, and print accordingly.  The following is a sample of the properly working program:

4. (15 points) There are at least 5 errors in the code below.  Comments, formatting or lack thereof are not any of the errors. Identify the line numbers and the errors.

1. #include <stdio.h>

/* The following program converts a number that the user enters from Celcius to Fahrenheit (decimals allowed).  The correct formula is: F = 9/5 C + 32.  If the user enters a value less than 0, the program should print, "Error". */

2. int main()

3. {

4.         float Celcius;

5.         printf("Please enter a value in Celcius ");

6.         scanf("%c",Celcius);

7.         if (Celcius < 0);

8.                     printf(Error);

9.         else

10.                    printf("%c degrees Celcius is %c degrees fahrenheit", Celcius, 9/5 Celcius + 32);

11.}      return 0;

5. (15 points total – 3 points each)

a. What is the value after the following statements?

int x  = 5;

x -= 1+2 * 3 % 2;  Answer:_____

b. If x = 1 and y = 3, what does the following print?

printf("%d", 2 * x++ + ++y + 1); Answer: ______

c. If a = 0 and b = 1, does the following expression in parentheses evaluate to true or false?
( !a && b )  Answer: ______

d. What will the following print:

int x = 1, y = 2;

if (x=2);

printf("stage 1");

if (x == y)

printf("stage 2");

e. What will the following print?

int x = 90;

switch(x)

{

case 100:

printf("Here ");

case 90:

printf("I ");

case 80:

printf("am ");

break;

default:

printf("not! ");

}