60-140-01         Fall 2009         Final Exam Preparation

 

Final Exam Date

(covers materials from chs. 1 – 9), lecture slides 1 – 225.

Minus binary search and sorting techniques and records.

Saturday, Dec. 12, 2009

Time:                      8:30 – 11:30am.

Where:                 ED 1101

                       (for section 60-140-01)

Tentative Exam Viewing Date

Monday,  Dec. 14 (10:00am – 4:00pm in ER 2139)

 

 

***    GOOD  LUCK   **** 

 

NOTE:  In the event you cannot write the exam due to illness or some other emergency (medical note required for proof), call the CS office or send email to me as soon as possible to inform.  There is only one make-up exam for all, who could not write final and that is on Friday, Jan. 8, 2010 (1:00 – 4:00pm in ER 1115).

 

We hope to mount exam viewing sessions on Monday, Dec. 14 (10:00am – 4:00pm in ER 2139) subject to we, completing the marking.  Viewing is as scheduled unless otherwise announced).

 

Student Name and id:________________________________________________

These questions are to help with preparing for final exams.  The questions in the final exam practice on pp. 243 of text are also helpful.  To prepare well, read through all of the lecture slides and the notes you have been writing to ensure you feel comfortable with the materials taught in the course.  Refer to the text part of the book when you want more information about some concepts in the lecture slides.  Chapter 10 also provides a quick reference collection you can use to refresh your memory before the exams. The key to success is good preparation and confidence.

Format of the final Exam: 7 short program/segment questions in section A and 1 full multi-function question with arrays and parameters in section B.

Que 1. Management wants to rank the four weeks in each month based on the patterns of attendance in its 5 departments using attendance data collected weekly for 4 weeks. The attendance (0 to 10) of each department is the same as the number of its employee attendance since each department has only 10 employees. The number of employees in attendance in each department every week for 4 weeks is read.   Your program should calculate:

1.        The average attendance achieved each week (0 to 10).

2.        The attendance rank of each week based on the condition that attendance average that is less than 7 is of rank low, average that is between 7 and 8 is of rank medium while average attendance that is greater than 8 is of rank high.

3.        Print the week with the lowest average and rank as well as its average.

Provide C program solution using top-down design approach.  Strictly use two-dimensional and one-dimensional arrays.

 

 

 

 

Sample input and output data:

 

                Dep1  Dep2           Dep3       Dep4       Dep5

 

Wk1        10            5              4              10            10           

Wk2        8              10            5              9              5

Wk3        10            7              8              1              5             

Wk4        10            9              6              7              9             

 

Sample Output is:

                Dep1  Dep2           Dep3       Dep4       Dep5       Weekly Average  Weekly Attend Rank

Wk1        10            5              4              10            10                            7.8                                           medium

Wk2        8              10            5              9              5                              7.4                                           medium

Wk3        10            7              8              1              5                              6.2                                           low

Wk4        10            9              6              7              9                              8.2                                           high

 

Week 3 has lowest attendance of  6.2

 

Solution to Question 1:

 

#include <stdio.h>

#include <string.h>

 

/*declare function prototypes*/

void ReadD (int [][5], int, int);

void Calc(int [][5], float [], char [][10], float *, int *);

void printres(int [][5], float [], char [][10], float *, int *);

 

void main () {

  int attend[4][5], numwk=4, numdept=5, lowestwk;

  float weekave[5], lowestavg;

  char  weekrank[4][10];

 

  ReadD (attend, numwk, numdept);

  Calc(attend, weekave, weekrank, &lowestavg, &lowestwk);

  printres(attend, weekave, weekrank, &lowestavg, &lowestwk);

}

 

/*read data into array of attend and numdemp*/

void ReadD (int attend[][5], int numwk, int numdept)

{

  int r, c;

 

for (r=0; r<numwk; r++) {

     printf("Type  5 weekly attendance for 5 depts for week %d and hit enter\n", r);

    for (c=0; c<5; c++){

      scanf ("%d", &attend[r][c]);}

 }

 

}

 

/*calculate rank averages and rating of each week */

void Calc(int attend[][5], float weekave[], char weekrank[][10], float *lowestavg, int *lowestwk)

{

  int r, c, wksum[4]={0,0,0,0};

 

    for (r = 0; r< 4; r++)

     {

      for (c = 0; c<5; c++)

       {

            wksum[r] += attend[r][c];

         }       

      weekave[r] = (float) wksum[r]/5;

      if (weekave[r] < 7.0) strcpy(weekrank[r], "low");

            else if (weekave[r] < 8.0) strcpy(weekrank[r], "medium");

                  else strcpy(weekrank[r], "high");  

       

      /* compute the lowest average and week  */

      if (r == 0) *lowestavg = weekave[r];

            else if (weekave[r] < *lowestavg)

                  {

                        *lowestavg = weekave[r];

                        *lowestwk = r;

                  }

      }

   }

 

/*column calculation*/

void printres(int attend[][5], float weekave[], char weekrank[][10], float *lowestavg, int *lowestwk)

{

  int r, c;

      printf("\t Dep1 \t Dep2 \t Dep3 \t Dep4 \t Dep5 \t Week Average \t Week Rank \n");

      for (r=0; r<4; r++)

      {

            printf("Wk%d \t",r+1);

            for (c = 0; c< 5; c++)

            {

                  printf("%d\t ", attend[r][c]);

            }

      printf("%0.2f\t %s \n", weekave[r], weekrank[r]);

      }

      printf("Week  %d has the lowest attendance of %0.2f \n", *lowestwk+1, *lowestavg);

 

}  /* end of printres */

 

 

 

Que. 2.  Write a short program that takes two 5-digit integers and prints the five pairs of single digits belonging to these two numbers starting with the leftmost (or most significant) digits.

 

 

Que 3: What is printed by the following switch_case instruction?

 

   int code = 2;

                switch (code) {

                case 1 : printf(“First”);  

                case 2 : printf(“Second”);

                case 3 : printf(“Third”);      

                default : printf(“All”);

                }

 

Solution:  SecondThirdAll

This is because it does not break once it executes a switch case that matches.

 

 

Que. 4.  The program below should read a list of ages and compute the maximum age.  The integer number 0 is a sentinel.  There are some logic errors (three of them), which cause the algorithm to work incorrectly.  Correct these errors.

 

 

#include <stdio.h>

void main(void)

{

float Age, Maxi;

 

scanf (“%f”, &Age);

Maxi = Age;

while (Age = 0)                                                     change to while (Age != 0)                

                {

                   if (Age > Maxi)

                                Age = Maxi;                                          change to then Maxi = Age;

                                                                                                insert     scanf (“%f”, &Age);

                                               

}

     printf ("Maximum age is: %f", Maxi);

}

 

Solution:  Answers already marked on the right sides of the lines with errors. Remember to solve event control problems by ensuring that all of their initialization, testing for termination, loop body processing with updating of control variable are properly done.

 

Que. 5. Write a short program to read  lines of data for each student consisting of studid, name, major, gpa from a disk data file (called stnrec.txt) until end of file (and not from a key board) and print to the monitor, lines of data read, number of data records read as well as the minimum of all gpas read.

 

Solution:

#include <stdio.h>

        void main (void)

        {       FILE *stnptr;                                       /* 1. must declare file pointer variable   */

        char studentid[15], name[15], major[20];

        float gpa, mingpa;

                int k;

 

                stnptr=fopen("stnrec.txt","r");          /* 2. must associate file pointer variable with                                                                                                                 a disk file and open for read   */

                k = 0;

                fscanf (stnptr, "%s %s %s %f", studentid, name, major, &gpa);  

                                                                                /*3. must read from the file pointer variable  */

                while (!feof(stnptr))   /* test for termination must indicate the file pointer  */

                 {

                 printf ("%s \t %s \t %s \t %0.2f \n", studentid, name, major, gpa);

 

                        if (k == 0) mingpa = gpa;

                                else if (gpa < mingpa) mingpa = gpa;

 

                        k++;

                        fscanf (stnptr, "%s %s %s %f", studentid, name, major, &gpa); 

                                                                                /*3. continuity read from file  */

 

                 }      /* end of while */

 

                printf("Number of records in file is %d\n", k);

               printf("The minimum gpa is: %0.2f\n", mingpa);

       fclose(stnprt);                                /* 4. must close the file   */

 

        }       /* end of program */

 

 

Que. 6.  The given table represents a multiplication table.  Write a program to produce it using looping, without reading any data from the keyboard or file.

 

               

                X             5              7              9              11

                1              5              7              9              11

                2              10            14            18            22

                3              15            21            27            33

                4              20            28            36            44

 

 

Que. 7. Given the following list, write a short program to search this list for a 91 and which prints the positon of the search key element if found and a message indicating it is not there if not found. The input list is:           80 34 68 91 52.

 

 

Que. 8.  Given the following program segments, write two program segments to say the same thing, using (a) do-while instruction, and (b) “for loop” instruction (c) show your tracing of these program segments.

 

N = 0;

while  ( N <= 10 )

{

                N ++;

                printf(“%d”, N);

}

**********

A = 5;

while  (A >= 1)

{

                B = 1;

  while   (B >= 1)

                {             

                                printf (“%d”, B);

                                                B--;

                                }

                                printf (“\n”);         

                                A--;

                }

 

9. Write a program that will take one integer number followed by an operator (% /), then followed by a second number.  Your program should read the data and perform the calculation and print the equation with result.  Eg. If the input data is 10 / 4, output is

10 / 4 = 2.

 

 

 

10. Given three student names in the order lastname firstname, write a C program to read these names and store them in an array and print them nicely in a table.

 

 

11. Write a short program that prints the following pattern using a double nested loop.

 

*

*  *   *

                                                                    *  *  *   *  *

                                                                *  *  *  *   *  *  *

Solution:

 

#include <stdio.h>

void main(void)

{

        int  row, col, nspace, nstar;

        for (row = 1; row <= 4; row++)

          {

                Nspace =  4 – row;

                Nstar =  (2  * row) – 1;

                for (col = 1; col <= nspace; col ++)

                {

                                printf (“ “);

                }

                for (col = 1; col <= nstar; col++)

                {

                                printf (“*”);

                }

                printf (“\n”);

        }

}

 

12. Given an array of 5 integer scores, write a C program to read these scores and print both the input array,  the minimum score and the maximum score.

 

Solution:

#include <stdio.h>
void main(void)
{
        /* The simple program that finds reads an array of 5 scores and
           prints the minimum and maximum scores.
           Also, print the result with input data in a table */

        int mark[5], mini, maxi, knt;
        /* initialization, testing                      */
        knt = 0;
        while(knt < 5)
        {
                printf("type the %d mark \t:", knt);
                scanf("%d", &mark[knt]);
                if (knt == 0)
                        {
                        mini = mark[0];  /* set the first as minimum */
                        maxi = mark[0];  /* set the first as maximum*/
                        }
                if (mark[knt] < mini)
                        mini = mark[knt];
                        else if (mark[knt] > maxi)
                                maxi = mark[knt];
                knt++;
        }
     /* This now prints the input data and result  */
        printf("Mark \n");
        for (knt = 0; knt < 5; knt++)
        {
                printf("%d \n", mark[knt]);
        }
        printf("minimum = %d \n", mini);
        printf("maximum = %d \n", maxi);
}

13. Given three student names in the order lastname firstname, write a C program to read these names a into an array and copy these names into a second array and print the two string arrays where the first student name in array1 is printed on the same line with the names of the last student in the second array2, etc.  nicely in a table.  E.g., if input data are: Adams John, Cooks Mary, Black Tony, the output should look like:

FirstName1 LastName1       FirstName2            LastName2

John                       Adams                   Tony                      Black

Mary                      Cooks                     Mary                      Cooks

Tony                      Black                      John                       Adams.

 

Solution:

#include <stdio.h>
#include <string.h>

void main(void)
{
                /* The simple program that reads three student names
                 as lastname firstname into a pair of arrays, called
                firstName1, lastName1 and copies the names into a
                second pair of arrays called  firstName2, lastName2,
                to print the first Names in forward order while the
                 second names are printed in reverse order all in  a table */

        int     r;
        char    firstname1[3][10], lastname1[3][10];
        char    firstname2[3][10], lastname2[3][10];
       for (r= 0; r< 3; r++)
                {
                    printf("please, type the %d lastname firstname of input\n", r);
                    scanf("%s %s", lastname1[r], firstname1[r]);
                    /* Now copy into second arrays  */
                    strcpy(lastname2[r], lastname1[r]);
                    strcpy(firstname2[r], firstname1[r]);
                }
        printf("FirstName1 \t LastName1  \t FirstName2 \t  LastName2\n");
        printf("**********************************************\n");
       /* Now print the table of results   */
       for (r=0; r<3; r++)
                 {
                printf("%s \t \t %s \t\t %s \t\t %s\n", firstname1[r], lastname1[r], firstname1[2-r], lastname1[2-r]);
                }
}

 

**

 

We have covered and should know:

Chs 1 – 3: Assignment Instructions,

                scanf and printf instructions with data types,

Ch. 4:      Functions with function prototypes, function calls with actual parameters,

                function definitions with formal parameters.

                Parameters can be either call-by-value or call-by-reference.

                Results computed by a function can be sent back through “return” instruction

                or changed directly using call-by-reference parameter. 

                A function can use its formal parameters, local variables or global variables. 

Ch. 5:      We have covered program logic structures sequential, decision, repetition.

Ch. 6:      Decision instructions (if, if-else and

                nested if-else instructions

-          negative if logic (as in instructions of the format: if …if … else …else),                                           

-          and positive if logic (as in instructions of the format: if … else if … else …),

                                -   as well as switch-case instructions.

Ch. 7:      we covered repetition instructions (while, do-while, for and their nesting).
Ch. 8:  brought coverage of  array data structure (1-dimensional and 2-dimensional arrays   as well as string processing, sequential searching technique). 
Ch. 9: we talked of reading from data files and use of pointers.