Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm very new to c++. I need help on how to:
get percentiles (which I think I'm doing right..?, and
how to add them to another number.
Here is part of my code, I'm not even sure if I'm including the right headers. Thanks.
#include <iostream>
#include <iomanip>
int main() {
double total, tipTotal, taxTotal, cost, tax, tip;
cost = 44, 50;
tax = 100 - 93.25;
tip = 100 - 85;
total = cost + tax + tip;
tipTotal = cost + tax + tip;
taxTotal = cost + tax;
// other code
return 0;
}
First, I think you have a typo.
I think you meant to say 44.50 (with a decimal point).
cost = 44.50;
Because the comma is a valid operator, the compiler does not catch this as an error.
Second, C++ does not handle percentages, so you have to specify percentages as a decimal value.
So 15% is not 15.0 but 0.15.
So your code should say
tax = 1.00 - 0.9325;
tip = 1.00 - 0.85;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This is the program:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double dollars;
double won=dollars/0.00093;
double yuan=dollars/0.16;
cout<<"Enter the amount of dollars you would like converted:"<<endl;
cin>>dollars;
cout<<"South Korean Won: "<<fixed<<won<<endl;
cout<<"Chinese Yuan: "<<fixed<<yuan<<endl;
return 0;
}
Whenever I try this, it comes out as
"South Korean Won: 0.000000
Chinese Yuan: 0.000000".
What should I change?
You should change the order like this:
cout<<"Enter the amount of dollars you would like converted:"<<endl;
cin>>dollars;
double won=dollars/0.00093;
double yuan=dollars/0.16;
Your version has undefined behavior, because you are using uninitialized variable dollars.
Define won and yuan after you input dollar from cin, else won and yuan contains grabage
int main()
{
double dollars;
cout<<"Enter the amount of dollars you would like converted:"<<endl;
cin>>dollars;
double won=dollars/0.00093;
double yuan=dollars/0.16;
cout<<"South Korean Won: "<<fixed<<won<<endl;
cout<<"Chinese Yuan: "<<fixed<<yuan<<endl;
return 0;
}
The line
double won=dollars/0.00093;
means "create variable 'won' and initialize it with value of expression dollars/0.00093.
While your idea how it should work exists as a paradigm in some programming languages, it's not how C++ works. You have to find value of dollars before making such statement in C++.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I was trying to make a C++ program to calculate value of Pi up to 'n' decimal places using the Leibniz formula.
The number 'n' would be entered by the user.
I was successful in creating the Leibniz formula but I am having trouble with the latter part i.e value precise up to 'n' places.
The trouble :-
As more and more terms will continue to add to it, it's digits will keep on changing so how to tell if a particular digit has stopped changing despite the addition of more terms.
The code written so far :-
#include<iostream>
using namespace std;
int main()
{
float s=0;
int w=-1;
for(float i=1;;i=i+2)
{
w=w*(-1);
s=s+w*(1/i);
cout<<s<<endl;
}
return 0;
}
It would be great if things would be kept simple since I am just a beginner at C++.
Thank you very much :)
Since you want to compute Pi up to arbitrary nth digit, you want a library for working with big float numbers; see
C++ library for big float numbers
the real trouble is that Leibniz formula is not useful in the context. The actual precision achieved can be estimated as the last term in the formula
Pi / 4 = 1/1 - 1/3 + 1/5 - 1/7 + ... + (-1)**(n + 1) * 1 / (2 * n - 1) + ...
If, for intance, you want Pi up to 100th digit it means that the residual (and the last term) should be less than 1e-100:
1 / (2 * n - 1) < 1e-100
2 * n - 1 > 1e100
n > 5e99
And as you can see 5e99 loops is by far too much for the modern (super-)computers
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I saw a problem where we had to find the wage. If the hours are less than 40 we pay regular wage ($100/hr) if there is overtime we give 1.5 times the original pay ($150/hr).
The challenge was to solve it without if-else/loops/or ternary operations.
It was solved like this
int hours = /*some_number*/;
int wage = (100*hours) + (50*(hours-40))*(hours>40);
This code works.
(hours>40) returns 1 if hours is greater than 40 and returns 0 if it is less.
I understand that it is some kind of boolean operation, but how does it work and what is this called exactly.
The right way to do it is straight-forward:
int hours = /*some_number*/;
int wage = 100*hours;
if (hours > 40) wage += 50 * (hours-40);
To squeeze it to a single expression, the example takes advantage of the fact that a boolean is either 1 or 0. So x*some_bool evaluates to either x or 0.
In your case, if (hours > 40) then
(50*(hours-40))*(hours>40) == (50*(hours-40)) * 1 == 50*(hours-40)
otherwise it is 0.
(50*(hours-40))*(hours>40) == (50*(hours-40)) * 0 == 0
In general it is less readable to write code this way. The only valid uses IMO are in advanced algebraic transformations used in cryptography or complexity theory.
Well you basically answered your question. The (hours > 40) returns 1 if hours is greater than 40 and returns 0 otherwise.
Maybe more tricky part is the fact that this boolean result is then converted to int implicitly before being multiplied by overtime payment, but that is out of the scope of the question.
So the whole code could be indeed expanded in this way
int hours = /* something */
int wage = 0;
int overtime = hours - 40;
if (hours > 40) {
wage = 100*hours + 50*overtime;
}
else {
wage = 100*hours
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know you're supposed to post code of what you have started when asking questions, but honestly I am completely lost. I am reading a book on C++ to learn (I'm self taught), the book is Sams C++ Primer Plus. I have recently just finished the chapter on type conversions and type casts. Well, I was interested in trying to make the program that converts seconds to days/minutes/seconds. It's a very simple application. I could code the application perfectly, but for some reason the math stumps me. I need help with this, or at least starting the variables and such. Thanks.
Also, I'm sure I am supposed to use the modulo (in C++ modulus) somewhere within the program.
The output of the program is supposed to be similar to this:
"Enter the number of seconds: SECONDS"
"SECONDS seconds = 364 days, 46 minutes, 40 seconds.
The actual values in the output don't matter as long as the conversion is correct.
const int SECSPERDAY=84600;
const int SECSPERHOUR=3600;
const int SECSPERMIN=60;
int days=SECONDS/SECSPERDAY;
int hours=(SECONDS-(days*SECSPERDAY))/SECSPERHOUR;
int mins=(SECONDS-(days*SECSPERDAY)-(hours*SECSPERHOUR))/SECSPERMIN;
int secs=SECONDS%SECSPERMIN;
As your task is connected to type conversions and -casts I would suggest the learning outcome is that dividing with integers drops the remainder and you can get the remainder by modulu operation.
therefor you can use (given SECONDS is the input)
int sec = SECONDS % 60;
int min = (SECONDS / 60) % 60;
int hours = (SECONDS / (60 * 60)) % 24;
int days = (SECONDS / (60 * 60 * 24) % 365;
int years = (SECONDS / (60 * 60 * 24 * 365);
I did not use constants to show the direct depedencies even if this is considered bad practice.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm coding a program to calculate the growth of a bacterial colony until certain point.
Given a "X", that will represent the initial number of bacteria. And given a "Y", that will represent the number limit desired of bacteria in the bacterial colony. Return the number of days and hours that the bacterial colony needs for reaching the limit.
The bacterial colony doubles each hour.
Example.1:
Input: 1, 8
Output: 0, 3
Example.2:
Input: 1000 , 1024000
Output:0, 10
Example.3:
Input: 123, 3453546624536
Output: 1, 10
If the hour calculated returns a fractional number, it must be rounded down.
So far I have written this code:
#include <iostream>
using namespace std;
int main(){
long int binitial, blimit, day, counter=0;
float hour;
cin >> binitial;
cin >> blimit;
while(binitial <= blimit){
binitial = binitial * 2;
counter++;
}
day = counter / 24;
cout << day << " ";
hour = (counter % 24) - 0.5;
cout << (int)hour;
return 0;
}
You can remove the loop by observing that the number of hours is Log2(Y/X). To calculate Log2(A) using the standard functions, calculate log(A)/log(2).
You may need to address precision issues when going from doubles to ints, because the calculations will be approximate. The final expression for the hours may look like this:
int hours = (log(Y/X) / log(2)) + 1E-8; // Add a small delta
Going from hours to days/hours is very simple, too:
cout << hours/24 << " " << hours % 24 << endl;
You can use a long int for hour if you do the following:
hour = counter - (day*24); // The total number of hours minus the number of hours that are in each day.
I don't have a compiler in front of me but you can probably also do something like this:
hour = counter % 24; // this will return the remainder when counter is divided by 24.
If blimit is always a multiple of binitial, the solution is simple:
counter%24 will be always an integer, so you don't have to round it.
In case of day days and hour hours, you only have to do is:
hour = counter%24
A note on the method of calculation: you don't need to iterate if you're only doubling each time. You're just looking for a value of n such that 2n gives the right result.
So, note that ngenerations = log2 blimit - log2 binitial
Once you have the number of generations (as a floating-point number) you can just truncate that to an integer number of hours.