3 Replies - 171831 Views - Last Post: 10 November 2006 - 12:37 AM Rate Topic: -----

#1 intaminfan628  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-November 06

scanf/printf format specifiers for double data types

Posted 09 November 2006 - 10:37 PM

 #include<stdio.h>
#include<math.h>
double intgr(double m, int n, double A, double a, double b);
double func(double x, double , int n, double a);
double nfact(int n);
int main ()

{

		double a, z, b, A, x, m, sum2;
		int n;
		printf("Enter the exponents (double)m and (int)n in f(x)=A*x^m/n!:");
		scanf("%f %d", &m, &n);
		printf("\n\n Enter the coefficient A in  f(x)=A*x^m/n!:");
		scanf("%f", &A);
		printf("\n\nEnter the bounds for the integration interval, a < b:");
		scanf("%lf %lf", &a, &b);
		printf("\n\n Integrate f(x) on [a,b]\n");
		printf("m=%f\n", m);
		z= intgr(m, n, A, a, b);
		printf("The value of the integral is %lf\n", z);


}

		double intgr(double m, int n, double A, double a, double b)

{
				int trap, k, fact;
				double sum2, f, del_x, x, z;
				printf("Enter the number of trapezoids:");
				scanf("%d", &trap);
				printf("%f %lf %f %d %f \n", m, a, b, trap, A);

				del_x=(b-a)/trap;
				sum2=0;
				f=a*pow(x,m)/fact;
				sum2=-.5*f;

				for(k=0; k<=trap; k++)
						{

						fact=1;
						for(b=n; b>1; b--)
						 {	  fact=fact*b;}
								printf("%lf=factorial\n", fact);
								x=k*del_x;
								f=a*pow(x,m)/fact;
								sum2=sum2+f;
								z=sum2;
								printf("%lf = integral\n\n", z);
								return z;
						}




i am using m=3.25
n=5
A =-1.5
a=1.5
b=3.75

When printf prints m it says m=64 and A=-1.5 why is this happening? Also the integral does not print out a number value, it only prints 0.000.

I am sure there are other problems, but i need to work the others out first.

Thanks in advance.

edit: modified title ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: scanf/printf format specifiers for double data types

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: scanf/printf format specifiers for double data types

Posted 09 November 2006 - 10:46 PM

When taking input for type double you need to use lf format specifier in the scanf statement. But when you are outputting a double you use f format specifier in the printf statements.
Was This Post Helpful? 1

#3 intaminfan628  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-November 06

Re: scanf/printf format specifiers for double data types

Posted 09 November 2006 - 11:23 PM

Great, thanks, but now how come the integral and fact (factorial) loop both =0, the integral should = -0.792905
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: scanf/printf format specifiers for double data types

Posted 10 November 2006 - 12:37 AM

the following now gets fact OK
#include<stdio.h>
#include<math.h>
double intgr(double m, int n, double A, double a, double b);
double func(double x, double , int n, double a);
double nfact(int n);
int main ()

{

		double a, z, b, A, x, m, sum2;
		int n;
		printf("Enter the exponents (double)m and (int)n in f(x)=A*x^m/n!:");
		scanf("%lf %d", &m, &n);
		printf("\n\n Enter the coefficient A in  f(x)=A*x^m/n!:");
		scanf("%lf", &A);
		printf("\n\nEnter the bounds for the integration interval, a < b:");
		scanf("%lf %lf", &a, &b);
		printf("\n\n Integrate f(x) on [a,b]\n");
		 printf("%f %d %f %f %f \n", m, n, A,  a, b);
		z= intgr(m, n, A, a, b);
		printf("The value of the integral is %f\n", z);


}

		double intgr(double m, int n, double A, double a, double b)

{
				int trap, k, fact;		
				double sum2, f, del_x, x, z;
				printf("Enter the number of trapezoids:");
				scanf("%d", &trap);
				printf("%f %f %f %d %f \n", m, a, b, trap, A);

				del_x=(b-a)/trap;
				printf("%f %f %f %d %f \n", m, a, b, trap, A);
				sum2=0;
				f=a*pow(x,m)/fact;  // ***** x and fact not initialised  ???
				sum2=-.5*f;		
				printf("%f %f %f %d %f \n", m, a, b, trap, A);

				for(k=0; k<=trap; k++)
						{

						fact=1;
						for(b=n; b>1; b--)
						 {	  fact=fact*b;}
								printf("%d=factorial\n", fact);
								x=k*del_x;
								f=a*pow(x,m)/fact;
								sum2=sum2+f;
								z=sum2;
								printf("%f = integral\n\n", z);
								return z;
						}
						}



but in the statement
f=a*pow(x,m)/fact;
x and fact have not been initialised (remember C does not initialise local variables inisde {})
initialise these and it may work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1