Perform a piece of code based on input - c

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;

Related

CS50 Problem Set 1(Cash/Greedy algorithm) getting runtime error: signed integer overflow

#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
int count = 0;
float change;
// prompt the user for input
do
{
change = get_float("Change owed: ");
}
while (change <= 0);
int cents = round(change * 100);
while (change >= 25)
{
cents -= 25;
count ++;
}
while (change >= 10)
{
cents -= 10;
count ++;
}
while (change >= 5)
{
cents -= 5;
count ++;
}
while (change >= 1)
{
cents -= 1;
count ++;
}
printf("%i\n", count);
}
If a delete the "round" function and then replace coins with 0.25 0.10 etc. The program works, but it shows the wrong answer on some inputs.
I can't think of anything. I'm new to programming but I feel like this is really simple it's just my lack of intelligence.
Oh my gah. Can I swear here?? I'm so dumb!! The solution was simple! The problem was: I created an integer "cents" which rounds the "change" value. But in every while loop for each cent type I wrote like (change >= 10) when it should've been (cents >= 10) so the rounding actually happens. Now it works just as intended! Here's the corrected(and a little bit changed) code if somebody need help on this problem set:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float change;
int count = 0;
int total;
// prompt the user for input
do
{
change = get_float("Change owed: ");
}
while (change <= 0); // ask the user for only positive numbers
//rounds the input and stores the value in the variable "total"
total = round(change * 100);
//loops for each type of coins
while (total >= 25)
{
total -= 25;
count ++;
}
while (total >= 10)
{
total -= 10;
count ++;
}
while (total >= 5)
{
total -= 5;
count ++;
}
while (total >= 1)
{
total -= 1;
count ++;
}
//prints the converted(to int) and rounded value
printf("%i\n", count);
}

Can't Print " * " in C

for some reason I am unable to print out "* " in my program. I want to print out * if the condition is met. the condition is if the rainfall that day is greater than the average.
under the mean column, i am getting weird symbols. i tried debugging to decimals and get -112 for ascii. i dont understand but i tried researching!
I am new to C so please be understanding. Just learned like 2 days ago!!.
Here is my code :
//Assignment one 9/20/2018
//Anthony Lomaistro and David Luong
//luongd5#student.lasalle.edu
//lomaistroa1#student.lasalle.edu
#include <stdio.h>
main() {
int n = 0; //counters for the loops
int x = 0; // counter for the loops
int counter = 0; // counter whenever i need to keep track of increments
int days_input = 0; // how many days we are keeping track of
int number_of_days = 0;
double rainfall_input = 0;
double rainfall_amount = 0; // how much rainfall per day
double rainfall_average = 0; // average of rainfall
double rainfall_total = 0;
double rainfall_counter = 0; // count how many days it rained above the average
int correct = 0;
double rainfall_array[50];//array that contains the user input
char rainfall_condition[50]; // array that contains the *
double sum = 0;
double average = 0;
double percent_days = 0; //rainfall % above average
double valid = 0;
double valid2 = 0;
printf("Welcome to Lomaistro and Luong's Rainfall Program \n");
printf("How many days would you like to keep track of? Please enter a value between 1 and 50: #%d:", n + 1);
//printf(number_of_days);
while (valid == 0) {
scanf_s("%d", &number_of_days);
if ((number_of_days > 50) || (number_of_days <= 0)) { // come back to this, this doesnt do anytihng
printf("Invalid value, please enter in a day that is between 1 and 50 \n");
}
else {
valid = 1;
}
}
//getting the user to enter in the rainfall
for (x = 0; x < number_of_days; x = x + 1) {
valid2 = 0;
while (valid2 == 0) {
printf("Enter rainfall (in inches): ");
scanf_s("%lf", &rainfall_amount);
if ((rainfall_amount >= 0) && (rainfall_amount <= 10)) {
valid2 = 1;
rainfall_array[x] = rainfall_amount;
}
else
printf("Please enter in a valid rainfall amount between 1 and 10");
}
}
//computing average
for (n = 0; n < number_of_days; n = n + 1) {
sum += rainfall_array[n];
average = sum / number_of_days;
}
printf("Mean daily rainfall(in inches): %lf", average);
//seeing if the * should be the array or not
for (n = 0; n < number_of_days; n = n + 1) {
if (rainfall_array[n] > average) {
rainfall_condition[n] = "*";
rainfall_counter = rainfall_counter + 1;
}
else
rainfall_condition[n] = "empty";
}
// print out the thing
printf("\n Days \t Amount \t >Mean \n");
printf("==============================\n");
for (n = 0; n < number_of_days; n = n + 1) {
printf("%d \t %f \t %c \n", n + 1, rainfall_array[n], rainfall_condition[n]);
}
percent_days = rainfall_counter / number_of_days;
percent_days = percent_days * 100;
printf("Number of days that rained above average : %f \n", rainfall_counter);
printf("Percentage of days that rained above average: %f%% \n", percent_days);
system("pause");
}
rainfall_condition is an array of char, but you're putting a pointer to a string literal in there when you use "*". Use '*' for a character literal instead. To be more specific, this line:
rainfall_condition[n] = "*";
Should be:
rainfall_condition[n] = '*';
Turn some warnings on in your compiler; the first line (what you have now) isn't valid C code and you should be seeing a diagnostic message to that effect.
Edit: now that I've read more of the code, it appears you want either a * or an empty in that column? In that case you want to change the variable declaration to:
char *rainfall_condition[50]; // array that contains the *
And then change the print statement to:
printf("%d \t %f \t %s \n", n + 1, rainfall_array[n], rainfall_condition[n]);

making greedy algorithm work for every input

I wrote a code for greedy algorithm of cs50. It works for inputs between 0 and 1. How can I make it work to inputs like 4.2 , 6.5 , 8 etc?. I have listed my code below. What should I modify in the program?
#include<stdio.h>
#include<cs50.h>
#include<math.h>
int main()
{
int count , change;
float cents;
printf("Enter the change amount : ");
cents = GetFloat();
cents = cents * 100;
change = round(cents);
count = 0;
while(cents < 0 )
{
printf("Enter a positive number : ");
cents = GetFloat();
}
while (change >= 25)
{
change = change - 25;
count++;
}
while (change < 25 & change >= 10)
{
change = change - 10;
count++;
}
while (change < 10 & change >= 5)
{
change = change - 5;
count++;
}
while (change < 5 & change >= 1)
{
change = change - 1;
count++;
}
printf("Total number of coins used : " );
printf (" %d " , count );
printf("\n");
}
I believe that your problem is that you are using bitwise logical operators. You should use && in your comparisons to compare the values of two expressions. There are other little issues: The loop that guarantees positive input should multiply cents by 100 and round(), before assigning this new value to change. But you don't actually need both change and cents. And you don't need as many comparisons as you have written, and without the extra comparisons, you don't need the logical operators that were causing you trouble in the first place!
Edit
I noticed that a number like 4.20 was being rounded first, then multiplied by 100! Of course this is wrong, giving the same results for 4.20, 4.75, and 4. I changed the code below accordingly, but your original code was doing this part correctly (except in the input validation loop, as mentioned earlier). Now the program correctly handle such inputs.
Here is a cleaned-up version (I don't have the cs50.h library, so there are some small differences):
#include <stdio.h>
#include <math.h>
int main(void)
{
int count;
float cents;
printf("Enter the change amount: ");
scanf("%f", &cents);
cents = (float)round(cents * 100);
count = 0;
while (cents < 0) {
printf("Enter a positive number: ");
cents = (float)round(cents * 100);
}
while (cents >= 25) {
cents -= 25;
count++;
}
while (cents >= 10) {
cents -= 10;
count++;
}
while (cents >= 5) {
cents -= 5;
count++;
}
while (cents >= 1) {
cents -= 1;
count++;
}
printf("Total number of coins used: %d\n", count);
return 0;
}

Change Calculation Program in C

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;

Infinite loop in c due to rounding?

The following code is looping infinitely. I believe it may be a rounding issue though not entirely sure.
I am fairly new to C so not sure why I'm getting the infinite looping that I'm getting. The code seems to make sense though keeps looping.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
int main(void){
float quarter = .25;
int Qtr = 0;
float dime = .10;
int Dm = 0;
float nickel = .5;
int Nck = 0;
float penney = .01;
int Pn = 0;
float change;
float userInput;
float newspaperCost;
do{
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
printf("Amount Tendered: ");
userInput = GetFloat();
printf("You entered $%2.2f\n", userInput);
change = userInput - newspaperCost;
printf("Change: $%2.2f\n", change);
}
while(newspaperCost <= 0);
while(change > 0){
printf("%f\n", change);
while(change - quarter > 0){
change = change - quarter;
Qtr++;
}
while(change - dime > 0){
change = change - dime;
Dm++;
}
while(change - nickel > 0){
change = change - nickel;
Nck++;
}
while(change - penney > 0){
change = change - penney;
Pn++;
}
}
printf("Your change consists of %d quarters, %d dimes, %d nickels, and %d pennies\n", Qtr, Dm, Nck, Pn);
} //end main
You have a logic error in the last while loop.
Instead of
while(change - penney > 0){
change = change - penney;
Pn++;
}
Use
while(change > 0){
change = change - penney;
Pn++;
}
Don't use floating point to represent currency.
Prefer double over float unless you have a good reason.
penny*
You're not using any equality operators with floating point operands, so you probably have a logical error.
Your code:
do{
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
printf("Amount Tendered: ");
userInput = GetFloat();
printf("You entered $%2.2f\n", userInput);
change = userInput - newspaperCost;
printf("Change: $%2.2f\n", change);
} while(newspaperCost <= 0);
Won't exit this loop while newspaperCost <= 0. I'm assuming you aren't inputting a negative number at
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
That's why you have an infinite loop.

Resources