Page 1 of 1

C/C++ Programming help

Posted: 2007-11-02 09:46pm
by Soontir C'boath
Edit: I don't need the help anymore since I have just submitted it incorrectly but if you're willing to still punt out ideas, that'd be great.

Ok, according to one of the TAs, this should be a simple program to type but I'm having trouble finishing it and getting it right.

The problem is to find the approximated square root of a number N by repeated calculations using the formula NG=0.5*(LG + N/LG) where NG=Next Guess and LG=Last Guess

LG starts at 1.0

After inputting a number N into the formula above, I have to get it to check if the difference between NG and LG is 0.005 in which NG is accepted as a square root but if not, the next guess becomes the last guess which I assume means LG=NG. This is when I repeat the loop.

The only problem I have is in what to put in the while brackets. Other than that, it'll give me the correct answer but of course it loops forever.

I have a feeling that I might have to change the coding around but I have no idea what.

Ideas?

Code: Select all

#include "stdio.h"
#include "math.h"

void main()
{float N, NG, LG, x;
LG=1;
printf("Type in a number\n");
scanf("%f", &N);

/*I have no idea of what to put in while() which won't cause the identifier to become without a value*/

	while(){
	NG=0.5*(LG+N/LG);
     if (abs(NG-LG)<= 0.005)
	printf("%f is a squareroot of N\n", NG); 
     else LG=NG;}

x=sqrt(N);
printf("%f is the intrinsic absolute value of the number\n", x);

}

Posted: 2007-11-02 10:17pm
by Beowulf
set LG to NG at the top of the loop.

Posted: 2007-11-02 10:35pm
by Soontir C'boath
That only served to make NG an unidentified value.

Thanks for the idea.

Anyway, I have to submit it now.

Posted: 2007-11-02 11:15pm
by Darth Paul
The simplest change to your code would be:

Code: Select all

while( true )
{
    NG=0.5*(LG+N/LG);
    if (abs(NG-LG)<= 0.005)
    {
        printf("%f is a squareroot of N\n", NG);
        break;
    }
    else LG=NG;
}
you can also evaluate the if condition in your while loop if you compute NG before the while loop as well.

Posted: 2007-11-02 11:16pm
by Durandal
No offense, but you submitted that? It runs an infinite loop, main() isn't of type int and you've got no condition in your while() loop. That's some skanky code.

Posted: 2007-11-02 11:26pm
by Braedley

Code: Select all

//pretense

double LG, NG=1;
do{
    LG=NG;
    NG=0.5*(LG+N/LG);
}while(abs(NG-LG)>threshold)

//say it's done, etc.
This is the most elegant because you are guaranteed to enter the loop (you don't have to set up a contrived condition), you don't have to nest an if statement within the loop (not that you really had to with others), and it's also the shortest (unless you want to get rid of NG and LG and just replace them with G, in which case the while condition turns into abs(G-N/G)>threshold).

Posted: 2007-11-02 11:31pm
by Braedley
Darth Paul
Never, EVER use break unless you have to!!! When programming in C or C++, you should only use break in a switch statement and very rare occasions when you can't accomplish your goals without it. This problem isn't one of those occasions.

Posted: 2007-11-02 11:55pm
by InnocentBystander
Braedley wrote:Darth Paul
Never, EVER use break unless you have to!!! When programming in C or C++, you should only use break in a switch statement and very rare occasions when you can't accomplish your goals without it. This problem isn't one of those occasions.
I'm honestly not terribly familiar with C compilers on an implementation level, whats wrong with using break?

Posted: 2007-11-03 12:35am
by Durandal
InnocentBystander wrote:
Braedley wrote:Darth Paul
Never, EVER use break unless you have to!!! When programming in C or C++, you should only use break in a switch statement and very rare occasions when you can't accomplish your goals without it. This problem isn't one of those occasions.
I'm honestly not terribly familiar with C compilers on an implementation level, whats wrong with using break?
It's just bad practice because it invalidates your loop condition. The idea is that the loop should have exactly one exit condition; a break statement gives it an additional one, making it more difficult to discern the loop's behavior at a glance.

Posted: 2007-11-03 12:55am
by Darth Wong
Durandal wrote:
InnocentBystander wrote:
Braedley wrote:Darth Paul
Never, EVER use break unless you have to!!! When programming in C or C++, you should only use break in a switch statement and very rare occasions when you can't accomplish your goals without it. This problem isn't one of those occasions.
I'm honestly not terribly familiar with C compilers on an implementation level, whats wrong with using break?
It's just bad practice because it invalidates your loop condition. The idea is that the loop should have exactly one exit condition; a break statement gives it an additional one, making it more difficult to discern the loop's behavior at a glance.
If it's a very simple loop, what's the difference? I could see that if it's some rather complicated code, but when you're talking about a loop with a half-dozen lines in it, it seems like it should be pretty easy to see the alternate exit condition.

Posted: 2007-11-03 12:57am
by InnocentBystander
I don't write software professionally yet, but its been my experience that sometimes you really do want to break out of your loop - its usually when something unexpected happens, but I don't think its some great sin as Braedley believes (or at least, thats how I interprete the large bold letters).

Posted: 2007-11-03 01:13am
by Durandal
Darth Wong wrote:If it's a very simple loop, what's the difference? I could see that if it's some rather complicated code, but when you're talking about a loop with a half-dozen lines in it, it seems like it should be pretty easy to see the alternate exit condition.
As I said, it's just bad practice. You fudge a little here and there on the small stuff, then it just gets easier to fudge it in a bigger loop, which leads to unreadable code that's difficult to predict.

Granted, I don't think it's a really huge sin (I see it in C code written by the old hands all the time), but it's something to avoid, if you can. Especially in small loops. In this case, he could've easily just specified the persistence condition in the while() statement.

Basically, break is there for edge cases, not the common case.

Posted: 2007-11-03 01:17am
by Beowulf
Darth Wong wrote:
Durandal wrote:
InnocentBystander wrote: I'm honestly not terribly familiar with C compilers on an implementation level, whats wrong with using break?
It's just bad practice because it invalidates your loop condition. The idea is that the loop should have exactly one exit condition; a break statement gives it an additional one, making it more difficult to discern the loop's behavior at a glance.
If it's a very simple loop, what's the difference? I could see that if it's some rather complicated code, but when you're talking about a loop with a half-dozen lines in it, it seems like it should be pretty easy to see the alternate exit condition.
If someone isn't expecting it to exit out of the loop oddly, they may miss it (this is one of the reasons for the advent of syntax highlighting). Simply put, unusual causes bugs.

Posted: 2007-11-03 01:34am
by Braedley
It's a simple case of if you don't have to use break, then don't. This program is so short that spending an extra couple of lines to exit a loop properly doesn't really matter. In this case, you actually save a couple of lines by not using it. If it would require the use of several hundred lines in a loop that already has a couple thousands lines to do it without break, then use the break, but that doesn't happen all that often, if ever. Break isn't as bad as goto though, which with one occurrence, can set a maintenance programmer back a month or two.

Posted: 2007-11-03 08:17am
by Soontir C'boath
Durandal wrote:No offense, but you submitted that? It runs an infinite loop, main() isn't of type int and you've got no condition in your while() loop. That's some skanky code.
I know. That's why I asked for help but time ran out. :)

Edit: I just set in Braedley's code and it works!

Posted: 2007-11-03 01:28pm
by Darth Paul
Braedley wrote:Darth Paul
Never, EVER use break unless you have to!!! When programming in C or C++, you should only use break in a switch statement and very rare occasions when you can't accomplish your goals without it. This problem isn't one of those occasions.
That was a bit over the top, don't you think? Yes, your snippit is better. I was just giving him the simplest change to his own loop (which I actually mentioned). I wouldn't be too impressed with a maintenance programmer who lost 2 months on one line of control flow code, but whatever (I guess he's from India, :twisted: ). You will see break used like this in a lot of "production" code. Strangely enough, although taught early in most texts, I have found that do loops are quite uncommon in practice, maybe because of the exit condition being so far from the entry condition.

Posted: 2007-11-03 05:43pm
by Braedley
Darth Paul wrote:That was a bit over the top, don't you think? Yes, your snippit is better. I was just giving him the simplest change to his own loop (which I actually mentioned). I wouldn't be too impressed with a maintenance programmer who lost 2 months on one line of control flow code, but whatever (I guess he's from India, :twisted: ). You will see break used like this in a lot of "production" code. Strangely enough, although taught early in most texts, I have found that do loops are quite uncommon in practice, maybe because of the exit condition being so far from the entry condition.
No, actually I don't think it's over the top, because every new programmer today is taught the exact same thing. You start using it as a crutch instead of writing the code the proper way.

The thing about goto is if it points to a line number and not a label, one extra line of code breaks the entire program. There are also other nasty side effects if used improperly. goto isn't even mentioned in most new textbooks because of the nasty things it can do to a program. The thing about the maintenance programmer being set back a month because of a goto is that instead of just changing the line number that it points too, he actually has to fix it so future maintenance programmers don't fall in the same hole. Using a label is only marginally better, but it is still a nasty solution when using proper conditions in loops makes life so much easier.

As for do-while versus while, it doesn't make that much difference here because the loop is so short. If it was a long loop, I may consider using just a while, but in that case, I also don't want to duplicate half my code outside the loop to ensure that it runs at least once when I can have that guarantee using do-while. In any case, I can easily rewrite my program to use while instead:

Code: Select all

//pretense

double LG=N, NG=1;
while(abs(LG-NG)>threshold){
    LG=NG;
    NG=0.5*(LG+N/LG);
}
//say it's done, etc. 

Posted: 2007-11-04 07:56pm
by Durandal
Destructionator XIII wrote:
Braedley wrote:The thing about goto is if it points to a line number and not a label, one extra line of code breaks the entire program.
Does anyone still do that? goto line number isn't even legal in any modern language I am aware of.

I find goto to be a really useful tool. In things like C, it is pretty declawed anyway, so lots of the old evil is not allowed, and if you think before you use it, it can be the most straight-forward way to implement something.
Indeed, I see plenty of uses of goto. In lower-level code that is heavy on error-checking, goto lets you provide a single block of exit code to free your memory and do whatever other clean up work you need. So in that sense, it avoids code duplication.

And no, I've never seen anyone use goto with a line number in any C written in the last 15 years.

Posted: 2007-11-04 08:15pm
by Beowulf
Line numbered gotos aren't legal C. You need to at least have a label. It'd be useful if C like languages had labeled breaks, thus removing yet another use for the unstructured goto, but only Perl has that, AFAIK.

Posted: 2007-11-04 09:06pm
by Durandal
Beowulf wrote:Line numbered gotos aren't legal C. You need to at least have a label. It'd be useful if C like languages had labeled breaks, thus removing yet another use for the unstructured goto, but only Perl has that, AFAIK.
What's the difference, honestly? A goto pointing outside a loop will still break outside the loop.