Name: ________________________ Netid: _______________ Section:
_______
Name: ________________________ Netid: _______________ Section: _______
Name: ________________________ Netid: _______________ Section:
_______
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.
_________________________________________ /* assign 100 to the
cdno field of the variable named mycd */
_________________________________________ /* assign "Holding Onto Strings Better Left to Fray"
to the title field */
_________________________________________ /* 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.
_____________________________________ /* assign "Country Song" to the name field of
the 14th track of the variable named mycd */
_____________________________________ /* 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)
_____________________________ = 2; /* assign 2 to the quantity field of mycd, use the * operator */ ______________________________ = 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", _______________________, _____________________________);
}
/* 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 = {______________________________________________ , __________________________________________________________};
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( ____________________________ + ________________________________ );
double mag2 = sqrt( ____________________________ + ________________________________ );
if ( mag1 > mag2 )
return 1;
else if ( ___________________________________ < ______________________________________)
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];
_______________________________________________
/* Declare a variable 'pt' that is a pointer to FILE */
pt
=
___________________________________________________________________/*
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(_________________________ , "%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(_____________________);
}
1. 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(____________________________,
___________________________, ____________________________,
___________________________);
2. Complete the code for
the compareQuantityAsc function.
int compareQuantityAsc(Album
* ptr1, Album * ptr2)
{
/* Step 5: Complete this function */
/* your code goes here */
}