#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0

    What is the error? (Polynomial interpolation)


    I'm having some small problem with the declaration for xx variable, can someone please find me the mistake?

    The code is about to calculate the value of the n-th order Lagrange's polynomial Pn(x).

    Code:
    #include <stdio.h>
    #include <math.h>
    
    double Lagrange(double x[], double y[], int n, double xx);
    
    void main()
    {
    	const int N=3;
    	double P=0.0;
    	double x[N];
    	double y[N];
    	double xx;
    	
    	printf("%lg, %lg, %lg\n", Lagrange (x, y, N, xx));
    }
    
    double Lagrange(double x[], double y[], int n, double xx)
    {
    	double P=0.0;
    
    	for(int i=0; i<n; ++i)
    	{
    		double L=1;
    		
    		
    		for (int j=0; j<n; ++j)
    		{
    			if (i!=j)
    			{
    				L=L*((xx-x[j])/(x[i]-x[j]));
    			}
    			P=P+y[i]*L;
    		}
    	}
    	return P;
    }
    Thanks in advance.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    Could you elaborate on the problem?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0
    yes, but it can not debug because of the xx value, which is not initialized.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    Whatever makes you think that there's anything wrong with the declaration of xx? Do please be specific.

    IOW, your physician must really love the way that you just walk in and tell him to cure you, but you offer him absolutely no information of any symptoms nor why you even think that you have a condition that needs curing. You wouldn't do it to your doctor, so why do it to us?

    Also, what language are you using? That doesn't even come close to compiling in ANSI C, which is what programmers mean when they say "C":
    C:TEST>gcc -Wall lagrange.c
    lagrange.c:7: warning: return type of `main' is not `int'
    lagrange.c: In function `main':
    lagrange.c:14: warning: use of `l' length character with `g' type character
    lagrange.c:14: warning: use of `l' length character with `g' type character
    lagrange.c:14: warning: too few arguments for format
    lagrange.c:9: warning: unused variable `P'
    lagrange.c: In function `Lagrange':
    lagrange.c:21: parse error before `int'
    lagrange.c:21: `i' undeclared (first use in this function)
    lagrange.c:21: (Each undeclared identifier is reported only once
    lagrange.c:21: for each function it appears in.)
    lagrange.c:21: warning: statement with no effect
    lagrange.c:21: parse error before `)'
    lagrange.c:26: parse error before `int'
    lagrange.c:26: `j' undeclared (first use in this function)
    lagrange.c:26: warning: statement with no effect
    lagrange.c:26: parse error before `)'
    lagrange.c:19: warning: unused variable `P'
    lagrange.c:31: warning: control reaches end of non-void function
    lagrange.c: At top level:
    lagrange.c:32: warning: type defaults to `int' in declaration of `P'
    lagrange.c:32: `y' undeclared here (not in a function)
    lagrange.c:32: `i' undeclared here (not in a function)
    lagrange.c:32: `L' undeclared here (not in a function)
    lagrange.c:32: initializer element is not constant
    lagrange.c:32: warning: data definition has no type or storage class
    lagrange.c:33: parse error before `}'

    C:TEST>
    And it's not right for C++ since the headers are wrong and you're not slavishly using iostream, though it compiles better in C++:
    C:TEST>g++ -Wall lagrange.cpp
    lagrange.cpp:7: return type for `main' changed to `int'
    lagrange.cpp: In function `int main(...)':
    lagrange.cpp:14: warning: use of `l' length character with `g' type character
    lagrange.cpp:14: warning: use of `l' length character with `g' type character
    lagrange.cpp:14: warning: too few arguments for format
    lagrange.cpp:9: warning: unused variable `double P'

    C:TEST>
    When you attempted to compile it, didn't you read the warnings? Oh, don't tell us that you ignored the warnings! Only a drooling idiot would be so stupid as to ignore the most important messages output by the compiler, the warnings.

    Assuming that you did ignore the warnings, here's what you missed:
    Code:
    	printf("%lg, %lg, %lg\n", Lagrange (x, y, N, xx));
    That is line 14 that the warnings are talking about. Now let's see, what are those warnings again?
    lagrange.cpp:14: warning: use of `l' length character with `g' type character
    lagrange.cpp:14: warning: use of `l' length character with `g' type character
    lagrange.cpp:14: warning: too few arguments for format
    Well, the first two are telling you that you don't know how to format the output of doubles. And the third one is telling you that you don't even know how to use printf. "too few arguments for format". What do you imagine that could possibly mean? How many arguments are you telling printf to expect? Three. How many arguments do you actually give it? One. I don't know how far you've gotten in math yet, but hopefully you should have learned by now that one is not equal to three. Do you know what printf does when you don't give it enough arguments? It goes looking for them nonetheless. Those arguments are values that are lined up on the stack one after another. So printf just keeps on reading where those missing arguments should be, which means that it ends up reading and trying to display garbage!

    Oh, was that the symptom that you had decided to keep secret from us? That your print was displaying garbage?

    And in the future, do not ignore warnings! If nothing else they will spare you public embarrassment.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    Originally Posted by ddeakpeti
    yes, but it can not debug because of the xx value, which is not initialized.
    Well then initialize it! In main(). At present, because you do not initialize it and it's a local variable, it contains garbage!

    PS

    Wait a minute! You tried to run that program? The compiler was throwing warnings at you and you still tried to run the program? That goes way beyond sheer idiocy! Never run a program that fails to compile cleanly! If the compiler is giving you warnings, that means that you're doing something wrong, even to the point where even the compiler can't figure out what you're asking it to do, in which case the compiler had to start making guesses. When a compiler has to start guessing what you want it to do, then you have no idea what that executable is going to do.

    Never ignore warnings! Never run a program that gave you warnings!
    Last edited by dwise1_aol; March 14th, 2013 at 06:39 PM.
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,072
    Rep Power
    1802
    Originally Posted by dwise1_aol
    That doesn't even come close to compiling in ANSI C, which is what programmers mean when they say "C"
    ... or since 1990 ISO C :rolleyes:
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0
    The program tried to debug. I do it with Ctrl+F5 usually, and a block appears, saying that the "The variable 'xx' is being used without being initialized." I can either Abort, continue or Ignore. No warnings nor errors appeared before. And the declaration for 'xx' is inside the void main() that's why I don't understand...
    The problem is with the printing, ok, I see it now. Thank you! :)
    By the way, I'm working in C, using MS Visual Studio 2008.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    Originally Posted by ddeakpeti
    No warnings nor errors appeared before. ... By the way, I'm working in C, using MS Visual Studio 2008.
    Bull****! First you try to make us play stupid guessing games and now you're just flat-out lying to us!

    I just now set up your program in MS Visual Studio 2008 as a C program (eg, as main.c). It refuses to even compile, reporting 28 errors and 2 warnings! When I try to use your bad-practice method of just telling it to debug, it tries to build, reports the errors and warnings, and then offers to run the previous build of the program instead.

    Of course, things go a bit differently when it's a C++ program instead (ie, as main.cpp). In that case, we only get warnings in which case the debugger would go ahead and try to run the screwed-up program. So then you lied to use about what language you were using (you claim C, whereas the only possibility is C++ -- before some C99 wienie pipes in, Visual Studio does not support C99).

    And you lied to us about there not being any warnings! When I built your program as C++, I saw those warnings displayed in the output window. After the debugger started and I closed it, the output from the build was gone having been replaced with the debugger's output. Which is one reason why you need to build first and then only after it has built cleanly should you try to debug it. If you get warnings, the program has not built cleanly; first clear all errors and warnings by correcting your code. The idiotic practice you describe is exactly that, idiotic. It is useless to even think of trying to debug a program that has not built cleanly.

    However, I am disappointed with Visual Studio's warnings. To begin with, it defaults to a lower warning level. Under Project, open the project's properties. Expand Configuration Properties, then expand C/C++. Click on General and the fifth line in the right pane will be Warning Level. It defaults to /W3, so you would have to explicitly raise it to the highest level, which is a mere /W4.

    Then when I compiled your program, I got two warnings (copied directly from the output window, but with the long, long pathname shortened):
    1>c: ... \lagrange\main.cpp(9) : warning C4189: 'P' : local variable is initialized but not referenced
    1>c: ... \lagrange\main.cpp(14) : warning C4700: uninitialized local variable 'xx' used
    When I compiled it with level /W3 warnings, all I got was the "uninitialized local variable 'xx' used". It didn't even begin to notice the massive screw-up with the printf call, which my MinGW gcc did catch both in C and in C++ (see my previous message). Even when I correct the errors in the C program (including your pure-C++ for-statements), the compiler does not notice the problem with printf. I see that as a failure of the compiler.

    However, the irrefutable fact does still remain that the build did indeed produce warnings and that one of those warnings was the use of an uninitialized variable, xx. And that you should have known better than to attempt to debug that program.

    BTW, have you figured out yet that you need to initialize xx?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0
    First I'm sorry about not being as smart as some of the people here about programming. To be honest I can't say I can do C programming...
    As far as I know what we do during our Computer Science classes is C-programming, in a C++ work filed, using MS Visual Studio... etc.
    I typed the codes and I pressed Ctrl+F5 without anything else, nor error, no linking, nothin in the bottom as usually when it brings errors or warnings. Then an Error box appeared with a red X, saying what's wrong and I can choose to either Abort, Continue or Ignore.
    So I tried to find the error, but I couldn't and then I wrote here expecting some help, and not some aswers telling me that I'm a liar and making comment on every words I wrote.
    Terribly sorry for not writing here first my whole way of programming and really I meant for playing with all of you who could possibly help. That was all my intention and of course to lie for the second time, as If I was pretending I don't have warnings.

    I don't think I deserve this kind of talking to me and really sorry If I didn't know about something! And I'm not saying you are wrong, no not at all, you know something which I didn't even realize, that's ok, I'm glad you shared with me and I'm open for every advices, but the way you proposed is a bit too much!

    Sorry If this text is not about the programs since this website is for that, so I would be glad if I could be back to the topic!

    I looked through the printf but still I don't know what is the two missing variables I should include. Also, in the void main() it says double xx; which is the initialization for xx, isn't it? Or I guess I need to add some values for it...

    Thank you in advance.
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    Example of uninitialized variable. You, starter of this thread, have done this:
    Code:
    void f() {
      double xx;
      /* what is the value of xx ? */
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    You started this thread by trying to get us to play a stupid guessing game. You got precisely the reaction that you deserved for pulling that kind of crap.

    You claim that your program is in C program when it's clearly C++. Honesty and truthfulness is a basic requirement any time you're asking others for help, since you would otherwise be misleading them into wasting their time and effort in which case we will surely display our appreciation for you jerking us around. Now, if your teacher has been teaching you to write C++ code and telling you that it is C, then it is your teacher who is misleading you, in which case you need to find yourself a new teacher.

    If you want to actually write a C program in Visual Studio 2008, then name the source file with a .c extension; the default .cpp extension is for C++.

    Likewise, when you claim that your compiler did not display any warnings during the build when my recreation of those events with the exact same compiler, Visual Studio 2008, did indeed display warnings, you are not telling the truth. Unless you just didn't both to even look, in which case you are committing the idiocy of ignoring warnings. Or you could have gone into the project properties and turned warnings off, in which case you committed a far worse idiocy. And if you had done that, who taught you to? The same teacher who's misleading you about what language you're using?

    Who taught you to blindly run a program that does not build cleanly? Who taught you to just run the debugger in order to build your project and then try to run it regardless of how frakked up it is? Teaching you to do that would be like teaching you to check that a gun is not loaded by looking down the barrel; most of the time you might not end up shooting your face off, but you can't always get away with it. Somebody teaching you to do that is doing you a grave disservice.

    Do you now at least understand that you should not do that? That you need to build first and then, if and only if it builds cleanly without any warnings, run it. And do you now understand that you must never ignore warnings?

    Originally Posted by ddeakpeti
    Also, in the void main() it says double xx; which is the initialization for xx, isn't it? Or I guess I need to add some values for it...
    What do you think? Did you go back and read your textbook to find the answer? Look at that part of your code:
    Code:
    	const int N=3;    // this const int has been initialized
    	double P=0.0;    // this double variable has been declared and initialized
    	double x[N];    // this cannot be done in C, but it can in C++
    	double y[N];    // this cannot be done in C, but it can in C++
    	double xx;    // this double variable has been declared, but not initialized
    When you declare a variable (eg, double xx;), then you tell the compiler that a variable exists of this datatype and with this name, but you say absolutely nothing about what value it contains; it is uninitialized. All the compiler does is to reserve a memory location of a size appropriate for that datatype and associate that name with that location, but it does not initialize it unless it's a global variable, but not if it's a local variable, such as xx is. To initialize xx, you need to either use an initializer like you did in the declaration of P or else assign a value to it (eg, in an assignment statement, in a scanf call).

    Remember, if you do not initialize a local variable, then it contains garbage. Never use an uninitialized variable. Even more important, never use an uninitialized pointer!

    Originally Posted by ddeakpeti
    I looked through the printf but still I don't know what is the two missing variables I should include.
    Just when I thought that it couldn't be any worse, you have to come up with that.

    How the hell should I know? It's your program, not mine! If you wanted to display three values, then you are the only person on this planet who has any idea what those three values should be. And if you only want to display one value, then change the format string so that it only expects one value!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0
    All right, everything is clear now. Thanks a lot ;)

IMN logo majestic logo threadwatch logo seochat tools logo