C++ Division Error? [duplicate] - c++

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 6 years ago.
Hi so whenever I try to do division such as double x = 3 * (5/10); it will make x = 0 for some reason. Is there a reason this happens in c++ I'm learning and have no clue why this happens.

think about this: what data type is 5?
what data type is 10?
INTEGER!!!
then
(int)5 / (int)10 = (int)0.5 = 0
try this
double a = 3;
double b = 5;
double c = 10;
double x = a * (b/c);

Related

How do I stop this loop from repeating forever? [duplicate]

This question already has answers here:
Why is if (2 < 9 < 3) true?
(6 answers)
Closed 11 months ago.
For homework we've been assigned I am trying to use a for loop in c++ but apparently I don't understand how they work that well. Here's the code:
{
double L,W,E,A,H,B,I;
L=6.5;
W=2;
E=450;
A=2.8;
H=0.5;
B=0.8;
I= (1/12)*B*(H*H*H);
for ( double x = 0; 0 <= x <= 2.8; x + 0.1)
{
double F,S,G;
F= -((W)*(x*x))/(24*E*I);
S= (6*(A*A))-(4*A*x)+(x*x);
G= F*S;
cout << G;
}
Basically what I'm trying to do is make a loop where x increases in increments of 0.1 until it hits 2.8, outputting the values for G along the way. But right now it gives me an infinite output of nan. Is there something I'm missing?
Your loop test and increment are wrong, you need
for ( double x = 0; x <= 2.8; x += 0.1)

How to fix rand()/RAND_MAX in a method that always produces 0.0000000? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Integer division always zero [duplicate]
(1 answer)
Random number c++ in some range [duplicate]
(6 answers)
Closed 3 years ago.
I need to produces numbers between 0 and a max (seen in code as assetMax). In the code, the rand()/RAND_MAX always produces 0 and I cannot seem to figure out why. I use the rand() function immediately before it to produce values in a range and it works completely fine. However, here it does not.
I have tried to switch the order of the variables, create the random number in a separate double before multiplying the two, and the header.
void cPortfolio::randomize(cProblem &portfolioProblem) {
int assetCount = 6 * rand() / RAND_MAX + (portfolioProblem.assetMax-8); //this line works as expected
int test;
for (int i = 0; i < assetCount; i++) {
double num = rand() / RAND_MAX; //this always produces 0.0000
int test = num * (portfolioProblem.assetNum); } `} //cannot format these correctly please ignore the brackets

What is the internal functioning of the following code? [duplicate]

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I am trying to understand how the following lines of code work in c++.
int main(){
int i;
i = 1 + (2,3,5,3,6);
cout<<i<<endl;
return 0;
}
Output: 7
Basically, the answer is the sum of 1 and the last integer in between the parentheses.
(2,3,5,3,6) turns out to be 6.
Hence 1 + 6 = 7
You can verify with a print statement
printf("\n%d\n", (2,3,5,3,6));
It will print 6 only.

C++ Random doubles from -1 to 1 [duplicate]

This question already has answers here:
generate random double numbers in c++
(10 answers)
Closed 4 years ago.
I need to randomly generate numbers (using random()) from -1 to 1 that are doubles. For example the output would be something like:
1,
-0.3324,
0.7821,
0.9823,
-0.111
etc... this is what I was trying to do walk_Length = (double)rand()%2 - 1;
You might get away with something like
double walk_Length = static_cast<double>(rand()) / RAND_MAX * 2 - 1;

Convert base 10 into a certain base [duplicate]

This question already has answers here:
Converting numbers between Number Bases
(4 answers)
Closed 7 years ago.
I have a problem.
I want to convert an integer (ex 64) into a certain base (ex 4). The number would look like 100 ( I think ).
I need to get the number as presented before ( 100 ).
If I use the algorithm I thought of I will get 001 which will be transformed into 1. How can I convert the base 10 integer into a base 4 representation starting with the first digit that is not 0.
Sorry for my bad explanation, I hope somebody will understand.
int x=64;
int t=x; // temp variable
int pp=0;
int base=4;
while(t!=0)
{
pp = pp * 10 + t%base;
t/=base;
}
You need to add the data to the front of your value:
int x = 64;
int t = x; // temp variable
int pp = 0;
int base = 4;
int pos = 1;
while (t != 0)
{
pp += (t % base) * pos;
t /= base;
pos *= 10;
}
I hope you only use this for playing around as there are a lot of traps you could fall in (e.g. if base is less than 0 or more than 10)