Solved

Problem with a program function in C

Posted on 1998-11-22
2
239 Views
Last Modified: 2010-04-15
hi there. the progrma consists in using the simpson 3/8 rule to
>calculate the integral of the following function: e^sinx from 1 to 3
>the rule for sypmpson is the following:
>
>I=(3/8)*(f1+3f2+3f3+2f4+3f5+3f6+2f7.....+3f20+3f21+f22)
>
> HERE I INCLUDE THE PROGRAM I MADE . IT RUNS BUT DOESNT GIVE THE
>RESULT TO THE INTEGRAL. IT SAYS IT IS zERO WHICH IS NOT.
>rules of the game:
>n=21, lower=1, upper=3, h= upper-lower/n, and I has to be a function.
>
>i will appreciate any help you can give me. this is for this monday.
>thanx,
>pedro
>
>
>#include<stdio.h>
>#include<math.h>
>void main (void)
>{
> float n,upper,lower,counter,counter2;
> float x1,acc,suma,f1,f2,f3,f4,Symp;
> float I(float,float);
> float h;
>
> acc=0;
> x1=1;
> counter=0;
> counter2=0;
> n=21.0000000000;
> lower=1.0000000000;
> upper=3.00000000;
> h=(upper-lower)/n;
>
> while(counter2<7)
> {
>  f1=exp(sin(x1+(counter*h)));
>  counter++;
>  f2=exp(sin(x1+(counter*h)));
>  counter++;
>  f3=exp(sin(x1+(counter*h)));
>  counter++;
>  f4=exp(sin(x1+(counter*h)));
>  suma=(f1+(3*f2)+(3*f3)+f4);
>  acc+=suma;
>  counter2=counter2+1;
> }
>
>Symp=I(h,acc);
>printf("sympson=%f",Symp);
>}
>
> float I(float w, float z)
>{
> float resultado;
> resultado=(3/8)*w*z;
> return (resultado);
>}
>
0
Comment
Question by:milalic
2 Comments
 

Accepted Solution

by:
visualc earned 200 total points
ID: 1254560
Hi!
When resultado is calculating, 3/8 is casting to integer, becouse 3 and 8 are implicite integers.
So you can force a cast to float:

float I(float w, float z)
{
 float resultado;
 resultado=((float)3/8)*w*z;
 return (resultado);
}
It,s works...


0
 

Author Comment

by:milalic
ID: 1254561
thanx for your help, wherecan i get a good source of c knowledge?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question