Thread: Total C newbie -- PLEASE HELP with writing a LOOP, asap!!!!

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    Total C newbie -- PLEASE HELP with writing a LOOP, asap!!!!

    Hi guys, please help me out as i am a complete newbie when it comes to C programming. I have the following code (as shown below). As you can see, it is a series of if/elseif statements. I'm very much a newbie in understanding loops (I do understand them but not how I might apply them to this situation, as with loops I generally have a condition, update, etc., and other than 'times' being the update (as in number of times), I don't see how I can apply this to a loop.

    Basically, at the end of each of the FIRST TWO if statements, it ends in 'times=times+1' ... I just need to find a way to embed this whole thing in a LOOP so that at the end of each of these two statements, it goes back up to the "x = ..." line and begins from there again (with new values for 'guess', as assigned). Obviously, if it hits the 3rd if statement, no looping back will be necessary, so I imagine the 3rd if statement would somehow appear outside of the loop? Not sure.




    Code:
    x = guess*guess - n
    
    if (|x|> tol && x>0)
    { 
    tooHigh = guess;
    guess=(tooHigh + tooLow)/2;
    times= times + 1;
    }
    
    else if (|x|>tol && x<=0)
    {
    tooLow = guess;
    guess = (tooHigh + tooLow)/2
    times= times + 1;
    }
    
    else if (|x|<=tol)
    {
    print times;
    return guess;
    }
    Last edited by Laila Sawyer; 07-26-2011 at 04:32 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Too bad you only have a little while to complete this assignment...

    When you finish, be sure to read the homework policy to find out why I wasn't:

    very direct as to how to write the loop.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If only there were tutorials for you to use on the internet.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    I am not asking anyone to do my entire assignment for me, and I genuinely do not understand how a for or while loop could be applied to a situation like this. I have wracked my brain for several hours and perhaps something like this may seem obvious to you, but it is not to me. Not all of us have programming minds. Quzah, I have looked at that exact tutorial you linked before and it hasn't helped. Sometimes a nudge in the right direction is all someone needs. It would be nice if you guys would be encouraging because this is something I want to learn.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It's difficult to give you some "guiding code" without giving away the whole thing. Besides, why didn't you ask for help much earlier than right before the deadline? This seem to be a bad habit you need to overcome.

    Quote Originally Posted by Laila Sawyer View Post
    So if you think one method or the other is better, please be SPECIFIC in how to implement them!

    Also, this assignment is due at midnight tonight (Pacific time), so please respond ASAP!! thank you!!!

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    I'm not asking you for judgement on my homework habits, Matticus, and I really don't think that's your place anyhow. Everyone deals with their own personal issues; the only reason I specifically mentioned the deadline was that if someone wanted to take the time to help me later on, I didn't want them to waste too much time in explaining things.

    'Guiding code' doesn't have to be code FOR my actual homework. Much of how I learn is by looking at examples done in class that are similar in format to what I aim to do with my program, and going from there. In this situation, I have never encountered a for/while loop where I was unable to identify a condition and an initialization clearly (unless there are obvious ones in this problem that I'm not seeing, which you could then simply point out). And also, I've never encountered a loop where "x= ... (something)" before the actual if/else statements come in. So if you had some examples of other code where this was done in that exact way, floating somewhere on the internet, that'd be helpful.

    Again, if you guys could be encouraging instead of critical, I think a lot more people would want to learn C programming. When a newbie comes here and encounters a dismissive attitude, they don't exactly feel too thrilled about continuing on the subject. Just a thought.
    Last edited by Laila Sawyer; 07-25-2011 at 10:54 PM.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    u could use a do while loop like this....
    Code:
    x = guess*guess - n;
    do
    {
             
             if (x>0)
    	{ 
    		tooHigh = guess;
    		guess=(tooHigh + tooLow)/2;
    		times= times + 1;
    	}
    	else
    	{
    		tooLow = guess;
    		guess = (tooHigh + tooLow)/2
    		times= times + 1;
    	}
            x = guess*guess - n;
    }while( |x| > tol);
    print times;
    return guess;
    and as u can see this wont work properly for checking the first guess...so u can strike out using do while loop
    u can use a while loop like this
    Code:
    x = guess*guess - n;
    while( |x| > tol)
    {
    	
    	if (x>0)
    	{ 
    		tooHigh = guess;
    		guess=(tooHigh + tooLow)/2;
    		times= times + 1;
    	}
    	else
    	{
    		tooLow = guess;
    		guess = (tooHigh + tooLow)/2
    		times= times + 1;
    	}
            x = guess*guess - n;
    }
    print times;
    return guess;
    or a for loop like this....
    Code:
    for(x=guess*guess-n;|x| > tol;x=guess*guess-n)
    {
    	if (x>0)
    	{ 
    		tooHigh = guess;
    		guess=(tooHigh + tooLow)/2;
    		times= times + 1;
    	}
    	else
    	{
    		tooLow = guess;
    		guess = (tooHigh + tooLow)/2
    		times= times + 1;
    	}	
    }
    print times;
    return guess;
    but really u should go through the tutorials....thats the way u can learn how to use the functions to ur needs....
    Last edited by subhash.rao; 07-25-2011 at 11:11 PM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    'm not asking you for judgement on my homework habits, Matticus, and I really don't think that's your place anyhow.
    It's relevant to how you're perceived on this board.

    Does this help?

    Code:
      while(condition is true)
      {
          // do stuff
      }
    EDIT: Nevermind...
    Last edited by Matticus; 07-25-2011 at 10:56 PM. Reason: ...

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Thank you subhash.rao, that's exactly what I needed. I think the second loop is exactly what I was going for, and in a sense the code seems obvious, if it were not for the "else if (|x|<=tol)" part. Does a while loop addressing (|x|>tol) essentially imply the opposite (that if |x|<=tol, the two final statements (print & return) are put into action)? Even though no <= is written? I think this is what tripped me up.... there are some conclusions that other view as obviously implied but that I might not understand unless it's explicitly stated.

    And trust me, I did go through tutorials, and pages and pages of my previous homework and class materials. It's just that most of the loops we've done so far have stayed true to form to the examples given in class. Matticus, I understand your reasoning, but I don't know that 2 posts where I was seriously in trouble out of the MANY hours of homework assignments I've completed over the past couple months is enough to make judgement. If you think I'm that much of a slacker that the very fundamental structure of a while loop isn't something I've already gotten through to, we're definitely on two very different pages. For future instances like mine, I hope you don't assume the worst, and at least give some genuinely helpful points in the right direction. I agree that people shouldn't come here expecting to have their entire homework assignment done for them, but if they've made some progress and are stuck at a certain point, one clear answer can make a world of difference in progressing overall... Also, it's been my experience that many programmers or IT people in general can come off as condescending instead of helpful (much of this is portrayed in popular culture as I'm sure you know). And I don't see how that sort of attitude benefits anyone, on either side of the issue.
    Last edited by Laila Sawyer; 07-25-2011 at 11:40 PM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Not a judgment - just an observation.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    a while loop addressing (|x|>tol)
    like
    Code:
    while(|x|>tol)
    {
         //do stuff A
    }
    //do stuff B
    means it will be doing stuff A till the condition (|x|>tol) is true....and it will do stuff B only when the condition is false i.e when it encounters |x|<=tol only then it exits the loop and does stuff B

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    VERY HELPFUL, my class notes did not lay it out so simply (sometimes professor glaze over what to them seems obvious). Thank you, I am saving this page for future reference and for studying for my final exam. Once again, thanks subhash.rao. Your info has been encouraging and inspiring!

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Laila Sawyer View Post
    VERY HELPFUL, my class notes did not lay it out so simply (sometimes professor glaze over what to them seems obvious) ...
    Once again, thanks subhash.rao. Your info has been encouraging and inspiring!
    That's just stupid and complete bull****! Look at the FAQ you were linked to:
    Code:
      int x = 0;  /* Don't forget to declare variables */
      
      while ( x < 10 ) { /* While x is less than 10 */
          printf( "%d\n", x );
          x++;             /* Update x so the condition can be met eventually */
      }
    You mean to say that is more confusing than this:
    Code:
    while(|x|>tol)
    {
         //do stuff A
    }
    //do stuff B
    And that is easier to understand than this:
    Code:
      while(condition is true)
      {
          // do stuff
      }
    You either didn't read a damn thing anyone here posted, or you are intentionally slighting everyone else here.

    Either way, I'm calling bull****.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Quzah - yes I DO mean to say that "do stuff A" and "do stuff B" is less complicated and more clear/obvious than every other example you provided. Are you seriously meaning to tell me it is not? The question I asked subhash.rao (if you bothered to read it) was whether the falsity of the opposite was IMPLIED by not referring to it. subhash explicitly confirmed that for me when I asked him. I am by no means intentionally slighting ANYONE. He was more helpful and that's the damn truth.

    PS: I call ego issues.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Laila Sawyer View Post
    Quzah - yes I DO mean to say that "do stuff A" and "do stuff B" is less complicated and more clear/obvious than every other example you provided.
    You're an idiot then. If you can't figure out that this:
    Code:
      while(condition is true)
      {
          // do stuff
      }
    ...means "while the condition is true, do this stuff", then you are an idiot. It has nothing to do with an ego, and everything to do with you being too dumb to program.

    You just wanted to have everyone do everything for you without you having to read anything - which is why you didn't click either of the two links provided to you. You are lazy. Furthermore, Matticus' example was just as clear as the one, you were just mad at him for posting a link for you in the first place, so you snubbed his second post.

    You're an idiot.


    Quzah.
    Last edited by quzah; 07-26-2011 at 12:29 AM. Reason: i should have written your a idioit so you'd understand it
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Help ASAP
    By slapgun87 in forum C Programming
    Replies: 8
    Last Post: 07-15-2010, 01:28 AM
  2. Total newbie to programming C! Need help...
    By majortony in forum C Programming
    Replies: 3
    Last Post: 11-17-2005, 07:51 AM
  3. Total Newbie Here
    By Wiser in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-14-2003, 10:31 PM
  4. total newbie question
    By Unregistered in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2002, 09:38 PM
  5. Newbie C Programer Needs Help ASAP
    By Halo in forum C Programming
    Replies: 4
    Last Post: 02-21-2002, 01:52 AM