Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam  (120 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: May 10, 2005

 


No questions will be answered during this examination.  If you do not understand a question, read it again.  If you still do not understand it, make reasonable assumptions and write them down. (Points will be deducted if unreasonable assumptions are made.)
DO NOT CHEAT: Cheating includes not only copying from another person but also allowing someone to copy from you.  Anyone who copies or allows someone to copy will receive a score of zero.  So be defensive and protect your work.

 

This examination contains 12 pages including this page. Check that your copy is complete, and ask for a replacement if it is not. Do all your work on these pages. For your own protection, in case pages come apart, write your NetID at the TOP of each page before beginning work.

The number of points for a question is roughly proportional to the amount of time you may need for it. Don’t spend too much time on any one question.

Do not forget to sign the attendance list and to write your signature on the line below:

 

_______________________________________________________________________
(If your exam is misplaced and you did not sign the attendance list then you will receive a zero score for the exam.)

You may not use any electronic devices, book, notes or other references during this examination.

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Score

Grader

1

15

 

 

2

15

 

 

3

24

 

 

4

24

 

 

5

14

 

 

6

12

 

 

7

18

 

 

8

16

 

 

9

12

 

 

10

12

 

 

11

12

 

 

12

15

 

 

13

17

 

 

14

12

 

 

15

12

 

 

16

20

 

 

Total

250

 

 

A

 

 

 

 

 

1.      The following program compiles and runs without errors. Write the output of the program.

 

#include <stdio.h>

 

void main (void) {

   int x[5] = {1, 2, 3, 4, 5};

   int i, *p1, *p2;

   p1 = x;

   p2 = &p1[2];

   *(p2+1) = 2;

   p2[2] = 1;

   p1[0] = 0;

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

     printf("%d ", x[i]);

}

 

 

 

 

 

 

2.      a) Write C code to define a new data type named country, a structure to store information about countries, which has the following fields(members):

 

 - name (string, maximum length 50 characters);

 - area (floating point number);

 - population (integer number);

 




 





b) Given the structure definition from a), suppose you have a variable x declared with data type country as follows:

 

country  x;

 

Complete the code fragment below to read in values into the corresponding fields of the variable x .

 

printf("Enter country name: ");

scanf("%s", ______________________);

printf("Enter its area: ");

scanf("%f", ______________________);

printf("Enter its population: ");

scanf("%i", ______________________);

The next two questions are based on the following structure definitions:

typedef struct {

  char first[15]; /* student's first name */

  char last[15];  /* student's last name */

} student;

 

typedef struct {

  char name[20];

  student roster[100]; /* max class size is 100 students */

  int count; /* actual number of students in a class */

} class;




3.      Fill in the blanks in the program below, which asks the user for a class name and searches the array classes for  a class of that name. If a match to the name is found, the program  prints the names of all the students in that class in the form “Last name, First name”, one student per line. The code for the readClasses function isn’t shown. Do not write code for this function.

 

#include <stdio.h>

#include <string.h>

#include "class.h" /*contains structure definitions (see above) */

 

/* prototype */

int readClasses(class classes[], int max_classes);

 

void main(void)

{

 int i, j;

 char temp[20];

 class classes[200];

 int num_classes; /* actual number of classes */

      /* code to read the data into the classes array */

 num_classes = readClasses(classes, 200);
 
 printf("Enter class name: ");


 scanf("%s" , ___________________________);


 for(i = 0; i < num_classes; i++) {


   if(strcmp(_____________________, _______________________) == 0){



      for(_______________; _____________________; __________________)


          printf("%s,%s\n",______________________,_______________________);

     } /* end of if */

    }

   }/* end of main */

 

 

 

4.      Complete the program by filling in the blanks in main and by writing the code for the compare function.  The call to qsort should sort the classes by the name of the class in descending order ( Z before A) using the compare function. The code for the readClasses and printClasses functions aren’t shown. Do not write code for these functions.

 

#include <stdio.h>
#include "class.h" /*contains structure definitions (see above) */


/* prototypes */

int readClasses(class [], int);

void printClasses(class [], int);

 

void main(void) {

  class classes[200];

     int num_classes; /* actual number of classes */

      /* code to read the data into the classes array */

  num_classes = readClasses(classes, 200);

  qsort( _____________ , _____________ , _____________ , ______________);

 

  printClasses(classes, num_classes);

}

 


      /* fill in the two blanks and then write the code for the compare function */

int compare( _________________ , ________________) {


/* write your code for the compare function here */

/* sort by name of class in descending order */
/* don’t forget to declare any variables you may need to use */


 

 




 

 

 

 




 

 

 

 

 

 

 

 


} /* end of compare */

The next three questions are based on the following structure definition:

typedef struct {

    int midterm;

    int final;

    char grade[3];

} CS101;

 

  1. The following program reads CS101 grades for students from a text file named 'grades.txt' into an array of data type CS101. Each line of the file consists of three items separated by spaces in this order: midterm, final, grade. The structure field(member) 'grade' is a string with no spaces and can be either 1 or 2 characters long. Here is an example content of the file:

93  180  B+

102  230 A

30  130  C-

The program will stop reading either when the end of the file has been reached, or when 10 records have been read, whichever comes first. Fill in the blanks to complete the program.

 

            #include <stdio.h>

     void main(void) {

 

       int count = 0;

       CS101 grades[10]; /* the array */    

    

       /* Declare a FILE pointer */

 

       __________________________________________________

 

       /* Open the file "grades.txt" for reading */

 

       __________________________________________________

 

       /* We assume that the file open operation will always

          succeed, so start reading data from the file. */

 

       while(EOF != fscanf( ________ , ____________________ , 

 

   __________________________________________________

 

   __________________________________________________  )) {

           count++;

           if(count >= 10) break;

       }

 

       /* Close the file */

 

       _______________________________________

}

 

 

 

 

 

 

 

  1. Given that an array named grades of data type CS101 has been created as follows:

CS101 grades[10];

 

            Assume that all 10 elements have been initialized with data. Answer the following questions:

 

a)      Write a single line of of code to change the 'grade' of the last element of the array to "B+"

 

________________________________________________________

 

b)      Write a single line of code to copy all the information (all three fields(members)) of the second element of the array to the first element of the array.

 

________________________________________________________

 

c)      Write a single line of code to change the 'final' field (member) of the third element of the array to 200.

 

________________________________________________________

 

 

 

 

  1. Given that an array named grades of data type CS101 has been created as follows:

CS101 grades[10];

 

            We would like to sort the array by the scores in the final field(member) in descending order using the function qsort.

 

a)      Fill in the blanks and then complete the following comparison function (for use with qsort):

 

int compare_grades( ________________ , _________________ ) {

 

/* write your code here */

 

 

 

 

 

 

 

 

 

 

 

}

 

b)      Assuming that all ten elements of the array named grades have been initialized with data, fill in the blanks to call the qsort function in order to sort the array  grades using the comparison function named compare_grades defined above:

 

qsort( _____________________ , _____________________ ,

 

       _____________________ , _______________________ );

  1. Given the following structure definition:

typedef struct {

   double x;

   double y;

} vector;

 

            Complete the function swap_xy below that swaps the values of x and y of a given vector. The input of the function is a pointer to a vector structure (as defined above). The following code fragment would produce the output "3.43  5.62":

 

           vector v = { 5.62, 3.43 };

           swap_xy(&v);

           printf("%.2lf  %.2lf \n", v.x, v.y);

 

     void swap_xy(vector * ptr) {

 

     /* write your code here */

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

     }

 

 

 

 

 

 

9.      Complete the code below to write "Hello World" to the file "output.dat".

 

#include <stdio.h>

 

void main(void) {

      FILE* fileptr = fopen(______________________, ___________________);

 

      fprintf(__________________, "Hello World");

 

      fclose(__________________);

}

 

 

 

10.  The following program compiles and runs without error.  Write the  output of the program.

 

#include <stdio.h>

 

/* Globals */

int x;

int y;

 

void f(void){

   int x = 2;

   y = 2; /* Not declared here */

}

 

void main(void){

  x = 1;

  y = 1;

  f();

  printf("x is %i\n", x);

  printf("y is %i\n", y);

 

}

 

_____________________



_____________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11.  The following program compiles and runs without error.  Write the output of the program.

 

#include <stdio.h>

 

int f(int x) {

  int s = 0;

  s = s + x;

  return s;

}

 

int g(int x) {

  static int s = 0;

  s = s + x;

  return s;

}

 

void main(void) {

  int r, k;

 

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

    r = f(1);

  }

 

  printf(" %i ", r);

 

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

    r = g(1);

  }

 

  printf(" %i ", r);

 

}

 

 

   _________________________________________________________________

 

 

 

 

 

 

12.  Fill in the blanks to complete the following code fragment. This code should use Dynamic Memory Allocation to create an array named x of 1000 elements of data type double. If there is insufficient memory then the message “Insufficient memory!” is printed.

 

 

double * x;


______________________ = calloc(__________________ , ____________________ );



if ( __________________ == ____________________ ){

  

printf("Insufficient memory!\n");
return;

}

 

 

 

 

13.  Write a comparison function named compareMagnitude to sort an array named points of data type coord. The values in the array points should be sorted in ascending order by magnitude. The magnitude of a point is computed by taking the square root of the sum of the squares of the values in the x and y fields. For example if the values in the x and y fields are (3.0,4.0) then the magnitude would be calculated by the formula,

magnitude of (3.0,4.0) =
Complete the code for the compareMagnitude function. Do not write the call to qsort or the main function. You may use any C function from the Math Library.

 

typedef struct {

   double x;

   double y;

} coord;

 

coord points[1000];

 

int compareMagnitude(coord *a, coord *b) {

   /* fill in code here */