Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lab 11 Answer Sheet

This lab is to be worked individually.


Name:________________________Netid:_______________Section:_______

Part 1: Reading strings with blanks

1. Complete the following C program to read in


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

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; /* in seconds */ int num_tracks; /* number of songs */ scanf(" %[^;]; _____ %[^;]_______ ________ %[^;]_____ %i; %i", artist, _____song_name___________, _______album_title___________, ________ &track_length__________);

/* However, you can use %s to print strings containing blanks */ printf(" %s \n %s \n %s \n %i (secs)\n %i (songs)\n",artist,song_name, album_title,track_length, num_tracks); }

Part 2: File I/O , Structures and Sub-structures


2. Complete the program below by filling in the blanks
#include <stdio.h> typedef struct { track name or song name */ length; /* time in seconds */ char name[40]; int } Track; /*

typedef struct { int cdno; char title[30]; char artist[20]; int num_tracks; Track tracks[4]; /* each album can hold up to just 4 songs in this problem */ } Album;

void main(void) { Album x; /* x is a single Album in this example. In MP 2 we will declare an array named albums of data type Album */
int i; FILE * fileptr; /* the following involves FILE I/O see lecture 24 slides 9-12 */ fileptr = fopen(_________part2.dat___________, file part2.dat for reading */ if (fileptr == NULL) { "r"); /* open

printf("file part2.dat not found! \n"); return; } /* read cdno, title, artist and num_tracks from the file */ fscanf(fileptr, "_________909_______ _____Let It be Naked________ _______The Beatles________ __________4_______", &x.cdno, x.title, x.artist, &x.num_tracks);

/* read name and length for the four tracks from the file */ for(i = 0; i < x.num_tracks; ++i) fscanf(fileptr, " %[^;]; %i", _________4______________, &x.tracks[i].length );

/* print out the results */


printf("%i %s %s %i \n", x.cdno, x.title, x.artist, x.num_tracks);

for(i = 0; i < x.num_tracks; ++i) printf("%s, %i \n", x.tracks[i].name, x.tracks[i].length); }

Part 3: MP2
3. Complete the program below by filling in the blanks.
int readAlbums( char *filename, Album albums[], int num_albums, int max_size) { /* begin of readAlbums */ int track, i; FILE * fileptr; /* flieptr is a pointer variable, FILE is a new datatype */ fileptr = fopen( filename, "r"); /* filename is a string, "r" means open file for reading */ if (fileptr == NULL) return -1; /* read the data from ALBUM_FILE */ /* first record looks like ... 1001; One Heart; Celine Dion; 2003; 14; 2; 13.99 211.10 96.15 67.93 340.67 138.01 250.97 259.76 74.66 114.92 268.26 126.47 79.63 */ /* The datatype Album (from mp2.h) looks like... typedef struct { int cdno; char title[30]; char artist[20]; int year; int

num_tracks; int quantity; float price; float sales[MONTHS]; cd sales ($US) over the 12 months in 2005 Track tracks[20]; } Album; */ /* check to make sure that we haven't exceeded the maximum size for the array albums */ if ( num_albums >= max_size) { fclose(fileptr); return num_albums; } /* you must fill in the blank on the line below */ /* size starts at 0 */ while( EOF != fscanf(_______fileptr______________, "%i; %[^;]; %[^;]; %i; %i; %i; %f", &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) ) { /* read the sales over 12 months */ for ( i = 0; i < MONTHS; i ++ ) { fscanf( fileptr, " %f", &albums[num_albums].sales[i]); } /* We need to read the track data into the array named tracks. * The first record has the following form ... * I Drove All Night; 240 */ /* The datatype Track (from mp2.h) looks like... typedef struct { char name[40]; track name or song name int length; in seconds } Track; */ for( track = 0; track < albums[num_albums].num_tracks; ++track ) { /* fill in the following blanks to read in all the track data */ fscanf( fileptr ,______ %[^;];%i ________ , albums[num_albums].tracks[track].name, ___________________&albums[num_albums].tracks[track].length____________ ____________ ); } /* we succeeded in reading in one more album so increase size by one */ num_albums++; /* we don't want to read more CDs than the size of the albums array so check now */ if ( num_albums >= max_size) { fclose(fileptr); return num_albums; } } /* end of while */ fclose(fileptr); return num_albums; } /* end of readAlbums */

You might also like