11
#include <stdio.h>
#include <math.h>

double integrateF(double low, double high)
{
    double low = 0;
    double high = 20;
    double delta_x=0;
    double x, ans;
    double s = 1/2*exp((-x*x)/2);

    for(x=low;x<=high;x++)
        delta_x = x+delta_x;
    ans = delta_x*s;

    return ans;
}

It says that low and high are "redeclared as different type of symbol" and I don't know what that means. Basically, all I'm doing here (READ: trying) is integrating from low (which I set to 0) to high (20) to find the Riemann sum. The for loop looks kinda trippy too...I'm so lost.

EDIT:

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

double integrateF(double low, double high)
{
    low = 0;
    high = 20;
    double delta_x=0;
    double ans = 0;
    double x;
    double s = 1/2*exp((-x*x)/2);

    for(x=low;x<=high;x++)
    {
        delta_x = x+delta_x;
        ans = ans+(delta_x*s);
    }
    return ans;
}

^That still doesn't work, after the braces and all. It says "undefined reference to 'WinMain@16'"...

nwn
  • 583
  • 7
  • 23
CodeNewb
  • 127
  • 1
  • 1
  • 4

6 Answers6

8

You are redefinign low and high inside the function which clash with those defined in the parameters.

The for loop is doing

for(x=low;x<=high;x++)
{
   delta_x = x+delta_x;
}

did you mean it to do

for(x=low;x<=high;x++)
{
   delta_x = x+delta_x;
   ans = delta_x*s;
}

However I think you wanted to do ans += delta_x*s;

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
3

lowand high are already passed as parameters of your integrateF method. But they are redeclared again inside the method. Hence the error.

Siddhartha Ghosh
  • 2,988
  • 5
  • 18
  • 25
1

low and high are already passed as parameters of your integrateF method and they are redeclared again inside the method..

And x is not assigned a value when it is using for the calculation of s..


double x, ans; double s = 1/2*exp((-x*x)/2);


Devi K M
  • 63
  • 7
0

You may want to try like this:-

for(x=low;x<=high;x++)
{                          //Use brackets since you are redefining low and high inside the function
delta_x = x+delta_x;
ans = delta_x*s;
}

or

for(x=low;x<=high;x++)
{                          //Use brackets since you are redefining low and high inside the function
delta_x = x+delta_x;
}

EDIT:-

It says "undefined reference to 'WinMain@16'"

Make sure you have main() or WinMain() defined. Also check that main() is not defined inside of your namespace

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • What do you mean? [Noob here...sorry :( ] The instructions say to *test* it by writing a main function but I should only submit one that contains only the function intergrateF... – CodeNewb Oct 20 '13 at 04:52
  • I didnt get that very clearly. But I think you are missing the main() function since the error which you are getting is because of that! – Rahul Tripathi Oct 20 '13 at 04:53
0

Another way you can cause this error is by "redefining" your function in a code where that nametag is already used as a variable outside of main function - Like so (psuedo code):

double integrateF = 0;

main(){
 // get vars to integrate ...
}

double integrateF(double, double){
  //do integration
}

You don't even have to call the function inside of main to have an error trying to compile, instead, the compiler cannot make any sense of: double integrateF = 0 = (double, double) { }; outside of main function.

-1

when u have declared the data type in the Parameters, you don't have to re-declare them.

instead of

double integrateF(double low, double high)
{
    double low = 0;
    double high = 20;
    .
    .
    .
}

you should do it like this

double integrateF(double low, double high)
{
    low = 0;
    high = 20;
    .
    .
    .
}
dmSherazi
  • 3,743
  • 5
  • 37
  • 62