Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active October 4, 2017 10:28
Show Gist options
  • Save rszewczyk/17ffeac928585fabe22af3a2d3ca4d21 to your computer and use it in GitHub Desktop.
Save rszewczyk/17ffeac928585fabe22af3a2d3ca4d21 to your computer and use it in GitHub Desktop.
UMBC CMSC 104 - Fall 2017 - Solutions for Class 7 & 8 assignments
#include <stdio.h>
double areaOfCircle(double radius)
{
return radius * radius * 3.14;
}
double areaOfSquare(double lengthOfSide)
{
return lengthOfSide * lengthOfSide;
}
double areaOfTriangle(double base, double height)
{
return base * height * 0.5;
}
void printSum(int x, int y)
{
printf("The sum is %d\n", x + y);
}
void printArea(double radius)
{
printf("The area is %lf\n", areaOfCircle(radius));
}
double totalCost(double costOfItem, int quantity)
{
// This is an example of where it's ok to use
// an int and a double in the same math operation
// the result of an int multipled by a double
// is a double, so we don't lose any precision.
// Also, logically - quantity makes more sense
// as an int.
return costOfItem * quantity;
}
double salesTax(double price)
{
return price * 1.06;
}
void printTotalCostWithTax(double costOfItem, int quantity)
{
double costWithoutTax = totalCost(costOfItem, quantity);
printf("total cost with tax is %lf\n", salesTax(costWithoutTax));
}
int main()
{
/* part 7.1 */
double areaOfCircleWithRadius10 = areaOfCircle(10);
printf("The area of a circle is %lf\n", areaOfCircleWithRadius10);
// Other than the value used for radius, line 58 results in the same
// thing as lines 53-54. Either method is correct for each part of
// this assignment.
printf("The area of a circle is %lf\n", areaOfCircle(20));
/* part 7.2 */
printf("The area of a square is %lf\n", areaOfSquare(10));
printf("The area of a square is %lf\n", areaOfSquare(20));
/* part 7.3 */
printf("The area of a triangle is %lf\n", areaOfTriangle(10, 5));
printf("The area of a triangle is %lf\n", areaOfTriangle(20, 5));
/* part 7.4 */
printSum(4, 6);
/* part 8.1 */
printArea(10);
/* part 8.2 */
printf("The total cost is: %lf\n", totalCost(12.0, 10));
/* part 8.3 */
printf("Total with tax applied to $120 is: %lf\n", salesTax(120.0));
/* part 8.4 */
printTotalCostWithTax(12.0, 10);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment