Lab 12 Answer Sheet

This lab is to be worked individually.


Name: ________________________ Netid: _______________ Section: _______



Part 1: Prelab

Part 1  is worth 3 points , 24 blanks each worth 1/8 point.

1.      Write the code to create a structure definition for Student, a structure with two fields (members) , the first called netid (integer data type and a second called name (char array of size 64). Use typedef to name the structure type Student.


____typedef struct{__________________________


_______int netid;_______________________


_______char name[64];_______________________


____} Student;__________________________

Define your structure in such a way that you can declare a strucure variable of type Student as follows:
Student x ;


2.      Now suppose we declare an array of Student structures as follows: Student students[10];


(a) Write a single line of C code to assign the name "Michael Jordan" to the name member(field) of the fifth element of the array students. Hint: use the strcpy function.

_____strcpy(students[4].name, "Michael Jordan");___________________________________________________
(b) Write a single line of C code to assign the value of the name member(field) in the fifth element of the students array to the name
member(field) of ninth element of the studnets array.

__________ strcpy(students[8].name, students[4].name);______________________________________________

(c) Write a single line of code to give the second element of the students array the same values as the last element of students array.

___________students[1] = students[9];_____________________________________________

3.      Using the Student datatype you created in the first question, declare a single variable named stu of datatype Student. On the same line that you declare your
variable assign "Kobe Bryant" to the name member(field) and 25 to the netid member(field).
___________Student stu = {25, "Kobe Bryant"};___(the values must be in this order since netid is the first field and name is the second)_______

4.      In this problem, you are going to use functions from math library. If you test your code by typing into a file named q4.c then to compile the code you would type,

gcc q4.c -lm

where -lm (dash L m ) means, link to the math library.

Complete the code for the function named convert that converts points in polar coordinates to points in Cartesian coordinates by means of the formula,

x = radius * cos(theta)
y = radius * sin(theta)

A point in the plane can be uniquely identified by the pair (radius, theta) where radius is the distance of the point from the origin and theta is angle formed between the two lines, the x-axis and the line between the origin and the point. We can compute the (x,y) coordinate by using the above formulas.

#include <stdio.h>
#include <math.h>

typedef struct {
double x;
double y;
} cartesian;

typedef struct {
double radius;
double theta;
} polar;

cartesian convert( polar point)
{

cartesian p;

________p.x = point.radius * cos(point.theta);_________

______ p.y = point.radius * sin(point.theta);____________

return p;
}
void main(void)
{
polar point = { 1.0 , 3.141592653589793 / 4 };
cartesian p ;
p = convert(point);
printf(" x = %lf , y = %lf \n", p.x , p.y);
}

When your code is run you should obtain the output shown below.


x = .707107 y = .707107

5.      (Questions 5 and 6 use the following datatype named Employee). Fill in the code needed to initialize the variable named tom with the information provided in myName,myNetid, and soc.
Note that the datatype of the variable tom is Employee.


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

typedef struct
{
char name[20];
int ssn;
char netid[20];
} Employee;

void printEmployee(Employee);

void main(void)
{
char myName[] = "John Smith";
char myNetid[] = "johnsmith";
int soc = 987654321;
Employee john;


strcpy( ______john.name________, myName);


strcpy( ______john.netid________, myNetid);


________john.ssn_____________________________ = soc;

printEmployee(john);
}

6.      Fill in the code for the printEmployee function.

void printEmployee(Employee e)
{
printf("Name: ");


printf(_____"%s"__________, ______e.name_________________); /* Print the name */
printf("\n");
printf("Netid: ");


printf(_____"%s"__________, _______e.netid________________); /* Print the netid */
printf("\n");
printf("SSN: ");


printf(____"%i"___________, ________e.ssn________________); /* Print the SSN */
printf("\n");
}

7.      One problem with %s is that we cannot read more than one word at a time since the space character serves as a delimiter. To fix this problem (only for reading strings) we will use the brackets conversion specifier %[ ], consult page 498 of Applied C for more information.
Inside the brackets we will use the special symbol '^' meaning NOT. For example, %[^;]; then the [^ ; ] part means read all characters (including blanks) while the character is NOT ; (and therefore stop reading when the next character is a ; ) and the ; outside the [^ ;]; means read and discard the next ;. For example, if we declared,

char artist[40];
char song_name[40];
char album_title[40];
int track_length;

Assume that the standard input (from the keyboard) contains:

The Beatles; 214

Then

 scanf(" %[^;]; %i", artist, &track_length);

reads "The Beatles" into the array 'artist' and the number 214 into 'track_length'.

Complete the following C program to read in

 The Beatles; The Long and Winding Road; Let It Be... Naked; 214

into 'artist', 'song_name', 'album_title' and 'track_length'


#include <stdio.h>


void main(void)
{
char artist[40];

char song_name[40];
char album_title[40];
int track_length;


printf(" Please enter the artist, song_name, album_title, and track_length\n");
scanf(" %[^;]; %[^;]; %[^;]; %i",artist, song_name, album_title, &track_length);

printf("%s \n %s \n %s \n %i\n",artist,song_name, album_title,track_length);
}






Part 2: Lab



Part 2 is worth 6 points.


1. (4.2 points) Fill in the code for the fscanf call in readAlbums.

In the following, each "%.." is worth of 0.6 pts.

while( EOF != fscanf(fileptr,"%d; %[^;]; %[^;]; %d; %d; %d; %f",  /* %i may be used rather than %d */
                              /*(NOTE: %i instead of %d is fine)*/ 
             &albums[num_albums].cdno,
              albums[num_albums].title,
              albums[num_albums].artist,
             &albums[num_albums].year,
             &albums[num_albums].num_tracks,
             &albums[num_albums].quantity,
             &albums[num_albums].price) ) {
 ...
}


2. (1.2 points)Fill in the code for the fscanf call in readAlbums.
In the following, each "%.." is worth of 0.6 pts.

  fscanf( fileptr ," %[^;]; %i ",   /* %d may be used rather than %i */
         albums[num_albums].tracks[track].name, 
        &albums[num_albums].tracks[track].length );

3. (0.6 points)How many points you get from mp2checker for test case 3 and 4?

0.3 points for each blank
For 3. readAlbums(), you get _____2______. For 4. mainMenu(), you get ____1_______.