Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to programming can someone tell me where I am going wrong?

Status
Not open for further replies.

workmanj

Technical User
Jul 22, 2003
2
US
Hi,
I am trying to write a program to handle some simple numerical integration schemes and I keep having certain errors. Whenever I try to convert the integer types from float to double the program stops working, I am getting accuracy errors using float so I would like to know what I am doing wrong. Also on the monte carlo routine I am trying to generate a random number between a and b and then convert it back to a floating point but if you run the code you can tell it doesnt work. Any general advice in addition would be helpful. I am trying to self teach myself c for grad school and have no formal background in any languages.

Please see code below:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>



/* use of variable declaration outside of main causes them to be global*/

void simp(void);
void trap(void);
void monte(void);
float f1(float x);
float f2(float x);

main ()
{
int choice;

printf(&quot;\nHello there and welcome to Jared's integration routine\n&quot;);

while(choice != 4)
{
printf(&quot;\nWould you prefer to use 1-The Trapezoidal Method (x^3) 2-Simpson's Method (e^(-x)) 3-Monte Carlo 4-exit\n&quot;);
scanf(&quot;%d&quot;, &choice);
switch(choice)
{
case 1: /*Trapezoidal Method for x^3*/
trap();
break;

case 2: /*Simpson's Method*/
simp();
break;


case 3: /*Monte Carlo Method*/
monte();
break;

}
}
printf(&quot;\n Goodbye &quot;);
}


/*Trapezoidal method*/

void trap(void)
{
float x1, x2, sumt, a, b, step;

printf(&quot;\nWhat is your starting point?\n&quot;);
scanf(&quot;%f&quot;, &a);

printf(&quot;\nWhat is your end point?\n&quot;);
scanf(&quot;%f&quot;, &b);

printf(&quot;\nWhat is your step size?\n&quot;);
scanf(&quot;%f&quot;, &step);

sumt = 0.0;
x1 = a;

printf(&quot;%f %f %f&quot;, a, b, step);

for (x2 = (a + step) ; x2 <= b; x2 = (x2 + step))
{
sumt += ((step)*(f1(x1) + f1(x2)))/2.0;
x1 = x2;
}
printf(&quot;\n The answer is %f&quot;, sumt);
return;
}



/*Simpson's Rule*/

void simp(void)

{
float sums, x1, x2, a, b , step;
int i;

printf(&quot;\nWhat is your starting point?\n&quot;);
scanf(&quot;%f&quot;, &a);

printf(&quot;\nWhat is your end point?\n&quot;);
scanf(&quot;%f&quot;, &b);

printf(&quot;\nWhat is your step size?\n&quot;);
scanf(&quot;%f&quot;, &step);

sums = (f2(a) + f2(b));
x1 = a;

for (x2 = (x1 + step); x2 <= b; x2 = (x2 + step))
{
sums += (2.0*f2(x2));
}

sums = (sums * step)/2.0;
printf(&quot;\n The answer is %f&quot;, sums);
return;

}

/*Monte Carlo Method*/

void monte(void)
{
float summ, yr, f, c = 0.0, rx, rxn, ry, ryn;
int rand();
int i, n, a, b, rxi;

printf(&quot;\nWhat is your starting point?\n&quot;);
scanf(&quot;%f&quot;, &a);

printf(&quot;\nWhat is your end point?\n&quot;);
scanf(&quot;%f&quot;, &b);


printf(&quot;\nHow many iterations would you like?\n&quot;);
scanf(&quot;%d&quot;, &n);

for (i=1; i<=n; ++i)
{

rxi = rand()%(b-a+1)+a;
printf(&quot;\n%d&quot;, rxi);
rxn = rxi/1.0;
printf(&quot;\n%f&quot;, rxn);
yr = f1(rxn);
printf(&quot;\n%&quot;, yr);
ry = rand()/32768.0;
ryn = (yr * ry);
c += (ry <= yr) ? 1.0 :0.;
}
f = (float) c/i;
summ = f*f1(b - a);
printf(&quot;\n The answer is %f&quot;, summ);




return;
}



/* function to evalaute by trapezoidal method*/
float f1(float x)
{
return ( x * x * x );
}

/* function to evalaute by simpson's method*/
float f2(float x)
{
return (exp(-x));
}




 
Well there are a few errors which need to be fixed
Code:
$ gcc -Wall -W hello.c -lm
hello.c:17: warning: return type defaults to `int'
hello.c: In function `main':
hello.c:44: warning: control reaches end of non-void function
hello.c: In function `simp':
hello.c:84: warning: unused variable `i'
hello.c: In function `monte':
hello.c:118: warning: float format, different type arg (arg 2)
hello.c:121: warning: float format, different type arg (arg 2)
hello.c:135: warning: spurious trailing `%' in format
hello.c:135: warning: too many arguments for format
hello.c:113: warning: unused variable `rx'
17 - say int main ( )
34 - say return 0;
84 - just delete the unused var
118 - make the format consistent with the variable - %d for int say
121 - ditto
135 - &quot;\n%&quot; is not a format - perhaps &quot;%d\n&quot; ?



Also in monte(), you want to delete this line
int rand();
There is no need to re-prototype functions which are already prototyped by those include files.
 
Thanks,
I realized I was scaning doubles using %f and not %lf and this fixed that problem.

Would you by any chance know how to write a random number generator which creates double or float values and computes numbers in a prespecified interval?

Thanks,
Jared
 
Well
Code:
double r = (double)rand() / RAND_MAX;
produces pseudo random numbers in the range [0..1.0]
After that, you can scale it to whatever you want.

You should be aware that the standard library rand() is pretty much useless for serious statistical purposes.

This one seems like a good one for statistical purposes (from reading the words - make your own mind up)
It is available as a download
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top