Hi all,
Im pretty new to programming, so if my code makes no sence in parts, please forgive me.
I need help with an assignment that I am doing. I have finished question 1 (was about Triangle types and whether they were valid, depending on what the user inputed), but I am stuck on question 2 (which has two parts).
Note that part 'b' is pretty much program 'a' except with added 'dollars'
Also I did not know really what rint() was in question 2b, so I did not add it to the program.
question 2a is:
You are asked to write a simple program that will accept an integer value in the range of 5-95 and in increments of 5 at a time, representing the number of cents to give to a customer in their change. The program should calculate how many coins of each denomination and display this to the user. Valid coin values are 50, 20, 10 and 5. Your solution (program and algorithm) should be modular in nature. This requires the submission of a high-level algorithm and suitable decompositions of each step.
for question 2a I have done so far:
Code:
/********************************************************************************
Author: Steve
Date: April 2004
Purpose: ICT 102, Assignment Program Two, Cents.
********************************************************************************/
#include <stdio.h>
#include <conio.h>
#include <math.h>
/*******************************************************************************
********************************************************************************
MAIN PROGRAM STARTS HERE
********************************************************************************
*******************************************************************************/
int main()
{
//Variables
int i, result, fifty, twenty, ten, five, money;
char option;
printf("This program calculates the amount of cents, and what type of coins need to be returned to the customer.");
do
{
printf("\n\nDo you wish to continue? (Y/N): ");
//MAKES UPPER OR LOWER CASE CHARACTERS INTO THIER OPPOSITE VALUE.
do
{
option = getch();
option = toupper (option);
}while (!((option=='Y')||(option=='N')));
printf("%c\n",option);
if (option=='Y')
while (1)
{
//GATHERS INPUT FROM USER.
printf("Please enter the amount of Cents that needs to be returned to the customer: \n");
scanf("%d", &money);
if ((money < 5) || (money > 95))
printf("You cannot enter an amount less then 5 or greater than 95."\n);
return (1);
else
{
for (i=0; i<money; i+5) //really need help here :[
{
//FINDS HOW MANY OF EACH COIN WILL BE NEEDED.
fifty = money / 50;
result %= 50;
twenty = money / 20;
result %= 20;
ten = money / 10;
result %= 10;
five = money / 5;
result %= 5;
//AMOUNT OF EACH COIN BEING RETURNED.
printf("To make change for %d, you will need the following coins:\n", money);
if (fifty > 1)
printf("%d Fifty cent piece's.\n", fifty);
if (fifty == 1)
printf("%d Fifty cent piece.\n", fifty);
if (twenty > 1)
printf("%d Twenty cent piece's.\n", twenty);
if (twenty == 1)
printf("%d Twenty cent piece.\n", twenty);
if (ten > 1)
printf("%d Ten cent piece's\n", ten);
if (ten == 1)
printf("%d Ten cent piece\n", ten);
if (five > 1)
printf("%d Five cent piece's\n", five);
if (five == 1)
}
}
//IF USER ENTERED 'N' FOR NO, PROGRAM QUITS.
while (option!='N');
return(0);
}
/*******************************************************************************
********************************************************************************
MAIN PROGRAM ENDS HERE
********************************************************************************
*******************************************************************************/
question 2b is:
You are to extend your solution to part (a) above by allowing the user to enter the amount of change as dollars and cents as a double (real number). Valid dollar values are 100, 50, 20, 10, 5 and 1. You program should output the correct combination of dollar and cent denominations required.
In Part b you will need to explicitly convert a double to an integer to break the inputted double down to its component parts as integers. You can use the following code as the basis for doing this:
float change;
int dollar, cents;
...
dollar = (int) change;
cents = (int) rint(((change - dollar)*100));
Note to use the rint() function you will need to include the math.h file.
Again your solution for this problem should be modular.
for question 2b I have done so far:
Code:
/********************************************************************************
Author: Steve
Date: April 2004
Purpose: ICT 102, Assignment Program Three, Dollars and Cents.
********************************************************************************/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <ctype.h>
/*******************************************************************************
********************************************************************************
MAIN PROGRAM STARTS HERE
********************************************************************************
*******************************************************************************/
int main()
{
//Variables
float money;
int i, total, dollars, fifty, twenty, ten, five;
char option;
printf("This program calculates the amount of cents, and what type of coins need to be returned to the customer.");
do
{
printf("\n\nDo you wish to continue? (Y/N): ");
//MAKES UPPER OR LOWER CASE CHARACTERS INTO THIER OPPOSITE VALUE.
do
{
option = getch();
option = toupper (option);
}while (!((option=='Y')||(option=='N')));
printf("%c\n",option);
if (option=='Y')
while (1)
{
//GATHERS INPUT FROM USER.
printf("Please enter the amount of Dollars and Cents that needs to be returned to the customer: \n");
scanf("%f", &money);
if (money < 5)
printf("You cannot enter an amount less then 5."\n);
return (1);
else
{
for (i=0; i<money; i+5) //I guessed at this and know its wrong, but i cant think of how to make it read in increments
{ //of 5+ at a time, instead of 1,2,3,4 (Australia doenst have anything less than 5 cent coins).
//I suppose i need to find some way of rounding (money) off to the nearest '5'.
//FINDS HOW MANY OF EACH COIN WILL BE NEEDED.
dollars = total / 100
total %= 100;
fifty = total / 50;
total %= 50;
twenty = total / 20;
total %= 20;
ten = total / 10;
total %= 10;
five = total / 5;
total %= 5;
//CONVERTING FLOAT (AMOUNT) INTO AN INT
??? need help here to :/
//AMOUNT OF EACH COIN BEING RETURNED.
printf("To make change for %f, you will need the following coins:\n", money);
if (dollars > 1)
printf("%d Dollars.\n", dollars);
if (dollars == 1)
printf("%d Dollar.\n", dollars);
if (fifty > 1)
printf("%d Fifty cent piece's.\n", fifty);
if (fifty == 1)
printf("%d Fifty cent piece.\n", fifty);
if (twenty > 1)
printf("%d Twenty cent piece's.\n", twenty);
if (twenty == 1)
printf("%d Twenty cent piece.\n", twenty);
if (ten > 1)
printf("%d Ten cent piece's\n", ten);
if (ten == 1)
printf("%d Ten cent piece\n", ten);
if (five > 1)
printf("%d Five cent piece's\n", five);
if (five == 1)
}
}
//IF USER ENTERED 'N' FOR NO, PROGRAM QUITS.
while (option!='n');
return(0);
}
/*******************************************************************************
********************************************************************************
MAIN PROGRAM ENDS HERE
********************************************************************************
*******************************************************************************/
This is Homework, so I will understand if you people cannot help with this too much, but any help at all would be great
Sorry for this long post.