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

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)

Related

why does the output change when i change z -1 to z--? [duplicate]

This question already has answers here:
Pre vs Post Increment
(3 answers)
Closed 8 months ago.
I have the following c++ program:
#include <iostream>
using namespace std;
//looping through arrays backwards
int main() {
int a[3] {1, 2, 3};
int x = sizeof(a), y = sizeof(int), z = x / y;
for(int i = z - 1; i >= 0; i--) {
cout << a[i] << " ";
}
return 0;
}
And it outputs 3 2 1. But if I change the first parameter in the for loop to int i = z--;, it outpus 2 3 2 1 and I don't understand why. Aren't z - 1 and z-- supposed to be the same thing? Could someone please explain why? Also, I'm a begginer in C++ and I'm learning via the W3Schools tutorial about it. Thanks!
The expression z-- evaluates to z, then - as a side effect - z is decremented (scheduled according to scheduling rules). This means, you're essentially saying int i = z in your loop (and then decrement z, but it's not used anymore) - therefore, your code has UB. The 2 printed is purely coincidental, anything might be printed or anything could happen in your code. If you'd like to use --, use it as prefix, i. e., int i = --z.

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

C++ Division Error? [duplicate]

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);

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)

C++ code printing strange values [duplicate]

This question already has answers here:
Math-like chaining of the comparison operator - as in, "if ( (5<j<=1) )" [duplicate]
(4 answers)
Closed 8 years ago.
i am having a very silly but strange problem. When i am trying to compile and run the following code my compiler is printing "ggl" but i think it shouldn't. It is strange that after doing so much programming i am stuck here. What exactly is the problem? Can someone please help me out? Thanks in advance !!
#include <iostream>
using namespace std;
int main() {
int t=8;
if(1<t<5){
cout<<"ggl";
}
//cout<<aa;
return 0;
}
This line doesn't do what you think it does
if(1<t<5)
You would have to say
if (1 < t && t < 5)
The first version says
if ((1 < t) < 5)
Which evaluates to
if (true < 5)
if (1 < 5)
Which is always true.
Your if condition effectively says if ((1 < t) < 5), which is always true, because (1 < t) is either 1 or 0 (1 < 8 evaluates to 1).
Since chained comparisons do not (usually) work in C++, you'll need to check the condition differently:
if (1 < t && t < 5) {
cout << "ggl";
}
This
if(1<t<5)
does not do what you think it does. It does not determine whether t is between 1 and 5. You want
if ((1<t) && (t<5))
What it actually does is take the value (1<t) (which will 1 if 1<t and 0 otherwise), then see if that value is less than 5, which it always will be.