Prelab 14

Name: __________________________

NetID:__________________________

Section: ______________



The following exercises cover the topic of recursion and should help you prepare for the in-lab activities. As with all prelabs/labs, if you are not sure of the answer you may go to any EWS lab on campus and check your results by using the gcc compiler, or you can ask anyone of the CS101 staff for help. In order to understand the material needed to answer these prelab questions please read the following material:



Part A: DMA

1. Fill in the blanks below with the correct C code to dynamically allocate an array named albums that holds 750 elements of datatype Album.

   

        typedef struct 
        { 
           char name[40]; /* track name or song name */ 
           int  length; /* in seconds */
        } Track;
        typedef struct 
        { 
           int cdno; /* cd number */ 
        char title[30];
          char artist[20]; 
        int year;
          int num_tracks; /* number of tracks */ 
        int quantity; 
        float price; 
        float sales[MONTHS]; /* cd sales ($US) over the 12 months in 2005 */ 
        Track tracks[20]; /* info for each track */
        } Album;
 Album * albums;
 albums = calloc(_________________ , __________________________);

Part B: Mini Final Review

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

 

#include <stdio.h>

#include <string.h>

typedef struct

{
  int   id;
  char  name[32];
  float percentage;
} Student;

 

 

 

void check_student(Student * ptr)

{

   Student student;

   student.id = 2;

   student.percentage = 100.00;

   if(strstr( ptr->name, "Ang") != NULL)

      strcpy( (*ptr).name, "Brad");

  

}

 

 

void main(void) 

{

    Student student = {1, "Angelina", 90.5};

check_student(&student);


     printf(" Id is: %i \n", student.id);
     printf(" Name is: %s \n", student.name);
     printf(" Percentage is: %.2f \n", student.percentage);
}

________________________________________________

 

 

________________________________________________

 

 

________________________________________________



3. Fill in the blanks in the call to qsort to sort all 550 elements of the array named students based on compAgeDesc.



 qsort (____________,_______________,______________, compAgeDesc);





4. We will need to open a file using the “fopen” function under 3 scenarios, fill in the blank with the correct mode for opening the file, that is "r", "w" or "a".



I.      Concatenate characters to the end of file       mode = ___________

II.     Remove previous characters then add characters  mode = ___________  

III.   Read an integer from the file                   mode = ___________