Introduction to Computers and Programming: Section1

Midterm 1

October 25, 2000

Instructor Evan Korth

1.  (25 points) Write a program that prompts a user for a nonnegative value n.  The program then displays as its output:

1 2 3 … n-1 n

1 2 3 … n-1

1 2 3

1 2

1

(Note:  The …s are to be filled with the appropriate blanks)

2.  (25 points) Write a program to read in a collection of exam scores ranging in value from 1 to 100.  Your program should count and print the number of outstanding test scores (90-100), the number of satisfactory test scores (60-89), and the number of unsatisfactory test scores (1-59).  You do not know beforehand how many exam scores you will be processing.

3. (20 points) What does the following code produce?

#include <stdio.h>

int main()

{

int ind1 = 0, ind2 = 0;

for(ind1 = 1; ind1 <= 5; ind1++)

{

for(ind2 = ind1; ind2 <= 4; ind2++)

{

if ((ind2 % 2) == 0)

printf("%d", ind2);

}

printf("\n");

}

return 0;

}

4. (15 points) The following code contains at least 5 errors (at least 4 are syntax errors). Identify the line number and the error.

1 #include <stdio.h>

2 #define SENTINEL -1

3 main()

4 {

5        int grade, counter, sum == 0

6        float avg;

7        counter = 0;

8        printf("Enter first grade (-1 when finished): ");

9        scanf("%d", &grade);

10      while(grade != SENTINEL); {

11                sum = sum + grade;           /* add input's value to "sum" */

12                counter+=;    /* add 1 (increment) to counter */

13                printf("Enter next grade (-1 when finished): ");

14                scanf("%d", grade);

15      } /* end while */

16      avg = (float)sum / (float)counter;

17      printf("the average is %f\n", avg);

18      return 0;   /* program terminated successfully */

19 } /* main */

5.  a.  Assuming the following declarations:

int a=4;

int b=23;

What would be the output of the following code segment?

while (a <= b)

{

printf(“%d”, a);

a += 4;

}

b.  What is the value of the following expression?

(19 % 3) + (16 / 5) +  1

c.  How many times does the following loop execute?

total = 0;

for (index = 1; index <= 100; index *= 2)

{

total += index;

}

d.  How many times will the printf statement in the following fragment be executed?

for (a=2; a<=6; a++)

{

for(b=a; b<=5; b++)

{

printf(“*”);

}

}

e.  Would the following expression evaluate to true or false?

(((7 < 9) || (8==16))  && (5>9))