Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam (120 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                               Date: December 10, 2007

 


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 14 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. 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

Deduction

Grader

1

12

 

 

2

10

 

 

3

15

 

 

4

16

 

 

5

15

 

 

6

15

 

 

7

20

 

 

8

18

 

 

9

20

 

 

10

12

 

 

11

12

 

 

12

15

 

 

13

16

 

 

14

16

 

 

15

16

 

 

16

10

 

 

17

12

 

 

Total

250

 

 

 

 

Exam Scoreà

 

A

 

 

 

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



#include <stdio.h>

#include <string.h>

 

void main(void)

{

       int a = 0;

       double b = 4.0;

       int *p = &a;

       double *p2 = &b;

       char * ptr1 = "catdog";

       char * ptr2 = &ptr1[3];

 

       printf(" %i %lf \n", *p , *p2);

 

       if (strstr(ptr1,ptr2) != NULL)

printf("Substring!\n");

       else

printf("Not a Substring!\n");

}

 

 

 ______________________________           ______________________________

 

 

_____________________________________________________




 

 

 

2.      The program below compiles and runs without errors. Write the output of this program.

#include <stdio.h>

 

void main(void)

{

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

       int *p = a;

       printf("%i ",*p);

       p++;

       printf(" %i",*p);

}

 

 

____________            ______________

 

 

 

 

 

3.      Fill in the blanks to complete the program below.  The following program reads a list of integers, sorts the list of integers in ascending order and then prints the sorted list. The program makes use of the qsort() function to sort the list of integers.  Also, you will need to use dynamic memory allocation to create an array to store the list of integers.

 

     #include <stdio.h>

     #include <stdlib.h>

 

     /* This is the comparison function for qsort */

     int compare(int *a , int *b) {

 

      return  *a - *b ;

     }

 

     void main(void)

     {

       int i, N;

       int *num; /* num will hold the numbers to be sorted */

 

       printf("How many numbers? ");

       /* read an integer from the user, store it in N */

       scanf("%i",&N);

 

/* allocate memory for N elements */

 

       __________ = calloc(_____________________, ______________________);

 

 

       if( _________________________ ) { /* check for allocation failure */

         printf("Sorry, not enough memory...\n");

          return;

 }

 

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

         printf("Enter a number: ");

 

         _____________________________ /* read integer from the keyboard into array*/

       }

      /* qsort(arrayname , elts ,size_of_elts , compare_function) */

 

       qsort( _____________ , _____________ , __________________ , _______________ );

 

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

 

         printf("%i ", ______________________ ); /* print each number */

       }

       printf("\n");

 

       __________________________________ /* free the allocated memory */

     } /* end of main */

 

 

 

 

 

 

 

 

 

 

 

 

4.      Fill in the blanks to complete the code below to open a file with the name output.txt for writing   then fill in the blank to close the file.

 

FILE * fout;

int A[100];

int i;

 

_____________ = fopen(___________________,_______________________);

 

 

 

fclose(_________________);

 

 

 

5.      Fill in the blanks in the code below to dynamically allocate memory for an array named ptr with num_elements number of elements. Each element of the array is of data type double.

The variable num_elements  may take any integer value greater than zero.

int num_elements;

double * ptr;

int values[100];

 

printf("please enter the number of elements:");

scanf("%i", &num_elements);

 

_____________  =  malloc(___________________*___________________);

 

 

 

 

6.      The program below compiles and runs without errors. Write the output of this program.

 

#include<stdio.h>

 

void main(void)

{

       int A[2] = {2,5};

       int *ptr1, *ptr2;

       ptr1 = A;

       ptr2 = &ptr1[1];

       *ptr2 = 11;

 

       printf("%i %i %i\n", ptr1[0], ptr1[1], ptr2[0]);

}



________  ________  ________ 

 

 

 

 

 

 

7.      The program below compiles and runs without errors. Write the output of this program.

 

#include <stdio.h>

void fun1(int  *a, int *b)
{
  *a = *b;
  *b = *a;
}

void fun2(int a, int *b)
{
  int temp = a;
   a = *b;
  *b = temp;
}


void main(void)
{
  int a = 92, b = 15;

  fun1(&a, &b);
  printf(" %i   %i \n", a, b);
 

  a = 92;

  b = 15;

  fun2(a, &b);
  printf(" %i   %i \n", a, b);
 
}



___________              ____________


___________              ____________


 

 

 

 

 

 

8.      Given the structure named jump and the array named manyJumpers defined below answer the questions below.  Hint: array indexing begins with 0.

typedef struct
{   
  int   up;   
  int   down;
}jump;

jump manyJumpers[10];

a)      Write a single line of C code to assign the value 5 to the down member(field) of the second         element of the array manyJumpers.


        
          ______________________________________________________

b)      Write a single line of C code to assign the value of the down member(field) of the third element of the manyJumpers array to the down member(field) of the tenth element of the manyJumpers array.
       


          ______________________________________________________

  

c)      Write a single line of code to give the second element of the manyJumpers array the same values as the third element of manyJumpers array.


          _____________________________________________________

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9.      Complete the code for the compNameAsc function for sorting the array of artists based on their last names. Also, complete the code for the function sortName that calls qsort to sort the array and then display all the artists for which num_paintings is greater than 0, in the format shown by the example:

 

Piccaso(born in 1881): 5 paintings

 

The array named artists holds the artists last names, along with their date of birth and the number of paintings from that artist. We want to sort the array artists by the value in the last_name field lexicographically in ascending order. You may assume that the artists array contains information for 500 different artists.

typedef struct

{

  int day, month, year;

}Date;

 

typedef struct

{

  char last_name[100];

  int  num_paintings;

  Date birthdate;

}Artist;

 

Artist artists[500];

int num_artists=500;