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 6 years ago.
Improve this question
I'm having trouble on thinking of away to attack this problem.
X is defined below. For n=1,x=0.5,n=2,x=0.833.As you add more terms, X increases. Calculate n for which X becomes larger than 4. First write the algorithm and then implement the code in C.
x= 1/2+1/3+...1/n+1 answer: n = 83
The only thing I'm sure of is that it uses a for loop.At first I was thinking something like
For(int i = 0; i <= n.....
That doesn't seem close though.I dunno..Can I get a hint on where to start?
You will obviously compute the partial sums X.n and stop when X.n<4 and X.n+1>4.
To compute the partial sums, keep an accumulator variable and add the fractions one after the other
n= 0
S= 0
// Repeat the following instructions
n+= 1
S+= 1/(n+1) // Now, S = X.n
Remains to find the stopping condition. As the value of S goes increasing from 0, we will stop as soon as S exceeds 4. In other words, continue as long as S remains below 4.
n= 0
S= 0
while S < 4
n+= 1
S+= 1/(n+1) // Now, S = X.n
Translate that to C syntax.
Remains to look closer at the possibility that X.n = 4.
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 2 years ago.
Improve this question
If I have a string, say "abcdefghij"
How can I iterate and get to characters backwards at a time.
For example
first loop : ghij
second loop: fedc
third loop: ab
Im using C++ but dont feel like its simple enough i should be able to adapt any language
Heres what I have so far:
for(long unsigned int i=0; i<s.length();i+=4){
digits.push_back(std::stoi(s.substr(i, 4)));
}
My issue is that this is left justified, not right justified
In pseudo code:
iterate i from s.length stepping by -4, while i > 0
start = max(i - 4, 0)
part = s.substring(start, 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 4 years ago.
Improve this question
I've been given a task to write program in C using recursion. The program is given an equation as a string, such as "123+123=246".
My task is to determine, whether the equation is right. The catch is, that numbers in the equation are sometimes replaced with a '?', for example "1??+??3=??6". Using recursion, I need to sum all possibilities, when the '?' is replaced with a digit and the equation is right.
Obviously, the solution will be based on trying out all possibilities and only selecting those, that make up the right equation. But I have no idea, how to implement it.
Could anyone give me a hint or reply with a piece of code I could base my solution on ?
Thanks very much !
Here's a general idea:
Basically we want to first of all be able to determine if the equation is right or not
Implement this function
int eval(char * string )
This will return 1 for true 0 for false and -1 when there are still '?'
Now we want write our recursion it will return a string and take a string
char * recursion (char * string)
First we need to see if the string holds a full equation.
Int res = eval(string);
if(res == 1)return string;
else if(res == 0)return "";
If it didn't stop yet that means that it can not determine because of the '?' , we need to find a way to kill them.
etch '?' can be 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9
Let's do a for loop
But first implement the method to replace the first '?' with a number,
char * Replace(char* string,int num);
After you've implemented this we create our for loop
for (int i = 0; i< 10 ; i++){
char * result =recursion(Replace(string , I));
if(Eval(result)==1) ;//we found a right answer add it to our return
}
return ""+ [all right answers we found if we even found ];
Good luck learning !
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 6 years ago.
Improve this question
A line is given and a bunch of points are given. I have to find a point on line for which sum of distances from given points is minimum. I could't find any algorithm to implement in c. Please help, thanks in advance.
Without loss of generality, the line is the X axis (otherwise rotate the whole geometry). Then you want to minimize
Sum √[(X - Xk)² + Yk²]
which you can do by canceling the first derivative
Sum (X - Xk)/√[(X - Xk)² + Yk²] = 0
Unfortunately, this is a nonlinear equation that will require numerical methods.
As a starting approximation, you can use the minimizer of the sum of the squared distances,
Sum [(X - Xk)² + Yk²]
by solving
Sum (X - Xk) = 0
which simply gives the point (X*, 0) where X* is the average abscissa.
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
c program to print minimum number of days required to spread rumour in matrix to all houses located like a matrix of m*n. It takes exactly one day to travel rumour from 1 house to other rumour travels only horizontally or vertically
Here is code i have written but it is not getting accepted might be wrong at higher inputs.1<=m<=100000 and 1<=n<=100000.
here is my code
#include<stdio.h>
int main()
{
long m,n,days;
scanf("%ld",&m);
scanf("%ld",&n);
if(m<1||m>1000000||n<1||n>1000000)
return 0;
if(m==2&&n==2)
printf("3");
else
if((m+n)%2==0)
days=(m+n)/2;
else
days=(m+n)/2+1;
printf("%ld",days);
return 0;
}
The fastest way that the rumour will travel is when the roumor starts in the middle of the matrix.
Since it travels only horizontally or vertically, if it starts from the middle then it will take m/2 + n/2 days to reach a corner in the worst case.
Just try this printf("%d\n",m/2+n/2); it should work.
NOTE: Add 1 if you have to include a day for the starting house.
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 8 years ago.
Improve this question
Today I came across a question. It says that:
You need to find if a number can be expressed as sum of two perfect powers. That is, given x find if there exists non negative integers a, b, m, n such that a^m + b^n = x where 1 <= x <= 1000000 and m > 1, n > 1
Could someone please explain me how this can be done?
I know that we can write something like this:
for(int a = 1; true; a++){
for(int b = 1; true; b++){
// And so on and so forth
}
}
But this is not the very efficient (or correct) way of doing so.
Thanks.
Sometimes, that is the only way to solve these kind of problems. The one you have posted belongs to a class of problems called "one way functions": there is a trivial implementation of the problem in one way...
Given four non negative integers, a, b, m and n, find x so a^m + b^n = x
But the other way...
Given x, find four non negative integers a, b, m and n, so a^m + b^n = x
is non trivial. In fact, it could be impossible to solve it, or your best chance, have to use a force brute algorithm to solve it, which is what you have proposed.