C/C++ Programming help

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
Soontir C'boath
SG-14: Fuck the Medic!
Posts: 6853
Joined: 2002-07-06 12:15am
Location: Queens, NYC I DON'T FUCKING CARE IF MANHATTEN IS CONSIDERED NYC!! I'M IN IT ASSHOLE!!!
Contact:

C/C++ Programming help

Post 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);

}
Last edited by Soontir C'boath on 2007-11-02 10:36pm, edited 1 time in total.
I have almost reached the regrettable conclusion that the Negro's great stumbling block in his stride toward freedom is not the White Citizen's Counciler or the Ku Klux Klanner, but the white moderate, who is more devoted to "order" than to justice; who constantly says: "I agree with you in the goal you seek, but I cannot agree with your methods of direct action"; who paternalistically believes he can set the timetable for another man's freedom; who lives by a mythical concept of time and who constantly advises the Negro to wait for a "more convenient season."
User avatar
Beowulf
The Patrician
Posts: 10621
Joined: 2002-07-04 01:18am
Location: 32ULV

Post by Beowulf »

set LG to NG at the top of the loop.
"preemptive killing of cops might not be such a bad idea from a personal saftey[sic] standpoint..." --Keevan Colton
"There's a word for bias you can't see: Yours." -- William Saletan
User avatar
Soontir C'boath
SG-14: Fuck the Medic!
Posts: 6853
Joined: 2002-07-06 12:15am
Location: Queens, NYC I DON'T FUCKING CARE IF MANHATTEN IS CONSIDERED NYC!! I'M IN IT ASSHOLE!!!
Contact:

Post by Soontir C'boath »

That only served to make NG an unidentified value.

Thanks for the idea.

Anyway, I have to submit it now.
I have almost reached the regrettable conclusion that the Negro's great stumbling block in his stride toward freedom is not the White Citizen's Counciler or the Ku Klux Klanner, but the white moderate, who is more devoted to "order" than to justice; who constantly says: "I agree with you in the goal you seek, but I cannot agree with your methods of direct action"; who paternalistically believes he can set the timetable for another man's freedom; who lives by a mythical concept of time and who constantly advises the Negro to wait for a "more convenient season."
User avatar
Darth Paul
Sith Apprentice
Posts: 52
Joined: 2002-08-22 11:44pm
Location: Canada

Post 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.
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post 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.
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
User avatar
Braedley
Jedi Council Member
Posts: 1716
Joined: 2005-03-22 03:28pm
Location: Ida Galaxy
Contact:

Post 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).
Image
My brother and sister-in-law: "Do you know where milk comes from?"
My niece: "Yeah, from the fridge!"
User avatar
Braedley
Jedi Council Member
Posts: 1716
Joined: 2005-03-22 03:28pm
Location: Ida Galaxy
Contact:

Post 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.
Image
My brother and sister-in-law: "Do you know where milk comes from?"
My niece: "Yeah, from the fridge!"
User avatar
InnocentBystander
The Russian Circus
Posts: 3466
Joined: 2004-04-10 06:05am
Location: Just across the mighty Hudson

Post 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?
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post 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.
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
User avatar
Darth Wong
Sith Lord
Sith Lord
Posts: 70028
Joined: 2002-07-03 12:25am
Location: Toronto, Canada
Contact:

Post 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.
Image
"It's not evil for God to do it. Or for someone to do it at God's command."- Jonathan Boyd on baby-killing

"you guys are fascinated with the use of those "rules of logic" to the extent that you don't really want to discussus anything."- GC

"I do not believe Russian Roulette is a stupid act" - Embracer of Darkness

"Viagra commercials appear to save lives" - tharkûn on US health care.

http://www.stardestroyer.net/Mike/RantMode/Blurbs.html
User avatar
InnocentBystander
The Russian Circus
Posts: 3466
Joined: 2004-04-10 06:05am
Location: Just across the mighty Hudson

Post 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).
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post 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.
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
User avatar
Beowulf
The Patrician
Posts: 10621
Joined: 2002-07-04 01:18am
Location: 32ULV

Post 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.
"preemptive killing of cops might not be such a bad idea from a personal saftey[sic] standpoint..." --Keevan Colton
"There's a word for bias you can't see: Yours." -- William Saletan
User avatar
Braedley
Jedi Council Member
Posts: 1716
Joined: 2005-03-22 03:28pm
Location: Ida Galaxy
Contact:

Post 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.
Image
My brother and sister-in-law: "Do you know where milk comes from?"
My niece: "Yeah, from the fridge!"
User avatar
Soontir C'boath
SG-14: Fuck the Medic!
Posts: 6853
Joined: 2002-07-06 12:15am
Location: Queens, NYC I DON'T FUCKING CARE IF MANHATTEN IS CONSIDERED NYC!! I'M IN IT ASSHOLE!!!
Contact:

Post 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!
I have almost reached the regrettable conclusion that the Negro's great stumbling block in his stride toward freedom is not the White Citizen's Counciler or the Ku Klux Klanner, but the white moderate, who is more devoted to "order" than to justice; who constantly says: "I agree with you in the goal you seek, but I cannot agree with your methods of direct action"; who paternalistically believes he can set the timetable for another man's freedom; who lives by a mythical concept of time and who constantly advises the Negro to wait for a "more convenient season."
User avatar
Darth Paul
Sith Apprentice
Posts: 52
Joined: 2002-08-22 11:44pm
Location: Canada

Post 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.
User avatar
Braedley
Jedi Council Member
Posts: 1716
Joined: 2005-03-22 03:28pm
Location: Ida Galaxy
Contact:

Post 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. 
Image
My brother and sister-in-law: "Do you know where milk comes from?"
My niece: "Yeah, from the fridge!"
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post 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.
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
User avatar
Beowulf
The Patrician
Posts: 10621
Joined: 2002-07-04 01:18am
Location: 32ULV

Post 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.
"preemptive killing of cops might not be such a bad idea from a personal saftey[sic] standpoint..." --Keevan Colton
"There's a word for bias you can't see: Yours." -- William Saletan
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post 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.
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
Post Reply