An algorithm for plotting [closed] - 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 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;
}

Related

Given a string output it in a specific way using recursion? [closed]

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 2 months ago.
Improve this question
Given a string we have to output the string in a special way.
• If the string consists of one char, we output that char normally.
• Otherwise, we divide the string into two equal parts (if the number of letters in the substr is odd, the second part of the substr will be one letter longer than the first), and we output the first part twice and then the second part (according to the same rules).
For example, let's assume that we want to output the string YOGURT. We divide that string into two equal parts: YOG and URT.
How will we output the substr YOG? Again, it will be divided into two parts - Y and OG. The substr Y we output normally (but in the output of the substr YOG we will do it twice), and the substr OG we output as OOG. So the substr YOG we output as YYOOG.
Analogously, the substr URT is going to give the output UURRT. So the string YOGURT is going to be output as YYOOGYYOOGUURRT.
Length of the string can at max be 10000.
Now I tried using a non recursion way to solve this problem but it was way to slow so I have come to an conclusion I have to do this with recursion. And since I don't have that much experience with recursion I would really need some help.
This is very naturally implemented with recursion like so:
void print(std::string_view s) {
if (s.size() <= 1) std::cout << s;
else {
auto m = s.size() / 2;
print(s.substr(0, m));
print(s.substr(0, m));
print(s.substr(m));
}
}

check if there is anything next to the item in board (2D array) [closed]

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 1 year ago.
Improve this question
Hey I'm making simple console game in C++. We have pawns on board (I made it using 2D dynamic array). I want to check if there is space to move our pawn. My simplest and best idea would be if I could check every square next to the pawn. The problem, however, arises when the pawn is in the corner of the board.
(x-1,y), (x, y+1) , (x+1, y) , (x, y-1) [this means every field around our pawn]. My board is 2D dynamic array. So if I check field e.g. array[-1][0] (this means field x: -1, y: 0) of course it goes out of memory range. And my question is, Is it good if I go outside this range anyway but I will make a condition in case of this error?
You can just check whether index is in bounds:
if (x >= 0 && x < SIZE_X && y >= 0 && y < SIZE_Y)
return array[x][y];
else
return nullptr;
Alternatively use try/catch:
try
{
return array[x][y];
}
catch (out_of_range e)
{
return nullptr;
}

why my code is getting time excedded in "decrease to zero" problem [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Trying to solve hackerrank problem.
You are given Q queries. Each query consists of a single number N. You can perform 2 operations on N in each move. If N=a×b(a≠1, b≠1), we can change N=max(a,b) or decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0.
Could you suggest how can I improve my code?
int downToZero(int n) {
int dp[n+1];
dp[0]=0;dp[1]=1;dp[2]=2;dp[3]=3;
for(int i=4;i<=n;i++)
{
dp[i]=dp[i-1]+1;
for(int j=2;j*j<=i;j++)
{
if(i%j==0)
{
int fac=max(j, i/j);
dp[i]=min(dp[i], dp[fac]+1);
}
}
}
return dp[n];
}
The bit that is too slow is:
for(int j=2;j*j<=i;j++)
{
if(i%j==0)
You are scanning linearly to find factors. There has to be a better way. Keeping a list of primes, for example.

Can someone explain please what does this do : n&1? [closed]

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
Here is the code, I know what does it do , but I don't understand, what does the if condition do?
if(n&1)
{
for(i=n/2,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
else
{
for(i=n/2-1,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
return 0;
}
Its a bitwise operator. There's a search term for you!
The & operator provides a mask that "cancels out" bits in the first depending if they're set in the second parameter - so assume N is the number 17, that expressed in binary is 00010001, the number 1 in binary is 00000001, so masking the two together will "blank" the first set of bits, leaving you with N as 00000001.
Basically that particular if statement drops all except the last bit, which is either 0 or 1, so it is a condition detecting if N is odd or even.

C++ - Array Math [closed]

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