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))
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
I am new to programming. I am learning how to sum in C language. Please see below code, What am I missing? Why its giving error?
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3, d = 0;
d = a + b + c;
printf(d);
return 0;
}
The "f" in "printf" means "formatted". The first parameter you pass to it has to be the format that you want to print with, not what you want to print. In this case, it seems to me that you likely want to:
printf("%d\n", d);
The %d means that printf should interpret the second parameter as a signed integer (which d actually is). The \n adds a newline (and usually flushes the buffer).
You can learn more about printf and its format by googling for it, or reading a man page about it, or its page in a compiler help file.
you are printing a integer value. In order to print a integer value you have follow this way...
printf("%d",d); // for integer it's %d
// so your progam should looks like this
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3, d = 0;
d = a + b + c;
printf("%d\n", d);
return 0;
}
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 6 years ago.
Improve this question
I am trying to do this problem: http://poj.org/problem?id=1003
#include <stdio.h>
int c;
int a = 0;
int i;
int main()
{
scanf("%.2f", &c);
if (0.01 <= c <= 5.20){
for (i = 1; a < c; ++i){
a += (1/(i + 1));
}
printf("%d card(s)", i + 1);
}
return 0;
}
My code isn't working? For some reason it always returns 2 card(s) no matter what I enter. Can someone find the problem?
Thanks!
Problem 1: This is not how you test if a variable is between two values:
if (0.01 <= c <= 5.20){
The correct way is
if (0.01 <= c && c <= 5.20){
Your code is interpreted as if you'd written:
if ((0.01 <= c) <= 5.20){
(0.01 <= c) will be either 0 or 1, and both of these are less than 5.20, so it's always true.
Problem 2: The variables a and c need to be float, not int, because int variables can't have fractions in them, and %f format in scanf requires that the corresponding argument be a pointer to float.
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 7 years ago.
Improve this question
int main(){
int x;
int sum;
printf("Enter a positive integer: ");
scanf("%d", &x);
do{
sum += (x%10);
x=(x/10);
if((x/10)==0){
sum += x;
}
}
while((x/10)!=0);
printf("%d",sum);
}
Hey, I'm trying to get this to add up each digit within the entered integer, but the code I'm using keeps returning the wrong output. Would someone please help me fix my equation/code, because I'm not sure why the output is incorrect.
in your code
int sum;
is not initialized. use something like
int sum = 0;
Note: local variables are not automatically initialized [to 0 or anything], without explicit initialization their contents will be garbage. Thereby, using sum += (x%10); will lead to read before-write scenario, producing wrong result.
Here is a small math problem:
Somebody gave you ten apples, then someone else gave you two more. How many apples do you have?
The right answer is that this question is impossible to answer, because nobody told you how many apples you had at the beginning.
Your program suffers from the same problem: you failed to initialize sum before starting to add to it, so it has initial "garbage" value.
Changing the declaration to
int sum = 0;
will fix the problem.
Initialize your variable sum -
int sum = 0;
If you don't initialize your variable in C and some other language then the a garbage/random value is assigned to it when it is used in expression for the first time.
Late Answer
Since you've already found the problem with your code, I would like to provide a more concise alternative:
int x;
int sum = 0
int digit;
printf("Enter a positive integer: ");
scanf("%d", &x);
while (x > 0) {
digit = x % 10;
sum += digit;
x /= 10;
}
printf("%d", sum);
x % 10 extracts the last digit of the number, and then x /= 10 truncates the integer by removing the last digit.