QuestionQuestion

Lab Exercise 3 – Short C programs and module creation

OBJECTIVES:
• Practice writing short C programs using selection statements.
• Gain experience with creating modules to separate the functions from the main for reuse.

NOTES:
• Properly acknowledge (add a note and/or hyperlink and/or comment) any help or resource you used.
• Programs without ID boxes will have marks deducted

INTRODUCTION: It is not unusual for C programs to grow to be hundreds of thousands or millions of lines of (source) code or LOC, for short. Proper organization and compilation of these files is important. In this lab, you will see how to organize a C program into several files and use make to compile the individual files and generate the executable code.

INSTRUCTIONS:
0. Read the document General Lab Directions that is posted on Blackboard.
1. Small C program: Write a C program named firstchange.c that asks the user for two monetary amounts (so all input should have dollars and cents, such as 5.75). The first of these is a total for a bill and the second is the amount the customer provided. Calculate change for the transaction (use bills up to 50$ only in the change). To avoid rounding errors you should use the double type for the amounts. Notice that you will need to use %lf specifier to read and print double values/variables.

Hint: to make everything a bit prettier, you may wish to look up the trunc function in the textbook appendix.

Examples:
Input: The underlined text is what the user would type: Enter amount owed: 16.50
Enter amount paid: 140.65

Output: must match this Change of:
2 fifty dollar bill 1 twenty dollar bill 2 two dollar coin
1 dime
1 nickel

2. Making a module: For this task you have to reorganize the previous program using modules.
a) in a file named change.c create a function named change with two double parameters: the first one is a total for a bill and the second one is the amount the customer provided. The function must calculate and print the change for the transaction, as indicated in point 1.
b) create a header file change.h which contains the prototype of the change function. Do not forget to document your function.
c) Write a C program named secondchange.c, similar in purpose to firstchange.c but this time use the change function from change.h. The program MUST be able to handle multiple input lines. It must continue processing lines, until it encounters the end of a file (CTRL^D – from stdin).
Note: here we are assuming that the input will be redirected from a file, which DOES HAVE an end of file at the end.
Hint: you may want to analyze the attached echoNums.c file.
d) Create a makefile with separate compilation to produce an executable named change of secondchange.c For this point you may want to review part 3 of Lab Exercise 2.
e) Add a clean rule to your makefile to remove all .o files created during compilation
f) Add a tar rule to your makefile to create a tarball named lab3.tar.gz

The tarball must include the following files only:
firstchange.c
change.c
change.h
secondchange.c
makefile

3. Demo: Write a C program that prompts the user for two dates, prints out which date is earlier and whether the dates are from the same year. When printing out the dates, change the format as indicated below.

To get the full marks you must use a switch statement to print the new format. Make the most effective use of the switch statement.

Example
Input: The underlined text is what the user would type:
Enter Date 1 (mm/dd/yyyy): 5/13/1989
Enter Date 2 (mm/dd/yyyy):9/2/1989

Output: must match this
May 13, 1989 is earlier than September 2, 1989.
The dates are from the same year.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <stdio.h>
#include <math.h>

int main() {
double owed, paid, change_amt;
int fifty, twenty, ten, five, two, one, quarter, dime, nickel, penny;

// load the user input
printf("Enter amount owed: ");
scanf("%lf", &owed);
printf("Enter amount paid: ");
scanf("%lf", &paid);

// calculate the change and truncate it to two decimals
change_amt = round((paid - owed)*100) / 100;

// find the amounts of each banknote
fifty = change_amt / 50;
change_amt -= fifty*50;
twenty = change_amt / 20;
change_amt -= twenty*20;
ten = change_amt / 10;
change_amt -= ten*10;
five = change_amt / 5;
change_amt -= five*5;
two = change_amt / 2;
change_amt -= two*2;
one = change_amt / 1;
change_amt -= one*1;
quarter = change_amt / 0.25;
change_amt -= quarter*0.25;
dime = change_amt / 0.10;
change_amt -= dime*0.10;
nickel = change_amt / 0.05;
change_amt -= nickel*0.05;
penny = change_amt / 0.01;

// print the results
printf("Change of:\n");
if(fifty)
    printf("%d fifty dollar bill\n", fifty);

if(twenty)
    printf("%d twenty dollar bill\n", twenty);

if(ten)
    printf("%d ten dollar bill\n", ten);

if(five)
    printf("%d five dollar bill\n", five);

if(two)
    printf("%d two dollar coin\n", two);

if(one)
    printf("%d one dollar coin\n", one);

if(quarter)
    printf("%d quarter\n", quarter);...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$35.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats