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.
Related
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 1 year ago.
Improve this question
Why does &x1 cannot be accpeted
int calculte (float a, float b, float c, float &x1, float &x2)
I'm trying to solve quadratic equation, here's my full program
#include<stdio.h>
#include<math.h>
int calculte (float a, float b, float c, float &x1, float &x2){
float delta = b*b -4*a*c;
if (delta <0) {
x1 =x2 = 0.0;
return 0;
} if (delta == 0) {
x1 = x2 = -b/(2*a);
return 1;
} if (delta >0) {
delta = sqrt(delta);
x1 = (-b-delta)/(2*a);
x2 = (-b - delta)/(2*a);
return 2;
}
}
int main () {
float a, b, c, x1, x2;
do {
scanf ("%f %f %f", &a, &b, &c);
}
while (!a);
int ans = calculte (a,b,c,x1,x2);
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f", x1, x2);
}
}
I am new to programming, so hope you guys can explain me more details.
You cannot use references in C.
Instead of that, you should make the arguments to pointers by changing & to *.
Also the function body and calling have to be changed according to that.
#include<stdio.h>
#include<math.h>
int calculte (float a, float b, float c, float *x1, float *x2){ /* change the arguments to pointers */
float delta = b*b -4*a*c;
if (delta <0) {
*x1 =*x2 = 0.0; /* dereference the pointers */
return 0;
} if (delta == 0) {
*x1 = *x2 = -b/(2*a); /* dereference the pointers */
return 1;
} if (delta >0) {
delta = sqrt(delta);
*x1 = (-b-delta)/(2*a); /* dereference the pointer */
*x2 = (-b - delta)/(2*a); /* dereference the pointer */
return 2;
}
}
int main () {
float a, b, c, x1, x2;
do {
scanf ("%f %f %f", &a, &b, &c);
}
while (!a);
int ans = calculte (a,b,c,&x1,&x2); /* add & to get the pointers */
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f", x1, x2);
}
}
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.
Consider my attempt to implement the Babylonian method in C:
int sqrt3(int x) {
double abs_err = 1.0;
double xold = x;
double xnew = 0;
while(abs_err > 1e-8) {
xnew = (2 * xold + x/(xold* xold))/3;
abs_err= xnew-xold;
if (abs_err < 0) abs_err = -abs_err;
xold=xnew;
}
return xnew;
}
int main() {
int a;
scanf("%d", &a);
printf(" Result is: %f",sqrt3(a));
return 0;
}
Result is for x=27: 0.0000?
Where is my mistake?
While the function returns an int, that value is printed with the wrong format specifier, %f instead of %d.
Change the signature (and the name, if I may) into something like this
double cube_root(double x) { ... }
Or change the format specifier, if you really want an int.
Following the explanation from tutorialspoint, which states, that the basic idea is to implement the Newton Raphson method for solving nonlinear equations, IMHO, the code below displays this fact more clearly. Since there is already an accepted answer, I add this answer just for future reference.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
double rootCube( double a)
{
double x = a;
double y = 1.0;
const double precision = 0.0000001;
while(fabs(x-y) > precision)
{
x = (x + y) / 2.0;
y = a / x / x;
}
return x;
}
int main(int argc, const char* argv[])
{
if(argc > 1)
{
double a =
strtod(argv[1],NULL);
printf("cubeRoot(%f) = %f\n", a, rootCube(a));
}
return 0;
}
Here, in contrast to the original code of the question, it is more obvious, that x and y are the bounds, which are being improved until a sufficiently accurate solution is found.
With modification of the line in the while block, where y is being updated, this code can also be used to solve similar equations. For finding the square root, for example, this line would look like this: y = a / x.
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 6 years ago.
Improve this question
The task was to find the Area and Volume of a sphere using functions and an additional task was provided to give a function to find the powers of the radius separately (using another function) and then call it in the functions of area and volume.
I can't figure out the way to call the radius function for different powers.
The formula for Surface area is 4*PI*r(power 2)
The formula for the Volume is 4/3 *(PI)*r(power 3)
#include <stdio.h>
#include <math.h>
#include <conio.h>
#define PI 3.142
float surface(int x);
float volume(int y);
int radius();
int main()
{
int r;
float a, b;
clrscr();
printf("Enter the radius :");
scanf("%d", &r);
a=surface(r);
b=volume(r);
printf("Surface Area =%f", a);
printf("\n");
printf("Volume =%f", b);
getch();
return 0;
}
int radius(int z)
{
int f;
f=z*z;
return (f);
}
float surface(int x)
{
float s;
s = 4*PI*radius(x);
return (s);
}
float volume(int y)
{
float v;
v = (4*PI*radius(y)/3);
return (v);
}
I was also instructed to "Refrain from using Arrays or others methods of solving this question."
I cant figure out the way to call the radius function for different powers. An explanation or the correct way to solve this would be helpful!
radius is not really the best name for a function thet computes the square or power of 3. I would call the function power instead. And to make it do different powers you need the exponent as a parameter. You can then use a loop to calculate the power.
/* Calculate the power for exponents >= 0 */
int power(int radius, int exponent) {
int i;
int result = 1;
for (i = 0; i < exponent; i++) {
result = result * radius;
}
return result;
}
You use it like this:
int radius;
// After assigning a value to radius you can call power() like this:
int square = power(radius, 2);
int cube = power(radius, 3);
int cube(int z){
return z * z * z;
}
or
#include<math.h> // at the top
...
int power(int base, int exponent){
return pow(base,exponent);
}
the formula is pretty complicated. the numerator is num and the denominator is den, in the formula there is a root on the denominator so i have putted den in sqrrt() but sqrrt only accepts doubles
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define LEN 11
// for the following set of x and y find r by the formula ..
float sum(float arr[]);
void main(void)
{ int i;
float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};
float num,den[LEN],r[LEN],xy[LEN],x2[LEN],y2[LEN];
for(i=0;i<LEN;i++)
{
x2[i]=x[i]*x[i];
y2[i]=y[i]*y[i];
xy[i]=x[i]*y[i];
}
num=sum(xy)-sum(x)*sum(y);
for(i=0;i<LEN;i++)
{
den[i]=((LEN*sum(x2)-(sum(x))*(sum(x)))*(LEN*sum(y2))-(sum(y2))*(sum(y2)));
r[i]=num /sqrt(den); /*<----------the problem is here-----> */
}
printf("%f",r);
getch();
}
float sum(float arr[])
{
int i;
float total=0;
for(i=0;i<=LEN;i++)
{
total+=arr[i];
}
return total;
}
Out of sheer boredom I have fixed your code. It is still ugly and extremely inefficient but compiles and should work. I'll leave you or someone else to make it decent.
#include <stdio.h>
#include <math.h>
#define LEN 11
// for the following set of x and y find r by the formula ..
float sum(float arr[]);
int main(void)
{ int i;
float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};
float num,den,r[LEN],xy[LEN],x2[LEN],y2[LEN];
for(i=0;i<LEN;i++)
{
x2[i]=x[i]*x[i];
y2[i]=y[i]*y[i];
xy[i]=x[i]*y[i];
}
num=LEN*sum(xy)-sum(x)*sum(y);
den = (LEN*sum(x2)) - sum(x)*sum(x);
float alpha = sum(y)/LEN - (num/den)*sum(x)/LEN;
printf("beta = %f, alpha = %f\n", num/den, alpha);
for(i=0;i<LEN;i++)
{
float term = y[i] - alpha - (num/den)*x[i];
r[i] = (term*term);
printf("%f",r[i]);
}
}
float sum(float arr[])
{
int i;
float total=0;
for(i=0;i<=LEN;i++)
{
total+=arr[i];
}
return total;
}
To be consistent with the rest of the code, you should presumably be writing:
r[i] = num / sqrt(den[i]);
However, the calculation is not one I recognize. The body of the second loop is going to produce the same result for each value in den and therefore also in r, which is probably not what the question asked for.
You need to give the index den[i] at the denominator....instead in your code you have just passed the base address!
r[i]=num /sqrt(den[i]);
If this is what you want to achieve, which is quite unclear.