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
Given g(x) = cos(log(2.21-x)) and that x = 0.99, estimate the value given by the nested g(g(g(x)... equation as the nesting approaches infinity (you don't have to actually go to infinity, but just set up a way to recurse this nesting n times or so).
I've tried some basic stuff like g(x) = cos(log(2.21-x))
and then creating a for loop defined by g(x) = g(g(x)). None of this works.
In these attempts I've gotten end of void errors and overflow errors. If anyone can figure out if this question is viable in C, even though C doesn't support "nested" functions, I will be amazed and very thankful.
Recursive solution:
// assuming g(double x) is already defined and returning a double value
double get_g_x_to_n(double x, int n) {
if (n > 1)
return g (get_g_x_to_n(x, n - 1);
return g(x);
}
get_g_x_to_n(0.99, 1024);
Iterative solution:
// assuming g(double x) is already defined and returning a double value
double get_g_x_to_n(double x, int n) {
double cur_operand = x;
for (int i = 0; i < n; i = i + 1) {
cur_operand = g(cur_operand);
}
return cur_operand
}
get_g_x_to_n(0.99, 1024);
Related
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 2 years ago.
Improve this question
I have to do a code that print the average of the numbers in an array. For sum, I use a function that I used in another time to calculate the recursive sum, and only divide by n later. But it doesn't work.
Why should I do?
int aveg(int *a, int n){
if (n == 0){
return 0;
}
else{
return ((aveg(a, n - 1) + a[n-1]) /n);
}
}
Because (a + b + c)/3 != ((a/1 + b)/2 + c)/3.
Instead, you can calculate the sum recursively, then divide by n at the very end.
You can recursively compute the mean (with zero based indices) as follows:
A possible implementation is:
#include <stdio.h>
#include <stddef.h>
double mean(const double a[],const size_t n)
{
if(n==0)
return 0;
return (a[n-1]+(n-1)*mean(a,n-1))/n;
}
int main()
{
double a[]={1,2,3,4,5};
size_t n = sizeof(a)/sizeof(*a);
printf("mean: %f\n",mean(a,n));
}
Compared to your solution:
return ((aveg(a, n - 1) + a[n-1]) /n);
you forgot a (n-1) factor:
return (a[n-1]+(n-1)*mean(a,n-1))/n;
It works like calling itself (function) again and again until you get the true or your desire output.
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 2 years ago.
Improve this question
Here is the problem link. I don't know why uva is showing wrong answer. For comparsion, I downloaded a solution and tried manually for many test cases. My code gives the same answer. Where is the problem?
#include<stdio.h>
#include<math.h>
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int x, n, a;
double b, sum1 = 0, sum3 = 0;
scanf("%d %lf %d", &n, &b, &a);
if (b > 0) {
x = a - 1;
double re = (pow(1 - b, x) * b) / (1 - ((pow(1 - b, x + n) * b) / (pow(1 - b, x) * b)));
printf("%.4lf\n", re);
}
else
{
printf("0.0000\n");
}
}
}
Try to return 0 at the end. There is a convention that if a program has no errors, it returns 0 at the end. Some verification softwares check if your code has run successfully by looking at it's return value.
info:
pow( doubleValue, 0 ) = 1
pow( doubleValue, 1 ) = doubleValue
the expression:
pow(1-probableWin,x )
where x is 1-1 = 0 (first player per your code)
pow(1-probableWin,x )
where x is 1 results in 1-probableWin (my idea for first player)
multiplying the above by:
*probableWin
where x is 0 results in 0 per the OPs code
*probableWin
where x is 1 results in (1-probableWin) * probableWin which is much better.
Suggest using a debugger and walking through the code, to determine where things are going wrong.
BTW: strongly suggest that long calculation of re be broken into (say) 4 or 5 statements so you can see the result of each expression.
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 4 years ago.
Improve this question
Im trying the powers of an integer. For example if my integers is 2 first 10 power is 2^1,2^2...2^10. Im using
while (expnt < 10)
{
preExpnt = expnt;
while (preExpnt)
{
preExpnt *= num;
printf("%lld\n", preExpnt);
}
expnt++;
}
but it doesn't work.`
Here is a way you could achieve your purpose.
int num = 2; // for example
int out = 1;
for (int exp = 1; exp <= 10; exp++)
{
out *= num;
printf("%d\n", out);
}
Remarks about your code:
Your inner while loop is infinite if num and expnt are both different from 0.
Assigning preExpnt to the value of expnt at each step and multiplying by num would display a something like: 1*n 2*n 3*n 4*n ... if expnt starts at 1.
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 4 years ago.
Improve this question
I want to write (a^n-1) in c programming. I wrote pow(a,n-1) and the output is
wrong. How can I solve this problem? Thanks.
Here is my code:
#include <stdio.h>
#include<math.h>
int main() {
float a, r,n;
float sum = 0;
a = 1.04*pow(a, n-1);
r = 1.02*pow(1.04,-1);
n = 2;
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
The correct output should be 286376 but the program showed 2903773
Every C program executes line by line. So, at the time when compiler came on
a = 1.04*pow(a, n-1);
this line, variable a and n was not assigned with any value, resulted in giving you a garbage value...
So, the problem is, you had not assigned values in variables, and still, you were using them.
You have to first assign values in variables before using them. Otherwise, they will pick any garbage value from memory (Any Random number).
Assign value in a and n and try again.
Edit: As chux's comment suggests, if your program is supposed to give 286376 as output, then value of a should be 1 and value of n should be 2.
So, your correct code would be this:
#include <stdio.h>
#include <math.h>
int main() {
float a=1, r, n=2;
float sum = 0;
a = 1.04*pow(a, n-1) ;
r = 1.02*pow(1.04,-1);
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
pow(a, n-1) translates into exp(log(a) * (n-1)) which isn't precisely the same.. You can try to round the output like this:
round(pow(a, n-1))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
This is related to the "Project Euler Problem 14"
As I think, my logic and the code is good enough,but it may take few seconds to give the answer.
But when i run this code, the program stops when "starting_number" is around 103152(i can't remember the number exactly).
Can anybody please have a look at this code and, tell me where and what's wrong with this code.
Here's the code :
#include<stdio.h>
int starting_number;
int number_of_terms;
int j=0,k=0;
int term;
int main(){
for(starting_number=2;starting_number<1000000;starting_number++){
term = starting_number;
number_of_terms = 1;
while(1){
{
if(term%2==0){
term = term/2;
number_of_terms++;
} else if(term%2!=0){
term = 3*term + 1;
number_of_terms++;
}
}
if(term == 1) break;
}
if(j>=number_of_terms) //finding which chain is longer
j=j;
else if(j< number_of_terms) {
j= number_of_terms;
k=starting_number;
}
printf("\n%d",starting_number);
}
printf("\n%d(%d)\n",k,j);
return 0;
}
This one's mildly tricky, but your problem is here:
if(term == 1) break;
If the variable term becomes very large (as it can easily do) then it can overflow the int datatype.
When this happens term becomes negative. The C language modulus of a negative odd number is itself negative. Therefore, the end condition for your loop is never met.
Solve this problem by using a larger data type such as unsigned long long.
A less convoluted version of your code would appear as follows. Note that I have eliminated the global variables (those outside of your main function) because global variables are evil. I've replaced your infinite while-loop with a loop that uses an end condition. I've reduced duplication of code within the while-loop. I've eliminated the j=j case. Since printf is a slow function to run, I've commented out the prinft you had in the for loop, which improves the run-time significantly.
#include <stdio.h>
int main(){
int number_of_terms;
unsigned long long term;
int j=0,k=0;
for(int starting_number=2;starting_number<1000000;++starting_number){
term = starting_number;
number_of_terms = 1;
while(term!=1){
if(term%2==0)
term /= 2;
else
term = 3*term + 1;
number_of_terms++;
}
if(j<number_of_terms){ //finding which chain is longer
j = number_of_terms;
k = starting_number;
}
//printf("\n%d",starting_number);
}
printf("\n%d(%d)\n",k,j);
return 0;
}
And, indeed, using unsigned long long solves the problem.