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 9 years ago.
Improve this question
I am trying to do this equation into C++
root[0] = root [0] - F(root[0])/ root[0] - root[1] * root[0] - root[2] * root[0] - root[3]
in this examples there are only 3, changes by user input.
The program is trying to solve polynomial equations hopefully this is enough information.
I have got the top of the Equation work here is what I am came up with:
complex<double> top, bottom;
top = (complex<double>)coefficientArray[1] * (pow (rootArray[0], Degree));
rootArray[0] = rootArray[0] - (top/bottom);
Solving linear equations is alot faster using:
The coefficient matrix
An unknown variable matrix (to which you will solve)
The answer matrix
To find the roots of higher grade equations -with an approximation error- you should use the Newton-Raphson method.
I think what you want is something like
int highestPower = // whatever the user input says is the highest power
for (int i=1; i<=highestPower; i++) {
bottom += root[i] * pow(root[0], i);
}
Related
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 3 years ago.
Improve this question
who knows how to calculate the sum using the do/while operator using the formula below?
I would be grateful for any help.
Your formula can be translated like this:
double i = 1;
double sum = 0;
do{
sum += (1/i + 18*i);
i--;
} while (i < 16);
I'm not really sure that using a do while is the best option, it would probably be better with a for loop like so:
double sum = 0;
for (double i = 1; i < 16; ++i){
sum += (1/i + 18*i);
}
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 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 have been working on this problem and I am having trouble solving it. Not sure where to start, can someone help me?
int below(Plot p, line l);
int above(Plot p, line l);
line findLine(Plot p, line l)
{
}
It's standard binary search problem.
Now think what you need to find?
The liney = k which divide the plot in 2 equal part (based on number of points).
What is the range of values y can take?
Clearly it would be -1 to +1.
Can you calculate if a line satisfies the property desired by the problem?
Yes you can. Just check every point of Plot p and then check how many of those points have y co-ordinate greater than k. As you know total number of points then you will know if the points on two sides of the line are equal.
Approach
Suppose you select a line y=k. Now at this point you see there are more points above than there is below it. Now you will move upward. And if there is more in downside then you will move downward.
double good = -1.0,bad= 1+EPSILON;
for(int iter=0;iter<=100;iter++)
{
mid = (good+bad)/2.0;
if(check(P,mid)) // if equally divides it then it's done
// mid is one answer;
else if( below(p,mid)<above(p,mid))
good=mid;
else
bad=mid;
}
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 7 years ago.
Improve this question
This is the link of the problem.
https://projecteuler.net/problem=8
below is my code.
#include <stdio.h>
int main() {
long i,sum;
long temp = 0;
long arr[1000] = {
// Increasingly large number is ommitted//
// I just add ',' between each numbers//};
for(i=0; i<988; i++){
sum = arr[i]*arr[i+1]*arr[i+2]*arr[i+3]*arr[i+4]*arr[i+5]*arr[i+6]
*arr[i+7]*arr[i+8]*arr[i+9]*arr[i+10]*arr[i+11]*arr[i+12];
if(temp<sum){
temp = sum;
}
}
printf("%ld",temp);
return 0;
}
so I got 2091059712 which seems kind of reasonable answer.
The real problem here is, that you did not account for the size of the product. An integer is 10 digits max (2,147,483,647). So this or something alike might happen:
sum = 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9;
This gives: 2,541,865,828,329 which overflows your integer leading to undefined behaviour.
Use a larger integer type or take a different approach.
That's a brute force solution that will work fine for this size of problem.
Potential improvements:
Split the array on "0", and only test the substrings that are longer than the desired length.
Print out the numbers that ended up being the best substring. That way you can test that it actually is present in the original and the multiplication is done correctly.
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 9 years ago.
Improve this question
I have a signal x(t). I must shift it a/2 and -a/2 and then take the sum of these two shifted signals. How can I write shift of x(t) in time in C++?
For time series data a shift in time is just an offset of n samples, so to sum data which is shifted by +/-a/2 samples:
for (i = a/2; i < N - a/2; ++i)
{
y[i] = x[i - a/2] + x[i + a/2];
}