I'm just starting to learn c++ i just want to ask how can I loop this? Please, don't give me direct answer give me a clue or just a "work frame" how to solve this problem. I want to solve it on my own.
So I'm kinda getting difficulties in looping decimals only i can loop solid numbers but i have some troubles looping decimal figures.
P.S Im reviewing for an exam 4 hours from now but this might come up, i having difficulties in this types of question.
This is the question:
Shipping Cost Calculator
A mail order company charges $3.00 for handling, free shipping for orders 10 pounds or less, plus $0.25 for each pound over 10. Write a program that repeatedly asks the user for the weight of an order, then writes out the shipping charge. The program stops when a weight of zero or less is entered.
Weight of Order:
5
Shipping Cost: $3.00
Weight of Order
20
Shipping Cost: $5.50
Weight of Order
0
bye
I keep on practicing to this but i seem to find error on a formula
how can I loop 3.25 to 4.50 to 5.75 to 6.00 and so on?
main() {
float a, b, x;
printf("Enter Weight: ");
scanf("%f", &a);
if (a <= 10)
{
printf("Your balance is 3.00");
}
else if (a > 10)
{
for (x =.25; x <= a; x++)
{
printf("Your balance is %.2f \n", a);
a += + .25;
}
}
else if (a == 0)
{
printf("Bye");
}
getche();
}
for ( double x = 3.25; x <= so_on; x += 1.25 ) { /*...*/ }
or
for ( float x = 3.25f; x <= so_on; x += 1.25f ) { /*...*/ }
Related
So in my little game, there is a happiness mechanic, I want it to be capped at 100, and show the player how much happiness they lost due to the overflow, but it keeps showing 100. (I created the integer happiness at the very start), its in a switch statement, all other parts of the switch work well.
case 5:
if (happiness <= 100) {
happiness = happiness + 20;
cout<< "The festival goes well";
if (happiness > 100) {
int lost_happiness = happiness - (happiness%100);
happiness = happiness - (happiness%100);
cout << ", however, happiness has reached the cap of 100. The amount lost is " << lost_happiness;
}
}
break;
Any ideas why?
From what I understand case 5 is always showing 100 and it should . Just think let initial happiness be 95 , you add 20 it becomes 115 .then you are subtracting 115%100 I.e. 15 from it , so the answer will become 100 in every case .
You are using the same formula to calculate both the lost_happiness and new happiness values:
int lost_happiness = happiness - (happiness%100); // Notice the similarity
happiness = happiness - (happiness%100); // in the two RHS codes?
This is incorrect, and the former should just be happiness % 100.
if (happiness <= 100) {
happiness += 20;
cout<< "The festival goes well";
if (happiness > 100) {
int lost_happiness = happiness % 100; // DON'T subtract this from total
happiness -= lost_happiness; // No need to recalculate the "%"
cout << ", however, happiness has reached the cap of 100. The amount lost is " << lost_happiness;
}
}
Note that I have also used some techniques to make your code rather more succinct; please feel free to ask for further details and/or clarification.
This is a class assignment that I can't seem to figure out. The assignment tells us to prompt a user for 2 numbers, those numbers each represent the radius of 2 separate circles. We are then supposed to find the difference between the areas of these circles by subtracting the smaller area from the larger.
I have been fine up to that part, sometimes it works other times it doesn't, but I have been testing it with the numbers 3 and 4, which gives me the areas of 9.42477 and 12.56632 respectively. But the difference keeps coming out as -3.14... and none of the answers are supposed to be negative if you're subtracting the smaller number from the larger number.
#include <stdio.h>
double areaOfCircle(double radius)
{
return radius * 3.14159;
}
int main()
{
double Num1;
double Num2;
printf("Type in a number: ");
scanf("%lf", &Num1);
printf("Type in a second number: ");
scanf("%lf", &Num2);
double areaOfCircle1 = areaOfCircle(Num1);
double areaOfCircle2 = areaOfCircle(Num2);
double areadifference = 0;
// this is where youre supposed to subtract
// the area of the smaller circle form the area of the
//larger circle, but I keep getting a negative number
// when using the numbers 3 (Num1) and 4 (Num 2)
// should a;ways be a postive number or 0.
if (areaOfCircle1 < areaOfCircle2)
{
areadifference = areaOfCircle2 - areaOfCircle1;
}
else (areaOfCircle1 > areaOfCircle2)
{
areadifference = areaOfCircle1 - areaOfCircle2;
}
printf("The difference between the areas is: %lf\n", areadifference);
return 0;
}
Your area calculation is wrong, area of circle is pir^2, but you have pir. And if you had checked what your area values came out as you would have seen that.
Hope you all are having a great day!
I love programming, but these past days I am having sleepless nights, with CodeChef always returning SIGSEGV errors on my Dynamic Programming solutions.
I am solving this question right now. Here's the question -
In Byteland they have a very strange monetary system. Each Bytelandian
gold coin has an integer number written on it. A coin n can be
exchanged in a bank into three coins: n/2, n/3 and n/4. But these
numbers are all rounded down (the banks have to make a profit). You
can also sell Bytelandian coins for American dollars. The exchange
rate is 1:1. But you can not buy Bytelandian coins. You have one gold
coin. What is the maximum amount of American dollars you can get for
it?
Input
The input will contain several test cases (not more than 10). Each
testcase is a single line with a number n, 0 <= n <= 1 000 000 000. It
is the number written on your coin. Output
For each test case output a single line, containing the maximum amount
of American dollars you can make. Example
Input: 12 2
Output: 13 2 You can change 12 into 6, 4 and 3, and then change these
into $6+$4+$3 = $13. If you try changing the coin 2 into 3 smaller
coins, you will get 1, 0 and 0, and later you can get no more than $1
out of them. It is better just to change the 2 coin directly into $2.
Now I know that it's very easy. And I did get stuck initially when I was declaring a big 10^9 integer long array (Over 1GB of memory..whoo!), but coming back to my senses - I decided to do memoization till 10001, and after that simple recursion. But still - I am making a mistake, and it's giving SIGSEGV error.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
long long n[100001];
long long calc(long long x) {
if (x < 10001) {
if(n[x] != 0) return n[x];
n[x] = max(x, calc(x/2) + calc(x/3) + calc(x/4));
return n[x];
}
else return max(x, calc(x/2) + calc(x/3) + calc(x/4));
}
int main() {
memset(n, 0, sizeof(n));
n[1] = 1;
n[2] = 2;
n[3] = 3;
n[4] = 4;
n[5] = 5;
n[6] = 6;
for (int i = 7; i < 10001; i++)
n[i] = calc(i);
int t = 10;
while (t--) {
long long c;
scanf("%lld", &c);
printf("%lld\n", calc(c));
}
return 0;
}
I have solved some previous questions too - and all of them gave me this error once or twice. I know this error means that I am trying to access memory that hasn't been allocated, but what is wrong in my approach that I always get this error?
The problem is with the corner case n=0.
calc(0) recurses indefinitely because 0<10001 and n[0]=0. You need to add the terminating condition that calc(0)=0.
Takeaways:
Always check your programming competition solutions on corner cases.
Always ensure that your recursion does not result in an infinite loop.
C++ question- "Write a program that will calculate total savings by a student with his/her parents’ contribution. The student’s parents have agreed to add to the student’s savings based the percentage the student saved using the schedule given below" This is the if/else if I used to find out the parents contribution. I now have to make this program again except with a switch statement. I don't know exactly how to do that. The user inputs total earnings, and amount he decided to put away. (My course just started so I have to use very simple processes to do this thank you) Here's the first version:
percent_saved = money_saved / money_earned; // calculates the percent of how much was saved
if (percent_saved <.05) // this if/else if statement assigns the parents percentage of contribution to their students saving
{
parents = .01;
}
else if (percent_saved >= .05 && percent_saved < .1)
{
parents = .025;
}
else if (percent_saved >= .1 && percent_saved < .15)
{
parents = .08;
}
else if (percent_saved >= .15 && percent_saved < .25)
{
parents = .125;
}
else if (percent_saved >= .25 && percent_saved < .35)
{
parents = .15;
}
else
{
parents = .2;
}
parentsmoney = parents*money_earned; // using the correct percentage, this creates the amount of money parents will contribute
total_savings = parentsmoney + money_saved; // this adds together the parent's contribution and the student's savings
This can't (shouldn't) be done in this case: switch is only useful for discrete integer values. It is not useful for non-trivial ranges and cannot be used directly with floats.
Anyway, about half the conditionals can be removed from the if-expressions if the ordering reversed such that the tests are inchworm'ed through..
if (percent_saved >= .35) {
parents = .2;
} else if (percent_saved >= .25) {
parents = .15;
} // etc.
Now, if the requirement is to "use a switch statement" (silly homework problems), then consider first normalizing the floating value into "buckets" such that 0.05 => 1, 0.1 => 2, 0.15 => 3, etc. The resulting integer can then be checked in a relevant case (with some cases being fall-throughs), as shown in the linked question..
int bucket = rint(percent_saved / 0.05);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am new to c++. The problem consists in minimizing the number of coins required to give the exact change I have 25 10 5 and 1 cent coins.
For example if a customer is owed $3.20 the number of coins to give would be 14 (12 of 25 and 2 of 10).
My problem:
A number like 4.20 says you need 22 coins instead of 18. I know the problem is generated when it multiplies change by 100. I get 419 instead of 420.
Here is my code.
int coins = change * 100;
//How many 25 cent coins you need
if (coins >= 25)
{
quarter = coins / 25;
coins = coins % 25;
}
//How many 10 cent coins you need
if (coins >= 10)
{
dimes = coins / 10;
coins = coins % 10;
}
//How many 5 cent coins you need
if (coins >= 5)
{
nickels = coins / 5;
coins = coins % 5;
}
//How many 1 cent coins you need
if (coins >= 1)
{
pennies = coins / 1;
coins = coins % 1;
}
NumCoins = quarter + dimes + nickels + pennies;
printf("%d \n", NumCoins);
Thanks for your help.
#include<iostream>
using namespace std;
int main()
{
int amount = 420;
int coins[] = { 25, 10, 5, 1 };
int ncoins = 0;
for( int i=0 ; i<sizeof(coins)/sizeof(int) ; ++i )
{
ncoins += amount / coins[i];
amount %= coins[i];
}
cout << "You need " << ncoins << " coin(s)." << endl;
}
You need 18 coin(s).
It is easy to track which specific coins are needed in the for loop. I assume the reader can adjust the code as needed to suit their purposes.
From my understanding of the problem my suggestion on how to do this is essentially having two variables. change (This is the change you have in cents.) as well as coins (This is the total number of coins you need to make change.)
Then once you have the change, you keep subtracting the quarters (that is 25), from the change variable until it is less than 25, then you move onto dimes, nickels and finally pennies. At the same time you decrement the change variable, you increment the coins in order to keep track of the minimum number of coins you need. This should be much cleaner and simpler than keeping track of all these other variables.
Some pseudocode could look like this:
declare variables
do loop of change > 25
change = change - 25
coins = coins + 1
do loop of change > 10
...
(keep doing this for dimes, nickels and pennies)
...
display number of coins needed.