Jump to content

C code homework help (syntax error)

JewishBacon

this is my code that i am working on, but i cant seem to get it to work. *warning math terms inbound* the end goal is to take the integral of x *ln(x) from [0,1] using simpsons method which should be 1/4. basically im taking the area under a curve by using a formula for those of you that dont know what an integral or simpsons rule is.  ANYWAY this program returns fx to be zero, and since the sum is started at zero i get zero for my answer, the odd part is the 3 terms that make up fx each have a nonzero value... i think my issue is a syntax error, but i am new to C and cant really see where the error is

 

#include <stdio.h>#include <math.h> int main(){double g,x,count,sum,fx,t1,t2,t3,r,m;sum=0;r=10;m=logf(6);        for (count=1;count<=r;count++)        {        x=count/r;        t1=x*logf(x);        g=x+1;        t2=4*((g/2)*logf (g));        t3=1*logf (1);        fx=(1/6)*(t1+t2+t3);        sum=sum+fx;        printf ("sum =%lf  x=%lf  fx= %lf t1=%lf   t2=%lf   t3=%lf \n",sum,x,fx, t1,t2,t3);        } printf ("the requested integral is = ___%lf___\n",sum);return 0;}
Edited by alpenwasser
added code tags

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that (1/6) is an integer division that results in 0. To make it a floating point division change it to (1.0/6.0)

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

I take Pre-calc accelerated Math 10, Have a vast knowlage of HTML, Minecraft plugin coding and Java script and im lost here... xD

My Car: http://linustechtips.com/main/topic/274320-the-long-awaited-car-thread/?p=4442206


CPU: i5 4590 |Motherboard: ASRock H97M PRO4|Memory: Corsair Vengance 8gbs|Storage: WD Caviar Blue 1TB|GPU: ZOTAC GTX 760 2gb|PSU: Thermaltech TR2 500W|Monitors: LG24M35 24" & Dual 19"|Mouse:Razer DeathAdder 2013 with SteelSeries Qck mini|Keyboard: Ducky DK2087 Zero MX Red|Headset: HyperX Cloud|Cooling: Corsair 120mm blue LED, Lepa vortex 120mm, stock 120mm|Case:Enermax Ostrog Blue Windowed


 

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that (1/6) is an integer division that results in 0. To make it a floating point division change it to (1.0/6.0)

ya thats getting me a value at least, but its still not what im going for, do you see anything else? it might be a math error though :(

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that (1/6) is an integer division that results in 0. To make it a floating point division change it to (1.0/6.0)

yep

or just

fx = (t1 + t2 + t3) / 6;

ya thats getting me a value at least, but its still not what im going for, do you see anything else? it might be a math error though :(

no? that 0 is multiplying t1+t2+t3 so it screws up everything

Link to comment
Share on other sites

Link to post
Share on other sites

Also logf(1) is zero which probably isn't what you're expecting.

i would like it to be 0, because i know it is, however c is spitting out 1.21 for t3....

why you do this c?!

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

yep

or just

fx = (t1 + t2 + t3) / 6;

no? that 0 is multiplying t1+t2+t3 so it screws up everything

i have changed the code to this

 

#include <stdio.h>
#include <math.h>
 
int main()
{
double g,x,count,sum,fx,t1,t2,t3,r,m;
sum=0;
r=10;
m=logf(6);
        for (count=1;count<=r;count++)
        {
        x=count/r;
        t1=x*logf(x);
        g=x+1;
        t2=4*((g/2)*logf (g));
        t3=1*logf(1);
        fx=(1.0/6.0)*(t1+t2+t3);
        sum=sum+fx;
        printf ("sum =%lf  x=%lf  fx= %d t1=%lf   t2=%f   t3=%lf \n",sum,x,fx, t1,t2,t3);
        }
 
printf ("the requested integral is = ___%lf___\n",sum);
return 0;
}

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

i would like it to be 0, because i know it is, however c is spitting out 1.21 for t3....

why you do this c?!

are you sure? it gives 0 to me

Link to comment
Share on other sites

Link to post
Share on other sites

 

are you sure? it gives 0 to me

 

 

Use "fg" to return to Pico
 
[4]+  Stopped                 pico -z simpson.c
 
sum =-0.003429  x=0.100000  fx= 286331157 t1=-0.003429   t2=-0.230259   t3=0.209682
sum =0.015851  x=0.200000  fx= 1145324610 t1=0.019281   t2=-0.321888   t3=0.437572
sum =0.069344  x=0.300000  fx= 1861152496 t1=0.053493   t2=-0.361192   t3=0.682147
sum =0.165278  x=0.400000  fx= -858993460 t1=0.095934   t2=-0.366516   t3=0.942122
sum =0.310248  x=0.500000  fx= -536870912 t1=0.144970   t2=-0.346574   t3=1.216395
sum =0.509835  x=0.600000  fx= 1073741824 t1=0.199586   t2=-0.306495   t3=1.504012
sum =0.768912  x=0.700000  fx= -733723580 t1=0.259077   t2=-0.249672   t3=1.804136
sum =1.091831  x=0.800000  fx= -1968526677 t1=0.322919   t2=-0.178515   t3=2.116032
sum =1.482535  x=0.900000  fx= -1281779303 t1=0.390703   t2=-0.094824   t3=2.439045
sum =1.944633  x=1.000000  fx= 0 t1=0.462098   t2=0.000000   t3=2.772589
the requested integral is = ___1.944633___
 
 
 
 
this is what is returned for this code 
 
 
#include <stdio.h>
#include <math.h>
 
int main()
{
double g,x,count,sum,fx,t1,t2,t3,r,m;
sum=0;
r=10.0;
m=logf(6.0);
        for (count=1;count<=r;count++)
        {
        x=count/r;
        t1=x*logf(x);
        g=x+1.0;
        t2=4.0*((g/2)*logf (g));
        t3=1.0*logf(1.0);
        fx=(1.0/6.0)*(t1+t2+t3);
        sum=sum+fx;
        printf ("sum =%lf  x=%lf  fx= %d t1=%lf   t2=%f   t3=%lf \n",sum,x,fx, t1,t2,t3);
        }
 
printf ("the requested integral is = ___%lf___\n",sum);
return 0;
}
 

 

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

i would like it to be 0, because i know it is, however c is spitting out 1.21 for t3....

why you do this c?!

In that case just get rid of it, 1*logf(1) is always 0 and t3 won't have any effect on the rest of the problem.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

sum =-0.003429  x=0.100000  fx= 286331157 t1=-0.003429   t2=-0.230259   t3=0.209682
sum =0.015851  x=0.200000  fx= 1145324610 t1=0.019281   t2=-0.321888   t3=0.437572
sum =0.069344  x=0.300000  fx= 1861152496 t1=0.053493   t2=-0.361192   t3=0.682147
sum =0.165278  x=0.400000  fx= -858993460 t1=0.095934   t2=-0.366516   t3=0.942122
sum =0.310248  x=0.500000  fx= -536870912 t1=0.144970   t2=-0.346574   t3=1.216395
sum =0.509835  x=0.600000  fx= 1073741824 t1=0.199586   t2=-0.306495   t3=1.504012
sum =0.768912  x=0.700000  fx= -733723580 t1=0.259077   t2=-0.249672   t3=1.804136
sum =1.091831  x=0.800000  fx= -1968526677 t1=0.322919   t2=-0.178515   t3=2.116032
sum =1.482535  x=0.900000  fx= -1281779303 t1=0.390703   t2=-0.094824   t3=2.439045
sum =1.944633  x=1.000000  fx= 0 t1=0.462098   t2=0.000000   t3=2.772589
the requested integral is = ___1.944633___

Well that just don't make no damn sense. The end result is the same as mine where t3 is 0. But whatever value you get for t3 you get it shouldn't be changing.. try a different compiler maybe?

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

i can't really say why, but the error is the warning given by

printf ("sum =%lf  x=%lf  fx= %d t1=%lf   t2=%f   t3=%lf \n",sum,x,fx, t1,t2,t3);

if you put a %lf after fx=, it works

printf prints t3 wrong

must investigate!

 

edit:

when i say "it works", i mean "it displays the real value of t3"

the result is always the same

 

edit again:

it seems like printf says "hey, i'm expecting an int but you're giving me a double. ah, whatever, i'll just print a shitrandom int, and i will place your double in the next substitution that, luckily, is expecting a double"

 

so, those wrong values of t3 are actually values of t2: all the printf parameters are shifted starting from fx

Link to comment
Share on other sites

Link to post
Share on other sites

i can't really say why, but the error is the warning given by

printf ("sum =%lf  x=%lf  fx= %d t1=%lf   t2=%f   t3=%lf \n",sum,x,fx, t1,t2,t3);

if you put a %lf after fx=, it works

printf prints t3 wrong

must investigate!

 

edit:

when i say "it works", i mean "it displays the real value of t3"

the result is always the same

ok well that seems to work for getting t3 to be correct, but this answer is bugging me 

 

ass i make the value for "r" higher, the end value for sum should approach .25, however it just gets larger :(

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

the most current version of the code is as follows 
 

 
 
#include <stdio.h>
#include <math.h>
 
int main()
{
double g,x,count,sum,fx,t1,t2,t3,r,m;
sum=0;
r=10.0;
m=logf(6.0);
        for (count=1;count<=r;count++)
        {
        x=count/r;
        t1=x*logf(x);
        g=x+1.0;
        t2=4.0*((g/2)*logf (g));
        t3=1.0*log(1.0);
        fx=(1.0/6.0)*(t1+t2+t3);
        sum=sum+fx;
        printf ("sum =%lf  x=%lf  fx= %lf t1=%lf   t2=%f   t3=%lf \n",sum,x,fx, t1,t2,t3);
        }
 
printf ("the requested integral is = ___%lf___\n",sum);
return 0;
}
 
 
returns this 
 
 
sum =-0.003429  x=0.100000  fx= -0.003429 t1=-0.230259   t2=0.209682   t3=0.000000
sum =0.015851  x=0.200000  fx= 0.019281 t1=-0.321888   t2=0.437572   t3=0.000000
sum =0.069344  x=0.300000  fx= 0.053493 t1=-0.361192   t2=0.682147   t3=0.000000
sum =0.165278  x=0.400000  fx= 0.095934 t1=-0.366516   t2=0.942122   t3=0.000000
sum =0.310248  x=0.500000  fx= 0.144970 t1=-0.346574   t2=1.216395   t3=0.000000
sum =0.509835  x=0.600000  fx= 0.199586 t1=-0.306495   t2=1.504012   t3=0.000000
sum =0.768912  x=0.700000  fx= 0.259077 t1=-0.249672   t2=1.804136   t3=0.000000
sum =1.091831  x=0.800000  fx= 0.322919 t1=-0.178515   t2=2.116032   t3=0.000000
sum =1.482535  x=0.900000  fx= 0.390703 t1=-0.094824   t2=2.439045   t3=0.000000
sum =1.944633  x=1.000000  fx= 0.462098 t1=0.000000   t2=2.772589   t3=0.000000
the requested integral is = ___1.944633___
 

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

ok well that seems to work for getting t3 to be correct, but this answer is bugging me

i edited my answer, it's a little less vague now, at least we can see a pattern in the behaviour

 

and please@JewishBacon, use the code tag

Link to comment
Share on other sites

Link to post
Share on other sites

i edited my answer, it's a little less vague now, at least we can see a pattern in the behaviour

 

and please@JewishBacon, use the code tag

 

will do but, how do i do the code tag.

 

how do i change the code to make the printf expect a double?

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

will do but, how do i do the code tag.

[code][/code]

how do i change the code to make the printf expect a double?

you already did that

the %lf in the printf format string stands for double

 

and for your problem, are you sure about the maths? i don't know simpson's method, i'm looking it up now, but it doesn't look too much like a right implementation to me

Link to comment
Share on other sites

Link to post
Share on other sites

[code][/code]

you already did that

the %lf in the printf format string stands for double

 

and for your problem, are you sure about the maths? i don't know simpson's method, i'm looking it up now, but it doesn't look too much like a right implementation to me

 

this is a link to the problem i am trying to solve 

 

https://www.wolframalpha.com/input/?i=integrate+x+*+ln+x+from+0+to+1

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

yep

 

but why is simpson's rule implemented as an iterative algorithm?

 

maybe you're looking for something like the trapezoid/rectangle methods

I checked the math and i misunderstood what t1 is supposed to be, i readjusted and this new code is within error for what i should be getting, now i just need to make the code look a little prettier so i can turn it in :) thanks @Ciccioo

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

i mean, consider that i just read about is, so bear with me and be patient, but this is my implementation for simpson's rule

#include <stdio.h>#include <math.h>double  f(double x){    if(x == 0)        return 0;    return x * logf(x);}int main(){    double a = 0, b = 1;        double result = (b - a) / 6 * (f(a) + 4 * f((a + b) / 2) + f(b));    printf ("the requested integral is = ___%lf___\n", result);    return 0;}

result: -0.231049

Link to comment
Share on other sites

Link to post
Share on other sites

this is my code:

 

how do i do the color code thing

 

@Ciccioo

 

 
 
#include <stdio.h>
#include <math.h>
 
int main()
{
double x,count,fx,t1,t2,t3;
 
 
 
        t1=.000000001*logf(0.000000001);
        t2=4.0*((1.0/2.0)*logf (1.0/2.0));
        t3=1.0*log(1.0);
        fx=(1.0/6.0)*(t1+t2+t3);
 
printf ("the requested integral is = ___%lf___\n",fx);
return 0;
}
 
 
i get the same answer you do :) 
 
the confusion you are feeling is normal, welcome to engineering school
 

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

how do i do the color code thing

[code]// put your code hereint somethingfloat something elsefunction a lot of colors[/code]

 

result:

// put your code hereint somethingfloat something elsefunction a lot of colors
Link to comment
Share on other sites

Link to post
Share on other sites


#include <stdio.h>
#include <math.h>
 
int main()
{
double x,count,fx,t1,t2,t3;
 
 
 
        t1=.000000001*logf(0.000000001);
        t2=4.0*((1.0/2.0)*logf (1.0/2.0));
        t3=1.0*log(1.0);
        fx=(1.0/6.0)*(t1+t2+t3);
 
printf ("the requested integral is = ___%lf___\n",fx);
return 0;
}
 
[code]

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×