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;

 

int compNameAsc(Artist * m1, Artist * m2)

{

 

  return strcmp(_______________________,_______________________);

 

}

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

void sortName(void)

{

   qsort(_____________,_____________,_____________,_____________);

 

   int i;

 

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

{

          if (____________________________________  > 0)

{

 

              printf("%s(born in %i): %i paintings\n",

 

              ___________________________________,

 

              ___________________________________,

 

              ___________________________________ );

 

          }

     }

  }

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

 

#include <stdio.h>

 

int mystery(int first, int last, char word[])

{

       /* Voom!!! */

       if (first == last)

          return 1;

       else if (word[first] != word[last])

              return 0;

       

       return mystery( first+1 , last-1 , word);

}

 

     void main(void)

{

       printf("%i \n",  mystery(0, 0 , "A"));

       printf("%i \n",  mystery(0, 2 , "cat"));

       printf("%i \n",  mystery(0, 2 , "eye"));

       printf("%i \n",  mystery(0, 4 , "sonar"));

       printf("%i \n",  mystery(0, 4 , "radar"));

}

 

______________________________

 

 

______________________________

 

 

______________________________

 

 

______________________________

 

 

______________________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

 

int fun(int *a, int b)

{

  int c;

  *a = 2*b;

  b = *a;

  c = (*a)*b;

 

  printf("%i %i %i \n", *a, b, c);

  return c;

}

 

void main(void)

{

  int a, b, c;

 

  a = 1;

  b = 2;

  c = 3;

 

  fun(&a, a);

 

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

}

 

 

 

            __________    __________    __________

 

 

 

            __________    __________    __________   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

 

#include <stdio.h>

 

int array[10] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};

 

void exchange(int *a, int *i)

{

  int temp;

  temp = a[*i];

  a[*i] = *i;

  *i = temp;

}

 

void main(void)

{

  int index = 3;

  exchange(array, &index);

  printf("%i \n", index);

  printf("%i %i \n",array[0],array[3]);

  printf("%i %i \n",array[5],array[7]);

}

 

 

_______________

 

 

_______________      _______________

 

 

_______________      _______________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

13.  Complete the code for the function named ambiguous.  This function takes as input an array of data type Person and its number of elements and determines whether there are two people in the array with the same name, that is, whether there are two different elements in the array for which the values in the name fields are the same -- if this is the case, ambiguous returns 1, otherwise it returns 0.

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

typedef struct
{
  char name[60];
  char likesBest[60];
}Person;

int ambiguous(Person[], int);

void main(void)
{
  Person persons[100];
  int i, num_persons;
  printf("\n Enter the number of persons (<=100): ");
  scanf("%i",&num_persons);
  for(i = 0; i < num_persons; i ++)
  {
    printf("\n Enter the name of a new person: ");
    scanf(" %[^\n]", persons[i].name);
    printf("\n Enter the name of her/his best friend: ");
    scanf(" %[^\n]", persons[i].likesBest);
  }

  if (ambiguous(persons, num_persons)== 1)
    printf("\n The array contains ambiguous names.\n");
  else
    printf("\n The array does not contain ambiguous names.\n");
}




int ambiguous(Person persons[], int num_persons)
{
  int i,j;
  for(i = 0; i < num_persons; i ++)
    for(j = i + 1; j < num_persons; j ++)


      if(strcmp(___________________,_____________________) == 0)


        return ____________;


  return ________________;
}




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


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

typedef struct
{ int field1;
  int field2;
}Simple;

typedef struct
{ int field1;
  Simple *field2;
}Intricate;

typedef struct
{ int field1;
  Simple field2;
  Intricate field3;
}MoreIntricate;

void main(void)
{
  Simple s = {1, 7};
  Intricate i = {2, &s};
  Intricate *pi = &i;
  MoreIntricate m = {3, s, *pi};

  pi -> field1 = 3;

  printf("%i\n", (*(i.field2)).field1);

  printf("%i\n", ((&i)->field2)->field1);
  printf("%i\n",  m.field3.field1);
  printf("%i\n", (m.field3.field2) -> field2);
}


___________________________________


___________________________________


___________________________________


___________________________________

 

 

 

 

 

 

 

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

 


#include <stdio.h>
#include <stdlib.h>

typedef struct{
   int id;
   int quantity;
   int price;
 }Album;

int comp(Album *a, Album *b)
{
  if (a->quantity * a->price < b->quantity * b->price)
    return -1;
  if (a->quantity * a->price > b->quantity * b->price)
    return 1;
  return a->quantity - b->quantity;
}

void main(void)
{
  Album albums[10];
  int i;

  for(i = 0; i < 4; i++)
    albums[i].id = i;

  albums[0].quantity = 5;
  albums[0].price = 10;

  albums[1].quantity = 9;
  albums[1].price = 6;

  albums[2].quantity = rand() % 5;
  albums[2].price = 11;

  albums[3].quantity = 10;
  albums[3].price = 5;

  qsort(albums, 4, sizeof(Album), comp);

 
  for(i = 0; i < 4; i++)
    printf("  %i  ",albums[i].id);

}


___________              ___________              ___________              ___________

 

 

 

 

16.  Re-write the comp function of the previous question so that when the program in the previous question is run you will sort the albums array by the value in the id field in descending order. Do NOT write comments. Do NOT write code for the main function. Writing any other code besides the code for the comp function will give you a zero score for this problem.

 

int comp(Album *a, Album *b)
{

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

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

Hint: % means remainder so 5%2 equals 1.

#include <stdio.h>

int euclid( int a, int b)
{
   if ( (a % b) == 0)  /* if b divides into a without

                          remainder ...   Voom!*/
       return b;
    else
       return euclid(b, a  % b); 
}

void main(void)
{
  printf(" %i  ", euclid(2,4));
  printf(" %i  ", euclid(2,5));
  printf(" %i  ", euclid(4, 16));
  printf(" %i  ", euclid(12,18));

}

____________    ____________    ____________    ____________