I'm just beginning studying programming and am trying to write a program that will display how many of each denomination of currency is required for any given amount of change.
I'm studying in Japan, so the currency is yen, but I think the basic code is universal. I've seen other similar programs online, but mine has a few extra features which may be the cause of my problem, but I'm not sure.
First the user inputs whether or not there are two-thousand yen bills in the register or not. (as these bills are not common).
Then you enter the total amount due. Then enter how much was paid. it then calculates the change and how much of each denomination and then displays it.
However, after you enter the amount paid, the cursor goes to the next line and just sits there indefinitely. I don't know what's causing this. My only guess is that it's getting stuck in a loop somewhere.
Does anyone see a problem? (*I switched the text to be printed to English)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
//入力
int aru;
printf("Are there 2-thousand yen bills in the register?\n 1.) Yes\n 2.) No\n "); //レジに2千円札が入ってますか?\n 1.) 入ってます\n 2.)入ってません
scanf("%d", &aru);
int total, paid;
printf("Enter Total Price ");//お会計を記入して下さい。
scanf("%d", &total);
printf("Enter Amount Paid ");//お客さんのお支払った合計を記入してください。
scanf("%d", &paid);
//計算
if (paid < total)
{
printf("Insufficiant amount paid\n");//お金を十分にもらいませんでした
}
if (paid > total)
{
int change = paid - total;
int ichi = 0, go = 0, ju = 0, goju = 0;
int hyaku = 0, gohyaku = 0, sen = 0, nisen = 0, gosen = 0;
while (change > 5000)
{
change - 5000;
gosen++;
}
while (change > 2000)
{
if (aru == 1)
{
change - 2000;
nisen++;
}
else
{
nisen = 0; //skips calculating 2000 yen bills if answer was 'no'
}
}
while (change > 1000)
{
change - 1000;
sen++;
}
while (change > 500)
{
change - 500;
gohyaku++;
}
while (change > 100)
{
change - 100;
hyaku++;
}
while (change > 50)
{
change - 50;
goju++;
}
while (change > 10)
{
change - 10;
ju++;
}
while (change > 1)
{
change - 1;
ichi++;
}
//出力
printf(" %d \n", gosen);
printf(" %d \n", nisen);
printf(" %d \n", sen);
printf(" %d \n", gohyaku);
printf(" %d \n", hyaku);
printf(" %d \n", goju);
printf(" %d \n", ju);
printf(" %d \n", go);
printf(" %d \n", ichi);
}
return 0;
}
while (change > 5000) //This is an infinite loop
{
change - 5000; //no change is made to change
gosen++;
}
You might want change -= 5000; instead of change - 5000;
This is at several places in your code.
change-=5000 is equivalent to
change = change-5000;
Related
I need your wisdom. Looks not a big problem but I need a way.
First, I will share code. This code is correct but I need some addition, inside for loops have criteria if voltages are bigger than percentage it's ok but all be correct I need just one writing. I have 2 loops but need just one prompt.
If it is confusing I can share original question. Thank you guys.
I put original question:
Voltage readings are obtained from an electrical substation once every hour for six hours (so there are six
readings). Write a C program to perform the following checks on the substation:
a) display all voltages that differ from the average by more than 10% of the average.
b) display all pairs of consecutive hours where the change from the voltage at one hour
to the next is greater than 15% of the average.
Example 1
Enter 6 voltages: 210.1 223.2 189.6 206.2 235.1 215.0
The average is 213.2 volts.
10% = 21.3 volts.
15% = 32.0 volts.
The following problems occurred:
1. Voltage at hour 3 was 189.6 volts (difference of 23.6 volts).
2. Voltage at hour 5 was 235.1 volts (difference of 21.9 volts).
3. Voltage change from hour 2 to hour 3 was 33.6 volts.
Example 2
Enter 6 voltages: 233.1 201.0 221.5 240.2 222.7 208.1
The average is 221.1 volts.
10% = 22.1 volts.
15% = 33.2 volts.
No problems were encountered.
#include <stdio.h>
#include <math.h>
#include <string.h>
int i;
float volt[6];
float avg, avg10, avg15, total, a, b;
int main () {
total= 0 ;
avg = 0;
printf("Enter 6 Volts of Machine\n");
for ( i=0; i<6; i++) {
printf("Type %d. volt", i+1);
scanf("%f",&volt[i]);
total = total + volt[i];
}
avg = total/6;
avg10 = (avg * 10) / 100;
avg15 = (avg * 15) / 100;
printf("------------------------------------------\n");
printf("The machine Avarage Voltage is %.2f\n", avg);
printf("The Machine Avarage is%.2f\n", avg10);
printf("The Machine 15 Avarage is%.2f\n\n\n", avg15);
for (i=0;i<6;i++) {
a = fabs(volt[i] - avg);
if( a > avg10 ) {
printf("\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\n\n", i+1, volt[i], a);
}
}
for (i=0; i<5; i++) {
b = fabs(volt[i+1] - volt[i]);
if( b > avg15) {
printf("\nVoltage change from hour %d to hour %d was %.2f\n\n", i+1, i+2, b);
}
}
If you need just one loop try something like this:
for (i=0;i<6;i++)
{
if((a = fabs(volt[i] - avg)) > avg10 )
{
printf("\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\n\n", i+1, volt[i], a);
}
if((i < 5 && (b = fabs(volt[i+1] - volt[i])) > avg15 )
{
printf("\nVoltage change from hour %d to hour %d was %.2f\n\n", i, i+1, b);
}
}
If you want to print out a message when no problems were encountered, you must remember if any or how many errors were reported. You cannot print out such messages inside the loop, of course, because saying "No errors occurred" eight times and reporting thre errors is a bit of a contradiction.
Your expected out put shows an enumeration of errors, so it is a good idea to keep a count of errors. Proceed as follows:
Whenever you print an error message, increase the count of errors.
Before you do so, check whether this is the frst error that is reported. If so, print the caption ("The following errors occurred")
If you have checked everything and no errors have occurred, print the success message.
Or, in code:
int nerror = 0;
for (i = 0; i < n; i++) {
double v = fabs(volt[i] - avg);
if (v > avg10) {
if (nerror == 0) {
puts("The following problems occurred:");
}
nerror++;
printf("%d. Voltage at hour %d was %.2f volts "
"(diffrence of %.2f volts)\n",
nerror, i + 1, volt[i], v);
}
}
for (i = 1; i < n; i++) {
double diff = fabs(volt[i - 1] - volt[i]);
if (diff > avg15) {
if (nerror == 0) {
puts("The following problems occurred:");
}
nerror++;
printf("%d. Voltage change from hour %d to "
"hour %d was %.2f\n",
nerror, i, i + 1, diff);
}
}
if (nerror == 0) puts("No problems were encountered.");
Thank you for everyone my question was solved. Happy coding!
Code is :
#include <stdio.h>
#include <math.h>
int i;
float volt[6];
float avg, avg10, avg15, total, a, b;
int main () {
int voltageproblem1 = 0;
int voltageproblem2 = 0;
total= 0 ;
avg = 0;
printf("Enter 6 Volts of Machine\n");
for ( i=0; i<6; i++) {
printf("Type %d. volt", i+1);
scanf("%f",&volt[i]);
total = total + volt[i];
}
avg = total/6;
avg10 = (avg * 10) / 100;
avg15 = (avg * 15) / 100;
printf("------------------------------------------\n");
printf("The machine Avarage Voltage is %.1f\n", avg);
printf("The Machine Avarage is%.1f\n", avg10);
printf("The Machine 15 Avarage is%.1f\n\n\n", avg15);
for (i=0;i<6;i++) {
a = fabs(volt[i] - avg);
if( a > avg10 ) {
printf("\nVoltage at hour %d was %.1f volts (diffrence of %.1f volts)\n\n", i+1, volt[i], a);
voltageproblem1 =1;
}
}
for (i=0; i<5; i++) {
b = fabs(volt[i+1] - volt[i]);
if( b > avg15) {
printf("\nVoltage change from hour %d to hour %d was %.1f\n\n", i+1, i+2, b);
voltageproblem2 = 1;
}
}
if ((voltageproblem1==0)&&(voltageproblem2==0)) {
printf("No problems were encountered.\n\n");
}
}
I am teaching myself C for fun. I looked online for some practice programs, and found one where you take two input's: One with the amount of an imaginary product, and second the amount of money the imaginary customers gives you. Then you return the number of dollars bills, and change you would give to the customer.
This is the complete code which I wrote to solve this problem:
#include <stdio.h>
#include <stdlib.h>
float getDif(float c, float m)
{
// First find the difference between
// c & m (m - c = d)
float dif = m - c;
// Return the difference
return dif;
}
void getChange(float dif, int* cash, float* change)
{
// If difference is less than 0 we have a problem
// because the customer gave us less money than
// the cost of the product
if(dif < 0)
{
fprintf(stderr, "The customer has not given enough money!...\n");
exit(EXIT_FAILURE);
}
// Cash is seen as the difference without any
// decimals. So if the difference is $13.45
// the cash would be 13 since cash is, in my
// mind paper money
*cash = (int) dif;
// Change is seen as the difference - cash
// if the difference is $13.45 the change
// would be .45
*change = dif - *cash;
}
void getCash(int cash, int* oneHundred, int* fifty, int* twenty, int* ten,
int* five, int* one)
{
if(cash == 0)
{
printf("Wow close to exact change! - No need for cash...\n");
// End the function there is no need to continue
return;
}
// Since cash is > 0 we need to determine the bills to return
// to the user
if(cash >= 100)
{
while(cash >= 100)
{
cash = cash - 100;
(void) ++*oneHundred;
}
}
if(cash >= 50)
{
while(cash >= 50)
{
cash = cash - 50;
(void) ++*fifty;
}
}
if(cash >= 20)
{
while(cash >= 20)
{
cash = cash - 20;
(void) ++*twenty;
}
}
if(cash >= 10)
{
while(cash >= 10)
{
cash = cash - 10;
(void) ++*ten;
}
}
if(cash >= 5)
{
while(cash >= 5)
{
cash = cash - 5;
(void) ++*five;
}
}
if(cash >= 1)
{
while(cash >= 1)
{
cash = cash - 1;
(void) ++*one;
}
}
printf("After all loops cash = %d\n", cash);
}
void getCoins(float change, int* quarter, int* dime, int* nickel, int* penny)
{
// To find the correct change we need to turn the
// current format of variable change (0.57) into
// a easier to work with 57.
int tenChange = change * 100;
if(tenChange >= 25)
{
while(tenChange >= 25)
{
tenChange = tenChange - 25;
(void) ++*quarter;
}
}
if(tenChange >= 10)
{
while(tenChange >= 10)
{
tenChange = tenChange - 10;
(void) ++*dime;
}
}
if(tenChange >= 5)
{
while(tenChange >= 5)
{
tenChange = tenChange - 5;
(void) ++*nickel;
}
}
if(tenChange >= 1)
{
while(tenChange >= 1)
{
tenChange = tenChange - 1;
(void) ++*penny;
}
}
printf("After all loops change = %d\n", tenChange);
}
int main(void)
{
// Create variables for the various things we create
float c, m, dif, change;
int cash, oneHundred, fifty, twenty, ten, five, one, quarter, dime, nickel,
penny;
printf("Enter the exact amount of the items (18.37): ");
// Obtain the cost
scanf("%f", &c);
printf("Enter the amount of money given by the customer: ");
// Obtain the money from customer
scanf("%f", &m);
// Obtain the difference of the cost
// And the money given by calling the
// getDif() function
dif = getDif(c,m);
// Send the difference to the getChange()
// function, as well as the pointers
// cash & change which will be used in the
// function and returned back here
getChange(dif, &cash, &change);
// First send the cash variable to the getCash
// function along with pointers for each bill
// The function will calculate the number of bills
// to give to the customer and return each
getCash(cash, &oneHundred, &fifty, &twenty, &ten, &five, &one);
// Print the number of bills to give to the customer
printf("Give the customer %d Hundred doller bill(s)!\n", oneHundred);
printf("Give the customer %d Fifty doller bill(s)!\n", fifty);
printf("Give the customer %d Twenty doller bill(s)!\n", twenty);
printf("Give the customer %d Ten doller bill(s)!\n", ten);
printf("Give the customer %d Five doller bill(s)!\n", five);
printf("Give the customer %d One doller bill(s)!\n", one);
// Second send the change variable to the getCoins
// function along with pointers for each type of
// coin. The function will calculate the number of
// coins to give to the customer and return each
getCoins(change, &quarter, &dime, &nickel, &penny);
// Print the number of coins to give to the customer
printf("Give the customer %d Quarter(s)\n", quarter);
printf("Give the customer %d Dime(s)\n", dime);
printf("Give the customer %d Nickel(s)\n", nickel);
printf("Give the customer %d Penny(s)\n", penny);
printf("%d\n", cash);
printf("%.2f\n", change);
printf("$%.2f\n", dif);
return 0;
}
It is working ok, until I print out the number of Nickels, and Pennies to the customer. Instead of giving me a number which would make sense it is giving me a random string of numbers.
For example this is the output for these inputs:
Enter the exact amount of the items (18.37): 123.32
Enter the amount of money given by the customer: 124.00
Wow close to exact change! - No need for cash...
Give the customer 0 Hundred doller bill(s)!
...
Give the customer 0 One doller bill(s)!
After all loops change = 0
Give the customer 2 Quarter(s)
Give the customer 1 Dime(s)
Give the customer 32768 Nickel(s)
Give the customer 1596836443 Penny(s)
0
0.68
$0.68
Now if the program was working correctly, it should say:
2 Quarter(s)
1 Dime(s)
1 Nickel(s)
3 Penny(s)
But I'm getting those random(?) values. Could this be a problem with me not clearing my pointers? I have never worked with a language that uses them, and I have not learned enough to use them properly.
I tried to set all the cash variables (for example oneHundred) to NULL, but it gives me this compiler error:
change.c:204:14: warning: incompatible pointer to integer conversion assigning
to 'int' from 'void *' [-Wint-conversion]
oneHundred = NULL;
^ ~~~~
Any help would be amazing! I am just beginning to learn this language, and I hope this question will give me some insight on how to write better C in the future! Thank you!!
You don't need to set them to NULL be cause they are not pointers, initialized them to 0 like this
cash = oneHundred = fifty = twenty = ten = five = one = quarter = dime = nickel = penny = 0;
NULL is used to initialized pointers to a special value, so you can test against it to check if the pointer is pointing somewhere or if it doesn't.
I got the following code (see below). Works fine, only I want to make one thing happen. I now ask for user input. If the input is 0 or below zero, I want to make sure the rest of the code does not run.
Any suggestion on how to do this?
#include <cs50.h>
#include <math.h>
#include <stdio.h>
int
main(void)
{
float change = 0;
int quarter = 25, dime = 10, nickel = 5, penny = 1;
int quarterc = 0, dimec = 0, nickelc = 0, pennyc = 0;
printf("For what amount should I perform the calculation\n");
change = GetFloat();
if (change == 0)
{
printf("No change!");
// And code should be terminated!
} else if (change < 0)
{
printf("This is a negative number");
// And code should be terminated!
}
else {
printf("You entered this amount: %.1f\n", change);
}
change = change * 100;
while (change >= quarter)
{
change = change - quarter;
quarterc++;
}
while (change >= dime)
{
change = change - dime;
dimec++;
}
while (change >= nickel)
{
change = change - nickel;
nickelc++;
}
while (change >= penny)
{
change = change - penny;
pennyc++;
}
// print number of minimum quarters, dimes, nickels, and pennies needed
printf( "You owe the following number of coins as change:\n");
printf( "quarters = %d\n" , quarterc);
printf( "dimes = %d\n" , dimec);
printf( "nickels = %d\n" , nickelc);
printf( "pennies = %d\n" , pennyc);
// print total minimum number of coins needed for change
printf("The total number of coins needed for change: %d\n", quarterc + dimec + nickelc + pennyc);
}
Change
// And code should be terminated!
to
return 0;
I am working on an assignment in C where I have to read in multiple people's heights and weights and determine their bmi. I then classify them into their respective bmi categories, but I am getting stuck on how to do this properly, this is my code thus far:
# include <stdio.h>
int main () {
int people;
double bmi, weight, inches;
printf("How many peoples? > ");
scanf("%d", &people);
do {
printf("Enter height (inches) and weight (lbs) (%d left) > ", people);
scanf("%lf %lf", &inches, &weight);
people--;
}
while (people > 0);
bmi = (weight / (inches * inches)) * 703;
if (bmi < 18.5) {
printf("Under weight: %d\n", people);
}
else if (bmi >= 18.5 && bmi < 25) {
printf("Normal weight: %d\n", people);
}
else if (bmi >= 25 && bmi < 30) {
printf("Over weight: %d\n", people);
}
else if (bmi >= 30) {
printf("Obese: %d\n", people);
}
return 0;
}
where am i going wrong? where do i fix this code?
Use some data structure for storing data. You are getting input for more than one people but, finally processed for one person.
And also people--; is done. so people variable is decremented up to zero, which makes while to exit without executing your BMI calculation.
Modified Code:
#include <stdio.h>
#define MAX_PEOPLE 100
int main () {
int people;
double bmi[MAX_PEOPLE], weight[MAX_PEOPLE], inches[MAX_PEOPLE];
int index = 0;
printf("How many peoples? > ");
scanf("%d", &people);
index = people;
do {
printf("Enter height (inches) and weight (lbs) (%d left) > ", index);
scanf("%lf %lf", &inches[index], &weight[index]);
index--;
}while (index > 0);
for(index = 0; index < people; index++)
{
bmi[index] = (weight[index] / (inches[index] * inches[index])) * 703;
if (bmi[index] < 18.5) {
printf("Under weight: %d\n", index);
}
else if (bmi[index] >= 18.5 && bmi[index] < 25) {
printf("Normal weight: %d\n", index);
}
else if (bmi[index] >= 25 && bmi[index] < 30) {
printf("Over weight: %d\n", index);
}
else if (bmi[index] >= 30) {
printf("Obese: %d\n", index);
}
}
return 0;
}
Right now you are processing the same data.
Each time you assign a new value to weight the old one is erased.
You could create multiple variables like so:
double weight1, weight2, weight3, weight4, ...etc (highly unpractical!!)
or
create an array of doubles:
double weight[100];
and refer to each specific double variable like this:
scanf("%lf %lf", inches[0], weight[0]);
scanf("%lf %lf", inches[1], weight[1]);
scanf("%lf %lf", inches[2], weight[2]);
You see where i-m getting at ? you can manipulate the array tru a for loop.
Question : Program that asks the user to enter an item price value and then show how to pay that amount using the smallest number of $ 50,$20, $10,$5, and $1 bills consist.
Example Output:
Enter Price: 187
Enter Amount to pay: 500
Change is : 313
(6)$50 (1)$10 (3)$1
(0)$20 (0)$5
Here's my code: hope you help me , I am having a hard to in knowing the right formula for it..
#include <stdio.h>
#include <conio.h>
#define p printf
#define s scanf
#define g gotoxy
main()
{
clrscr();
int c1,c2,c3,c4,c5;
int price,amount;
float change;
p("Enter Price: ");s("%d",&price);
p("Enter amount: ");s("%d",&amount);
change=amount-price;
p("Change is : %f ",change);
c1=(change/50);
c2=(0);
c3=(change/change);
c4=(0);
c5=(change/change)+2;
g(5,5);p("(%d) Php 50",c1);
g(5,6);p("(%d) Php 20",c2);
g(18,5);p("(%d)Php 10 \t",c3);p("(%d)Php 1",c5);
g(18,6);p("(%d) Php 5 ",c4);
getch();
return 0;
}
You're on the right track:
change should be a int too (that means you should change %f to %d). You would then correctly determine the number of 50's (note that integer division in C truncates). You should look at % (modulus operator) to get the remaining amount of changes after the 50's are dealt with:
Using your example:
change = 313
fifties = 313/50 (6)
change %= 50 (13)
That means set change to the remainder after dividing itself by 50 (change = change % 50)
twenties = change / 20 (0)
change %= 20 (13)
tens = change / 10 (1)
change %= 10 (3)
This should give you the basic idea of the code you need. You just continue this pattern in order of decreasing denomination.
As noted, use better variable names, don't use those defines, and generally stick to one statement per line (add a newline after semi-colons). This will make your code more readable. You're also using more parentheses than needed, but that's not a big deal.
I would suggest define an array which holds the bill denominations, and an initially empty array of bill counts:
int denoms[5] = {50, 20, 10, 5, 1};
int bills[5] = {0, 0, 0, 0, 0};
for(int i =0; i < 5; ++i)
{
bills[i] = /* do something interesting with denoms[i] here */
change = /* more work for you here */
}
/* output answer */
for(int i =0; i < 5; ++i)
{
if (bills[i] > 0)
p("{%d)$%d", bills[i], denoms[i]);
}
p("\n");
for(int i =0; i < 5; ++i)
{
if (bills[i] == 0)
p("{%d)$%d", bills[i], denoms[i]);
}
p("\n");
void changeloop(int* change, int* counter, int amount) {
while (*change > amount) {
(*counter)++;
(*change) -= amount;
}
}
int main() {
clrscr();
int price; printf("Enter Price: "); scanf("%d", &input);
int amount; printf("Enter Amount: "); scanf("%d", &amount);
int change = amount - price;
int fifties, twenties, tens, fives, ones;
fifties = twenties = tens = fives = ones = 0;
changeloop(&change, &fifties, 50);
changeloop(&change, &twenties, 20);
changeloop(&change, &tens, 10);
changeloop(&change, &fives, 5);
changeloop(&change, &ones, 1);
printf("Fifties: %d\n", fifties);
printf("Twenties: %d\n", twenties);
printf("Tens: %d\n", tens);
printf("Fives: %d\n", fives);
printf("Ones: %d\n", ones);
getch();
return;
}
There's work to do, like input validation and error handling. But the basics are here. The code could be refactored to be much more extensible... but meh.