• Portfolio
  • Archive
    • Grad Students Halloween Party 2016
    • Praxis Showcase 2010 Highlights
    • 2015 Canada Blooms Near North Hardscapes
    • 2014 IEEE Toronto AGM Highlights
    • 2014 GSU Halloween Party
    • 2014 GSU Halloween Party 2
    • IEEE Day: Wine and Cheese
    • 2014 Akido Club Photoshoot
    • Canonball 1T5
    • 2014 Toronto Christmas Market
    • 2015 Nocturne
    • Cute-Baby-TBP
  • Stuff.
  • Contact Info

kmingk

Just Me.

  • Portfolio
  • Archive
    • Grad Students Halloween Party 2016
    • Praxis Showcase 2010 Highlights
    • 2015 Canada Blooms Near North Hardscapes
    • 2014 IEEE Toronto AGM Highlights
    • 2014 GSU Halloween Party
    • 2014 GSU Halloween Party 2
    • IEEE Day: Wine and Cheese
    • 2014 Akido Club Photoshoot
    • Canonball 1T5
    • 2014 Toronto Christmas Market
    • 2015 Nocturne
    • Cute-Baby-TBP
  • Stuff.
  • Contact Info

APS105 Tutorial 2

Mainly covers conditional statements, but once again going from code to program.  There are several key things to note here:

  • Constants are used to instead of "magic" numbers. 
  • Math library is used here
    • Netbeans --> Project properties --> Linker --> Libraries --> Standard libraries
/* 
 * File: main.c
 * Author: Kmingk
 *
 * Created on September 27, 2015
 */

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

/* Tutorial 2 - Advanced output
 * 
 * Program Description:
 * Build a program that takes a dollar (and cents) value less than 20
 * and rounds the value to the nearest nickel.It then computes the
 * smallest number of total bills and coins that sum to that rounded value
 * 
 * Inputs:
 *rawAmount - Double
 * Outputs:
 *roundedAmount - Double
 *numTens - int
 *numFives- int
 *numOnes 
 *numQuarters
 *numDimes
 *numNickels
 */
int main(int argc, char** argv) {
// Variable and Constants Declaration
const double TEN= 10;
const double FIVE = 5;
const double ONE= 1;
const double QRTR = 0.25;
const double DIME = 0.1;
const double NICKEL = 0.05;

double rawAmount, roundedAmount, savedRoundedAmount;
int numTens, numFives, numOnes, numQuarters, numDimes, numNickels;
 
// Get monetary value from user
printf ("Enter Value: ");
scanf ("%lf", &rawAmount);
if (rawAmount < 0) {
exit()
}

// Round value to nearest nickel
// round the normalized amount rawAmount/Nickels --> then multiply
// by the value of a nickel to get the rounded to nickel value
roundedAmount = rint(rawAmount / NICKEL) * NICKEL;
savedRoundedAmount = roundedAmount;
//printf ("%lf", roundedAmount);

// Calculating the number of each denomination, roundedAmount keeps a 
// rolling total of the remainder
numTens = roundedAmount/TEN; 
roundedAmount = roundedAmount - TEN*numTens; 
numFives = roundedAmount/FIVE; 
roundedAmount = roundedAmount - FIVE*numFives;
numOnes = roundedAmount/ONE;
roundedAmount = roundedAmount - ONE*numOnes;
numQuarters = roundedAmount/QRTR;
roundedAmount = roundedAmount - QRTR*numQuarters;
numDimes = roundedAmount/DIME;
roundedAmount = roundedAmount - DIME*numDimes;
numNickels = roundedAmount/NICKEL;
roundedAmount = roundedAmount - NICKEL*numNickels;

// Output Raw and Rounded amount with change needed
printf ("Raw Value: $%0.2lf\n", rawAmount);
printf ("Rounded Value $%0.2lf\n", savedRoundedAmount);
printf ("# Tens: %d\n", numTens);
printf ("# Fives: %d\n",numFives);
printf ("# Dollars: %d\n",numOnes);
printf ("# Quarters: %d\n", numQuarters);
printf ("# Dimes: %d\n",numDimes);
printf ("# Nickels: %d\n",numNickels);
}
Monday 10.05.15
Posted by Kei-Ming Kwong
Newer / Older