COMP 166 - Lab 1 - Variables & Operators

Learning Outcomes and Introduction

In this lab you will create some simple programs to practice what you've learned about:

You should pay special attention to using good coding style so that your code is readable, consistant, and easy to understand.

How to handle input

Ideally, both of the programs you create in this assignment should accept user input from the keyboard to get the initial data needed to do their work. You can read in a single integer value and store it in a variable with the following two lines of code:


				int example;
				scanf("%d", &example);
			

However, at this stage of the course we have not learned enough about C to fully understand how this code works. If you are uncomfortable using this code, feel free to provide your "input" data as hard-coded symbolic constants instead, eg:

#define INPUT 1324

Task 1: Acceleration in an Atwood Machine

diagram of the an Atwood machine
Diagram of forces in an Atwood Machine

Write a program that calculates the acceleration of the weights in an Atwood machine. The weights are connected by a massless cable that runs through an ideal pulley and are under the influence of gravity, such that the acceleration is given by the equation:

a = m1 - m2 m1 + m2 × g

Assume that g, the acceleration due to gravity, is exactly 9.806 m/s2.

The masses of the two weights are the inputs to your program, and should be expressed as positive integers. The unit of measurement is irrelevant due to the structure of this formula (although the same unit must be used for both).

Your program should print the acceleration in m/s2 to standard output, with a minimum of 2 decimal places of precision.

Task 2: Money Changer

Write a program that converts a pool of loose change into bills and coins using the largest possible denominations. The program should take a total value of cash in cents as input, and print the number of $10 bills, toonies ($2 coins), quarters (25¢), dimes (10¢), and pennies (1¢) that together total that value as output.

Your program should be written in a way that allows code for new denominations to be added (or existing denominations to be removed) without substantially changing the code for other bills and coins. For example, it should be easy to add output for $20 bills, loonies ($1), or nickles (5¢).

Your approach should be something like:

  1. Prompt the user to input a total value, or write a clear section of code comments to show which symbolic constant should be changed for different values.
  2. Store the total target amount, in cents, in a variable.
  3. Divide by 1000 to find the number of $10 bills, and reduce the target amount to whatever is left over.
  4. Divide by 200 to find the number of $2 coins, and similarly reduce the target amount.
  5. Similarly calculate the number of quarters and dimes.
  6. Record any remainder as the number of pennies.
  7. Print the number of bills or coins of each denomination (including zeros).

For example, given an input of 3049 cents, your output might look like:

3 $10 bills
0 toonies
1 quarters
2 dimes
4 pennies

Submission and Grading

Completing all tasks in this lab should result in 2 separate C files within a single folder named like LastnameFirstname_lab1. Compress the folder into a ZIP archive and upload it to the D2L assignment.

The marks for this lab are heavily weighted towards good coding practice and style. Pay attention to your code formatting, use comments to clarify your code, and make sure you are using symbolic constants and variables effectively.

NOTE: This assignment is to be done individually. You can help one another with problems and questions, but in the end everyone must write and submit their own code.

CriteriaMarks
Programs compile and run without error 3
Good coding style 3
Task requirements met or exceeded 3
Learning Outcomes achieved 3
Total 12