Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm trying to program Composite Simpson's Rule in C. The formula is:
where x_j=a+jh for j=0, 1, ..., n-1, n with h=(b-a)/n; in particular, x_0=a and x_n=b.
For some reason, the first and second loop have the same value. I checked over this a lot of times, but I can't seem to find my mistake.
#include <stdio.h>
#include <math.h>
float f(float);
float a;
float b;
float x;
float h;
int n;
int j;
a=0;
b=2;
n=8;
h = (n - j) / b;
float first;
float second;
int main() {
sum = (h / 3.0f) * (f(h) + f(n));
printf("%f\n", sum);
second = (4.0f) * h * f(a);
}
printf("second sum: %f\n",second );
sum = sum + first + second;
printf("%f\n", sum);
return 0;
}
The answer should be around 3.1 (The value of the final sum)
Your divisions won't probably do what you expect:
(2 / 3) == 0
Dividing int with int will result in int.
Use float constants (2.0f / 3.0f)
Edit:
You still have the same problem with the other n / 2.
And you should use %f when printing floats: printf("first sum: %f\n",first);
The value of the integral is: 3.109337
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
double fracsum (int a, int b, int c, int d){
float sum = 0;
int i;
for (i = 0; i < a; i++) {
sum += a;
}
return sum;
}
int main(void)
{
printf("%.3f %.3f %.3f\n",
fracsum(1,2,2,4),
fracsum(1,4,1,8),
fracsum(4,3,5,6));
return 0;
}
Did you mean:
float fracsum (float a, float b, float c, float d) {
return (a / b + c / d);
}
However the problem was maybe that you cannot divide int variables, you have to use float as argument type..
It's not clear to me exactly what you want. But I'd take a wild-elbow guess
that it's fracsum=(a/b)+(c/d). And if that's indeed what you want, then
double fracsum (int a, int b, int c, int d) {
return ( ((double)a)/((double)b) + ((double)c)/((double)d) ); }
Couldn't be much easier.
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
I have the following equation I need to represent in C:
20 * 2^((1-x)/5)
my c representation is as follows, but it seems like the pow function is always returning a high integer value (1073741824) with my n values ranging from 1-5.
double x = 20 * pow(2.0, (n/5.0));
I assume it is because both arguments are not double values, but I do not see why. Is there a different way to format this equation to get it to work?
I just compiled the example you give and it works
#include <stdio.h>
#include <math.h>
int main () {
for (int n = 1; n <= 5; ++n) {
double x = 20 * pow(2.0, ((1-n)/5.0));
printf("%lf ", x);
}
}
Output
20.000000 17.411011 15.157166 13.195079 11.486984
Make sure you use int n and not unsigned n. In case of unsigned you will get (1 - n) overflow and pow will return inf.
Your compiler is assuming pow() returns an int,
Remeber to #include <math.h> for the proper prototype
#include <math.h>
#include <stdio.h>
// ipow works as pow when the compiler assumes an int return value
int ipow(double base, double exp) {
double res = pow(base, exp);
return *(int*)((void*)&res);
}
int main(void) {
for (int n = 1; n < 6; n++) {
double x = 20 * pow(2.0, ((1-n)/5.0)); // with correct prototype
double y = 20 * ipow(2.0, ((1-n)/5.0)); // when compiler assumes int
printf("n=%d, x=%f, y=%f\n", n, x, y);
}
return 0;
}
See https://ideone.com/XvPWX6
Output:
n=1, x=20.000000, y=0.000000
n=2, x=17.411011, y=422418048.000000
n=3, x=15.157166, y=1240833840.000000
n=4, x=13.195079, y=-1971682192.000000
n=5, x=11.486984, y=-2036727536.000000
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am new to C programming and am writing a program to solve simple differential equations which gives output as the value of x. But I'm not getting the correct result.
I am getting the correct value of the equation, but the value of the differential equation is wrong. The code compiles without any warnings or errors.
#include <stdio.h>
#include <conio.h>
#include <math.h>
float poly(float a[], int, float);
float deriv(float a[], int, float);
int main()
{
float x, a[10], y1, dy1;
int deg, i;
printf("Enter the degree of polynomial equation: ");
scanf("%d", °);
printf("Ehter the value of x for which the equation is to be evaluated: ");
scanf("%f", &x);
for(i=0;i<=deg;i++)
{
printf("Enter the coefficient of x to the power %d: ",i);
scanf("%f",&a[i]);
}
y1 = poly(a, deg, x);
dy1 = deriv(a, deg, x);
printf("The value of polynomial equation for the value of x = %.2f is: %.2f",x,y1);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f",x,dy1);
return 0;
}
/* function for finding the value of polynomial at some value of x */
float poly(float a[], int deg, float x)
{
float p;
int i;
p = a[deg];
for(i=deg;i>=1;i--)
{
p = (a[i-1] + x*p);
}
return p;
}
/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;
for(i=0;i<=deg;i++)
{
ps = pow(x, deg-(i+1));
d[i] = (deg-1)*a[deg-1]*ps;
pd = pd + d[i];
}
return pd;
}
You are making a simple logical error. In the function float deriv(float a[], int deg, float x) It should be d[i] = (deg-i)*a[deg-i]*ps;. So your function would look something like this
/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;
for(i=0;i<=deg;i++)
{
ps = pow(x, deg-(i+1));
d[i] = (deg-i)*a[deg-i]*ps;
pd = pd + d[i];
}
return pd;
}
Good luck for the future.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#include <stdio.h>
main() {
int i;
printf("Celsius Fahr\n");
for(i=0; i<20; i++)
printf("%7d %4.2f\n", i, convert(i));
return 0;
}
int convert(int a) {
float b;
b=(1.8*a)+32;
return b;
}
It gives me as output all the Fahrenheit numbers as zero. What did I do wrong?
Change main() to int main().
Change int convert(int a) to float convert(int a).
Add a function prototype float convert(int); before int main().
Use proper indentation.
Modified code :-
#include <stdio.h>
float convert(int); // Funtion prototype
int main()
{
int i;
printf("Celsius Fahr\n");
for (i = 0; i < 20; i++)
printf("%7d %4.2f\n", i, convert(i));
return 0;
}
float convert(int a)
{
float b;
b = (1.8 * a) + 32;
return b;
}
Output :-
Celsius Fahr
0 32.00
1 33.80
2 35.60
3 37.40
4 39.20
5 41.00
6 42.80
7 44.60
8 46.40
9 48.20
10 50.00
11 51.80
12 53.60
13 55.40
14 57.20
15 59.00
16 60.80
17 62.60
18 64.40
19 66.20
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Here is the C source file i write, after i run it, i input 1 1 -2 1 , but the output is not x1=1.000000,x2=1.000000? the output is x1=2.000000,x2=2.000000 .
#include <stdio.h>
#include <math.h>
void fun(float a, float b, float c);
int main()
{
int n,i;
float a, b, c;
printf("please enter the times you want to try:\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("please enter the a,b,c:\n");
scanf("%f%f%f", &a, &b, &c);
fun(a, b, c);
}
return 0;
}
void fun(float d, float e, float f) {
float x1, x2;
if (e*e - 4 * d*f < 0)
printf("no answer\n");
if (e*e - 4 * d*f >= 0) {
x1 = (-e + sqrt(e*e - 4 * d*f)) / (2 * d); //should work better
x2 = (-e - sqrt(e*e - 4 * d*f)) / (2 * d);
printf("the roots are x1=%f x2=%f\n", x1, x2);
}
}
x1 = (-e + sqrt(e*e - 4 * d*f)) / (2 * d);
x2 = (-e - sqrt(e*e - 4 * d*f)) / (2 * d);