I'm struggling Pascal class

Discussion in 'Mac Programming' started by larswik, Feb 11, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Hey all, So as the subject stated it's starting to kick my but but not in the ways I thought it would. He is all over the place teaching and hard to follow. Monday we covered IF, IF THEN, NESTED IF's Which I understand just fine. What I am struggling with are the things I did not learn is the C book like "Create a Top-Down Design" and "Divided and Conquer Problems". When I come up with a problem I can solve it but when he gives us a problem by brain freezes.

    I uploaded my first Take home quiz. I am not expecting the answers but a little help in understanding a few things. The first one is '3 MOD 5'. I assume it is asking me what the remainder is but 5 won't go into 3 and it would leave me with a decimal and he wants integers? If it were 5 MOD 3 (5 % 3 for C) then the answer would be 2.

    Then the last one in that line up (g) 3 + 2 - 4 * 5 DIV 2. When I plug this into my computer it comes back with the answer of -5. My brain is saying 2 goes into 5, 2 times. Then 2 times 4 gives me 8, then 8 minus 2 is 6, the 6 plus 3 leaves me 9, not -5. The multiply and DIV and a higher precedence then the Plus and Minus do.

    What am I missing here?
     

    Attached Files:

  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Mod is the integer remainder of division. How many times does 5 go into 3? What is left over after that division?

    The second piece is just order-of-operations. Multiplication and division go before addition and multiplication, and from there you work left to right for things that have the same precedence.

    -Lee
     
  3. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    The second exercise would have been helpful for cybrscot's thread earlier.

    IMHO the "Create a Top Down Design" and "Divide and Conquer" are some of the most important skills you can develop! Are they taught in the book or only through the prof. in class?

    B
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    5 goes in to 3 '0.6' times if I did the Math right but that seems wrong

    The other question I found this link http://www.hkbu.edu.hk/~bba_ism/ISM2110/pas039.htm

    So the precedence with operators leaves me a little baffled still. The problem 3 + 2 - 4 * 5 DIV 2 if it reads left to right that means I would start with the 4 TIMES 5 which is 20, then DIV that by 2 leaving me 10. Then go back to the 3 PLUS 2 which is 5, the subtract 5 from 10 leaving me 5.

    Is that right?

    -Lars
     
  5. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    They are taught in the book and there is a large section devoted to it. I am having issues with the way the teacher is teaching the class too. Many of us are having to stay after class to ask many questions. The book is Turbo Pascal but he tells us to ignore the turbo part, so I spent a good deal of time reading a part on strings but was then told we would not deal with that.

    The big problem is that "Create a Top-Down Design" and "Divided and Conquer Problems" are abstract concepts that I am trying to break down in to more manageable routines. So example, "Go to the store and buy milk". Trying to find out how far that needs to be broken down. from "Getting in the car" or do I need to break that down any further to "pull car keys out of pocket", then "Get in the car".

    Those are the struggling points.

    -Lars
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    Does this help?

    HINT: Subtraction is not commutative.

    B
     
  7. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    Ahhh, yes that I can understand. Thanks. The last question I have there is that the math then reads left to right or right to left. As a final result am I subtracting 5 from 10 which leaves 5 or am I working right to left which would be 10 minus 5 leaving me with -5.

    So...

    Total := 5 - 10;

    I would say it would be -5 but here it says all operators are evaluated left to right http://www.hkbu.edu.hk/~bba_ism/ISM2110/pas039.htm In this case it would be removing 5 from 10 leaving 5 as the results. But I know you are removing 10 from 5 which leaves you -5, but that operates right to left, and that contradicts what it says.

    -Lars
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Well... You worked out 5 % 3. You know the remainder when you are doing integer or whole-number division is 2. So without the decimal, 5 goes into 3 how many times? What's the remainder? If you were doing long division on paper, what would it look like?

    You've got it right all the way to here. So you had 5 and 10, in that order... so left to right...
    Oooh, close but no cigar. Those numbers are right, subtraction is the operation... something is just awry with the order.

    -Lee

    Edit: Just saw your last post. You're overthinking and confusing yourself now.
    5 - 10
    You got that far. Left to right. Five minus Ten. What's your gut? Some things in programming are counterintuitive. This is not one of them.
     
  9. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #9
    Lee, you answered part of my question above. Minus is right subtraction.

    Thanks!

    -Lars
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    In mathematical notation, and in all programming languages I know, the order is left to right when the precedence is equal, with the only exception being exponentiation in Fortran.

    So the correct order is:

    3 + 2 - 4 * 5 div 2 =
    3 + 2 - 20 div 2 =
    3 + 2 - 10 =
    5 - 10 =
    -5.
     
  11. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #11
    Click, and the light went on, I understand. I was confusing myself over a simple problem
     
  12. balamw, Feb 12, 2011
    Last edited: Feb 12, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #12
    I predicted as much. What compiler/environment are the other students using?

    In a sense that's where the two concepts come together. It's hierarchical. At the top level it's

    Code:
    Go to store.
    Buy milk.
    Your goal is to write code. So you can break down each of those operations into finer and finer operations until each operation's code is clear. (At first you will probably go too far and will have things that are ultimately not needed in the code, and later you will be able to stop earlier because a block is familiar, examples to follow.).

    Along the way you can look for things that were missing from the plan, find common steps to reuse and generalize.

    e.g. I presume you really meant

    Code:
    Go to store.
    Buy milk.
    Return home with milk.
    The return home with milk part was implicit, but since many of the same operations required to get you to the store will also be required to get you back home you should think of it as a separate step. Then, plan them out accordingly.

    Reuse:
    Code:
    Go to store.
    Buy OJ.
    Return home with OJ.
    The Go to store bit is common, thus in your top down design you would not have to refine that any more. You already know how to do that.

    Generalize:

    Code:
    Define shopping list.
    Go to store.
    Buy items on shopping list.
    Return home with items on shopping list.
    The main difference between the earlier versions and the new one is that I need to have a data structure that contains the items I need to get at the store and take that with me (pass it) to the store. That should help design the function interfaces.

    This way you end up with a framework that will let you go to the store and get OJ too.

    B
     
  13. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #13
    Q. What compiler.
    A. I don't know. They have a computer lab running Linix. I'm doing it with Xcode at home and after class on their machines.

    Q. Top down design.
    A. Thanks for that information, that helps. When I think of a problem I can fi d a way of coding that. But for some reason my brain locks up when he presents the problem. He the asked this question in class " You have A,B,C of type int. Using IF statements find the one that has the highest value and adding it to a variable named BIG." when I saw the answer it was easy but my brain froze.

    Thanks again for the detailed examples for the top down designs.
     
  14. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
    Linux, with a u.

    When it comes to problem solving, you're just going to have to practice. Work on solving things methodically on paper before you worry about coding. Play some professor Layton without cheating. Revisit high school-level word problems. Build your confidence on your own time so you feel better on the spot in class.

    -Lee
     

Share This Page