Name: ________________________ Netid: _______________ Section:
_______
You should use the material found in Lecture 19&20 and
21 of the course notes. 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.
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.
______________________________
______________________________
______________________________
______________________________
Define your
structure in such a way that you can declare a structure variable
of
type Student as
follows:
Student x ;
2. Now suppose we declare an array
of
Student structures
as
follows:
Student
students[10];
Remember array indexing begins
with 0 in C.
(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.
________________________________________________________
(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 students
array.
________________________________________________________
(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.
________________________________________________________
3.
Using
the Student
datatype you created in the first question, declare a single
variable
named stud of
datatype
Student. On the
same
line that you declare your
variable assign "Kobe Bryant"
to the name
member(field) and 25 to the id
member(field).
_______________________________________________________________________________
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;
___________________________________
___________________________________
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 john 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(
____________________________________, myName);
strcpy(
____________________________________, myNetid);
_____________________________________ = soc;
printEmployee(john);
}
6.
Fill
in the code for the printEmployee function.
void
printEmployee(Employee e)
{
printf("Name: ");
printf(_______________, _______________________); /* Print the
name
*/
printf("\n");
printf("Netid: ");
printf(_______________, _______________________); /* Print the
netid */
printf("\n");
printf("SSN: ");
printf(_______________, ________________________); /* 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,
__________________, ____________________, _______________);
printf(" %s \n %s \n %s \n %i\n",artist,song_name,
album_title,track_length);
}
1. Fill in the code for the fscanf call in readAlbums.
while( EOF !=
fscanf(fileptr,"_________________________________________________________________________",
&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. Fill in the code for the fscanf call in readAlbums.
fscanf( ____________________ ," %[^;]; %i ", albums[num_albums].tracks[track].name, ___________________________________________________________________ );
3. How many points you get from mp2checker for test case 3 and
4?
For 3. readAlbums(), you get _____. For 4. mainMenu(), you get
_____.