DISC 3370 MIDTERM EXAM SAMPLE ANSWERS
FALL 1996 PARKS

Midterm Grade frequencies (all exams):

1007
90-996
80-896
70-7915
60-6923
50-5924
40-4930
30-3912
20-296
10-194
0-102

There were four exam versions. Only one of the versions is supplied with answers here. Other exams were similar.

  1. (40 points) Using only the command

    line (int first_horizontal, int first_vertical, int second_horizontal, int second_vertical)

    as your drawing tool, write the C program to create a color 300 pixels wide and 150 pixel tall Finnish flag. The Finnish flag has a white background with a vertical blue stripe and a horizontal blue stripe. The blue vertical and horizontal stripes are centered on the flag. The width of the both stripes is 20 percent of the height. Omit the initgraph elements. Assume a 640 x 480 16 color screen. Locate the flag in the upper left corner of the screen. Use the C function

    setcolor (color_num)

    to change the current drawing color. For blue the color_num is 9 and for white the color_num is 15.

    ANSWER:

    .
    .
    .
    int y,x; setcolor(15): /* set white */
    for (y=0; y<=149; y++) /* from top to bottom */
    line(0,y,299,y); /* paint a line from left to right */
    setcolor(9); /* change to blue */
    for (y=0; y<=149; y++) /* from top to bottom */
    line(185,y,214,y); /* paint a blue stripe in the center */
    for (y=61; y<=91; y++) /* in the vertical middle */
    line(0, y, 299, y); /* paint a blue stripe left to right */

    .
    .
    .

  2. (30 points) Given an array:

    int fred [2000]={23, 13, 12, ...pretend there are 1994 more integers here..., 3, 14, 19};

    All integers in the array fred are less than 56 and all are larger than 0. Write the complete C program to display which number (between 1 and 55) occurs most frequently in the array fred and how many times this integer occurs. If multiple numbers occur most frequently, display each of them.

    ANSWER:

    .
    .
    .
    int howmany[55]={0,0,0, ... ,0,0,0};/* make a place to count the values in fred */
    int y,i; /* some stuff I need */

    for (i=0; i<=1999; i++) /*for each element in john */
    {
    y=fred[i]; /* y sez what fred [i] is */
    howmany[y-1]++; /* count this value */
    }
    max=howmany[0]; /* assume first one is the max */
    for (j=1; j<54; j++) /* examine all counts */
    if(howmany[j]>max) /* if this one is bigger, then */
    max=howmany[j]; /* reset max value found */
    for(j=0; j<=54; j++) /* go thru all counts */
    if (howmany[j]==max) /* is this one is equal to max, then */
    printf("\n %d occured %d times", (j+1), howmany[j]); /* print it...*/
    .
    .
    .
    3. (30 points) Write a complete C program to read and store 123 positive non-zero integers from the user at the keyboard. The program should prevent the user from entering any integer twice. If the user attempts to enter the same value as any previous entry, the program should print the message: TRY IT AGAIN and then continue the process. When the entry process is complete, the program should display: AOK and then terminate.

    <.
    .
    .
    int i;
    char ok;
    int uservalue [123];

    for (i=0; i<=122;i++) /* get exactly 123 values from the user */
    {
    ok="Y"; /* assume the value from the user will be ok */
    do /* start a loop */
    {
    scanf("%d", &uservalue[i]); /* get an integer from the keyboard */
    for (j=0; j<=i; j++) /* compare to all prvious entries */
    if(uservalue[i]=uservalue[j]) ok="N"; /* flag if an equal found */
    if(uservalue{i]<1)ok="N"; /* check for 0 or negative */
    if(ok=="N") printf("\n TRY IT AGAIN"); /* print if a problem */
    } while (ok=="N"); /* if not ok redo it */
    } /* end of user for loop */
    printf("\nAOK"); /* last message */
    .
    .
    .