Name: ________________________ Netid: _______________ Section:
_______
Name: ________________________ Netid: _______________ Section: _______
Name: ________________________ Netid: _______________ Section:
_______
21 blanks, each blank worth 1/7 point for a total of 3 points.
Given the structures and variable declared above, complete
the C code below by filling in the blanks to assign to the
variable mycd the following values: cdno = 100 , title
= "Holding Onto Strings Better Left to
Fray", sales for January = 2000.00 . Hint: use strcpy
to assign strings.
_____mycd.cdno = 100;_________________
/* assign 100 to the cdno field of the variable named mycd */
______strcpy(mycd.title, "Holding Onto
Strings Better Left to Fray");____ /* assign "Holding Onto Strings Better Left to Fray"
to the title field */
______mycd.sales[0] = 2000.00;_________
/* assign 2000.00 to the first element of the sales array */
Continuing from question 1, complete the C code below by
filling in the blanks to assign to the variable mycd
the following values: for the 14th track, name = "Country Song", length = 349. Hint:
indexing in arrays start with 0.
___strcpy(mycd.tracks[13].name,"Country
Song");________ /* assign "Country
Song" to the name field of the 14th track of the
variable named mycd */
___mycd.tracks[13].length = 349;________________________
/* assign 349 to the length field of the track */
Given the structures defined before question 1, and given the declarations below, use the "*" and "->" operators to complete the assignment of the quantity = 2 to the variable named mycd .
Album * ptr = & mycd; /* ptr , points to mycd */
however we don't want you to write your answer as,
mycd.quantity = 2;
we want you to use the variable ptr
and the "*" operator to assign 2 to the quantity field of the
variable mycd and then do the same assignment again
but this time using the "->" operator .
(see lect21&22 slides 30-32)
__(*ptr).quantity_______ = 2; /* assign 2 to the quantity field of mycd, use the * operator */ __ptr -> quantity__ = 2; /* again, assign 2 to the quantity field of mycd,use the -> operator */
Given the declarations below, complete the C code by filling in the blanks with the correct answers.
#include <stdio.h>
typedef struct
{
double real;
double imaginary;
} complex;
complex add(complex,complex); /* prototype */
void main(void)
{
complex x = {1.0,2.0}, y , z;
z = x; /* assigns all values of fields of x to z */
y = add(x,z); /* perform the "complex" addition of x and z and assign to y */
printf(" real(y) = %lf, imag(y) = %lf \n", _____y.real__________________, ______y.imaginary_______________________);
}
/* to add two complex numbers use the rule {real1,imaginary1} + {real2,imaginary2} = {real1+real2, imaginary1+imaginary2} */
/* so {1.0,2.0} + {1.0,2.0} = {2.0,4.0} */
complex add(complex a , complex b)
{
/* declare c and initialize */
/* c is initialized with the sum of a and b */
complex c = {________a.real + b.real_______ , _______a.imaginary + b.imaginary_________};
return c;
}
Fill in the blanks below to complete the code for the function compare_mag that qsort will call to sort an array of complex numbers (see question #4) in ascending order by magnitude.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct
{
double real;
double imaginary;
} complex;
int compare_mag( complex * ptr1, complex * ptr2)
{
double mag1 = sqrt( ___ptr1 -> real * ptr1 -> real_______ + ____ptr1 -> imaginary * ptr1 -> imaginary_____ );
double mag2 = sqrt( ___pow(ptr2->real, 2)or in style above_________________________ + _____pow(ptr2->imaginary, 2)or in style above_______ );
if ( mag1 > mag2 )
return 1;
else if ( ___________mag1________________________ < _____________mag2_________________________)
return -1;
else
return 0;
}
void main(void)
{
int i;
complex arr[] = {{2.0, 0.0}, {3.0,4.0}, {0.0, 3.0}, {6.0 , 8.0}} ;
qsort(arr, 4 , sizeof(arr[0]), compare_mag);
for(i=0; i< 4 ; ++i)
printf(" %lf, %lf \n", arr[i].real,arr[i].imaginary);
}
Fill in the blanks to complete the following piece of code.
#include <stdio.h>
typedef struct
{
char first[30];
char
last[30];
int total_points;
} student;
/* You may
also assume that all names are less than 30 characters long and
that
the file contains at most 30 pairs of names. */
void main(void)
{
int
i=0, k;
/* Declare a variable called 'class' that is an array of 30 student(s): */
student class[30];
________FILE
* pt;_______________________________________
/*
Declare a variable 'pt' that is a pointer to FILE */
pt
=
_____fopen("input.dat",
"r");______________/*
Use fopen to open the file "input.dat" for reading */
if
(pt == NULL)
printf("file not opened for reading!\n");
/* Read in all names from the the input file */
while(EOF
!= fscanf(__________pt_______________
, "%s %s %i\n", class[i].last,
class[i].first , &class[i].total_points ))
{
i++;
}
for(k=0; k < i; ++k)
printf(" %s %s %i \n", class[k].last, class[k].first, class[k].total_points);
/* Close the file */
fclose(________pt_____________);
}
Part 2 of the lab is worth 6 points. See
allotment of
points described below.
1. (1/2
point per blank, 2 points total) From Step 4 Complete
the code
to sort the albums by quantity in ascending order. The name of the
function that qsort calls to sort the albums in ascending order by
quantity is called compareQuantityAsc.
qsort(____albums____,
__num_albums______, ___sizeof(albums[0])
or sizeof(Album)_, _compareQuantityAsc_);
2. (4 points total)Complete
the code for the compareQuantityAsc function.
int
compareQuantityAsc(Album * ptr1, Album * ptr2)
{
/* Step 5:
Complete this function */
/* your code goes here */
return ptr1 -> quantity - ptr2 ->
quantity ; (may use (*ptr1).quantity and (*ptr2).quantity
instead)
or
if ( ptr1 -> quantity <
ptr2 -> quantity)
return -1;
else if ( ptr1 -> quantity
> ptr2 -> quantity)
return 1;
else
return
0;
}