CS 149 01 / Zelle | Fall 2019 |
Write at least 4 programs of the simple input/process/output variety that you might have written for CS 120. Here are some suggestions:
Calculate the volumen and surface area of a sphere from a user-inputted radius.
Calculate the cost per square inch of a circular pizza given its diameter and prices.
Calculate the distance to a lightning strike given the elapsed time between seeing and and hearing it.
Calculate the distance between two points.
Calculate the slope of a line through two points.
Calculate the are of a triangle given the lengths of its three sides (Heron's formula).
Generate a Socrates username from a students first name, lastname and id (using string functions).
Generate a 3 letter acronym from 3 input words.
Feel free to invent your own example problems.
Using an array of musiccd structs (as discussed in class), write a program that allows the user to enter information about 5 cds (entered in a loop) and then uses a loop to print a summary of the collection. Here is a sample of how the output should look (user input is in bold):
Enter info for CD 1 Title: Texas Flood Artist: Stevie Ray Vaughan Price: 1195 Enter info for CD 2 Title: The Wall Artist: Pink Floyd Price: 895 Enter info for CD 3 Title: Wasting Time Artist: Sleepy Bones Allison Price: 300 Enter info for CD 4 Title: Hotel California Artist: Eagles Price: 885 Enter info for CD 5 Title: Night at the Opera Artist: Queen Price: 1556 Here is your collection: Texas Flood, Stevie Ray Vaughan, $11.95 The Wall, Pink Floyd, $8.95 Wasting Time, Sleepy Bones Allison, $3.00 Hotel California, Eagles, $8.85 Night at the Opera, Queen, $15.56
Note: you do not need to use any helper functions or data abstraction at this point. Just do the variable declarations and write a couple of loops. The purpose here is to use an array of structures.
Use the value union from chapter 12 to create a variable that can store either an unsigned long int or a regular float (both should be 8 bytes, but you might check that on your machine using the sizeof function). Write a program that allows a user to enter a floating point number, and then print out the hexadecimal representation of the bits of the floating point that they entered. An example run is shown below:
Enter a floating point value: 3.14 The bits are: 0x00007fff4048f5c3
Exercises 13-1 and 13-2 on page 206.
Improve your program for music cds by using some helper functions. Cds will be passed by sending pointers to the appropriate structure. Here are the functions you should implement and use:
void cd_fill(musiccd *cd, char *title, char *artist, int price); void cd_get(musiccd *); (* get info from user and fill the cd *) void cd_printinfo(musiccd *);
Update the "CD library" program so that you can store the cd information into a file and read that file back in again at a later time. The point right now is simply to practice using file I/O operations, but it might also be nice to practice encapsulating your procedures into functions like: store_collection and load_collection. You can extend the file format that we discussed at the end of class today. Each CD just takes up 3 lines of the file for title, artist, and price.
Modify the exam score average that we did in class today so that it is not necessary to pass both the array the value of n to all of the helper functions. I suggest two changes. First, move allocation of the dynamic array to get_scores, so the only parameter to get_scores is n. Second, use a sentinel value (score < 0) to indicate the end of the array. Here are the new helper prototypes:
int get_int(char prompt[]); int *get_scores(int n); float average(int *scores);
Modify the istack example (istack.h, istack.c) to allow it to store arbitrary dynamic objects (using void *). You can test your modification using the stacktest.c driver program.
Implement the musiccd and cdcoll types as outlined in class and write testing code that shows that your types are working properly. As a bonus, write a menu-driven program that allows the user to manage a cd collection.