This site seems like an amazing C++ resource. I am currently a beginner and I am coding my first project. I need to print the following formula, in basic arithmetic. In other words, if I can avoid using pow and abs that would be great:
y = (x^4 - x^3 - 7x^2 + x + 6) / !x-3! + sqrt(5-x)
^ means to the power
!x-3! means absolute value
y ranges from -4 to 3 going up by 0.5
So far I have did the following, but not seems to be calculating
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double x, y;
cout << "x = y= ";
for (x -=4; x = 3; x + 0.5);
{
y = ((x*x*x*x) - (x*x*x) - (x*x*7) + x + 6) / (abs(x-3) + ((5-x)/2));
if (y = 0) cout <<"Y is zero";
}
}
What am I missing, do I need cmath to use abs? Any ideas?
Thank you very much in advance.
Your initial statement, x-=4, sets the value of x to whatever random value it had when it started, minus four, so this is now some random value.
This condition at the end of each loop x = 3 is setting the value of x to 3, and it will leave the value 3 as the condition to be checked, so essentially this is always true, so the loop will run forever.
This x + 0.5 does not change the value of x. It calculates the value of x + 0.5 and then does nothing with the result.
Your for loop is the bit between the ) after the 5, and the ; - which is nothing, so your for loop has no code in it. It just runs forever, doing nothing. Remove the ;
I suspect you might have meant something like:
for (x = 4; x == 3; x = x + 0.5) although what you're trying to set x to at the start is just a guess.
You need to go back and read about for loops, and look at some examples.
if (y = 0) This sets the value of y to zero. Is that what you meant? Did you mean if (y == 0)?
Did sqrt(5-x) really get replaced by ((5-x)/2))? It isn't valid.
If you really must use the square root function, you have two main choices: use the library function, or write your own. I expect that at this stage you would not want to write your own sqrt function.
Although it can be done in just a few lines and using only the basic arithmetic operators + - * / it really would make a project in its own right, and not just a tiny part of this one.
The cout statement was to print the output of Y in terms of X for the values given in the for loop.
I have replaced the for loop with for (x = 4; x == 3; x = x + 0.5) removed the ; and modified if (y == 0) . My program seems to be opening and closing immediately. Any ideas why?
I simply need to calculate that formula with x values starting from -4, -3.5, -3.0, -2.5 and going up until I hit 3.
I than need to print the output for y for each one of those values. Never suspected this to be this intensive.
Also, be careful with floating point numbers and comparisons. These numbers are stored in a binary fraction representation, so they might not always behave how you think. Consider this:
1 2
float a = 0.1; //a == 0.09999997
if (10 * a == 1.0) //fail == 0.9999997
Changing the type to double doesn't really help.
You need to google this stuff. There are particular ways of doing comparisons with floating point comparisons.
But you need to get the other stuff sorted out first.