"...redeclared as different kind of symbol"?
Asked Answered
G

7

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'"...

Grouping answered 20/10, 2013 at 3:52 Comment(7)
Use braces - Otherwise you get caught with you trousers down - this is a good example!Strake
@CodeBewb:- Make sure you have main() defined. Check my updated answer!Pfeiffer
Just a question, if you are just going to be using variable settings in the function, why are you accepting them as parameters?Playoff
Fix this 1/2. This is integer division and the result is 0. Use 0.5 directly or 1.0/2 at least.Heflin
You need to have a function named main somewhere in your program.Righthander
Or maybe you used the function name as a variable somewhere!Eeg
'WinMain' is the entry point for Windows programs; 'main' for console programs. Every exe needs an entry point. If you want a console program check your project settings or use the wizard from Visual Studio and select console program.Prog
C
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;

Calchas answered 20/10, 2013 at 3:57 Comment(0)
R
3

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

Rabi answered 20/10, 2013 at 4:1 Comment(0)
R
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);


Roomful answered 20/10, 2013 at 4:25 Comment(12)
But in the for loop, x starts from "low"...?Grouping
but calculation of s is outside the loop na?Roomful
double s = 1/2*exp((-xx)/2); // here value of x is not assigned.. for(x=low;x<=high;x++) { delta_x = x+delta_x; ans = ans+(delta_xs); }Roomful
How do I do that? I tried setting x as 0 initially but it still doesn't work. I even tried moving the s into the loop.Grouping
when i am trying to find the value of exp(-2), i got zero value.. and exp(2) of 7.38.. please print and check the value of s and ans inside loopRoomful
i call this function and it is compiled .. no error.. but the ans returning is zero..Roomful
How did you do it? Ok, I did: double x; double s; for(x=low;x<=high;x++) { delta_x = x+delta_x; s = 1/2*exp((-xx)/2); ans = ans+(delta_xs); printf("The integral of the function is equal to %f\n", ans);Grouping
It just says "undefined reference to WinMain@16"Grouping
from where you call this function? is it from main() ?Roomful
please check it also #13460412Roomful
I'm not sure what you mean by main()? How do I add that to my code? (Sorry...totally clueless)Grouping
check it also..it may help u.. #5260214Roomful
P
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

Pfeiffer answered 20/10, 2013 at 3:56 Comment(2)
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...Grouping
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!Pfeiffer
R
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.

Rendon answered 25/12, 2018 at 5:57 Comment(0)
E
0

In double integrateF(double low, double high) you already declared low and high then inside of the integrateF you declare them again as double so that's the reason why you get error of redeclaration of low and high you should erase the declaration inside of the integrateF and change double integrateF(double low, double high) to double integrateF(double low = 0, double high = 0)

Ergener answered 10/5, 2024 at 18:12 Comment(1)
While assigning default values to those parameters will likely fix the warning, it's unclear how this function is to be called. You might edit and add some explanations.Emphasize
J
-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;
    .
    .
    .
}
Jacob answered 20/10, 2013 at 6:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.