Exam question help: Take 2

This is a discussion on Exam question help: Take 2 within the C Programming forums, part of the General Programming Boards category; Ok so I'v been told already how to do this question in another thread which got troll bombed. All I'm ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    16

    Post Exam question help: Take 2

    Ok so I'v been told already how to do this question in another thread which got troll bombed. All I'm asking for is the answer so that I can work out the cinks in my head. Thanks.

    b) If a program contains the main function
    Code:
    void main()
               {  int X=2, Y=11;
                  func(X,&Y);
                  printf("%d %d\n",X,Y);
               }
    and another function
    Code:
    void func(int P, int *Q)
                { int X;
                  X=(*Q);
                  P=X/3;
                 (*Q)=P+1;
                }
    what two values will the main function print out?

    P.s. This is not a homework question, I'm doing revision so don't think I'm trying to get out of learning.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > X=(*Q);
    X is 11

    > P=X/3;
    > (*Q)=P+1;
    Can you work out the rest?

    Can you see why X is 11 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Yeah, the answers I got were X=11 and Y=14/3
    I understand the X part because it's equal to the pointer of Y right?

    But I'm wondering why X doesn't equal 2 after you printf() considering it doesn't have a pointer to change it?
    Last edited by ÉireKarl; 05-02-2010 at 12:39 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > But I'm wondering why X doesn't equal 2 after you printf() considering it doesn't have a pointer to change it?
    But the X in main does equal 2 (try it)

    The X's in each function are separate variables - changing one does not change the other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Quote Originally Posted by Salem View Post
    But the X in main does equal 2 (try it)

    The X's in each function are separate variables - changing one does not change the other.
    Oh right sorry, I just thought you meant X=11 when you printf(). In the second function, are they just making it int X; to confuse me or does it have to be int X; ?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    They name it X just because they want to, and the name is available. Unless there is a pointer involved, each function encapsulates (surrounds to make or keep it unique), it's own variables.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Oh ok, and Y does equal 14/3?
    And how would I check this using devc++? Sorry I have the lowest of understanding when it comes to this subject.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is no quantity like 14/3 because these are integers -- whole units only.

    They're like pennies - nobody ever gives you 14/3 pennies.

    To check it out, run the program - don't be afraid to investigate these things, yourself. Some exploring is not only GOOD, it's the only way to really learn the subject, well.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Quote Originally Posted by Adak View Post
    There is no quantity like 14/3 because these are integers -- whole units only.

    They're like pennies - nobody ever gives you 14/3 pennies.

    To check it out, run the program - don't be afraid to investigate these things, yourself. Some exploring is not only GOOD, it's the only way to really learn the subject, well.
    Damn I forgot that, first thing they thought us too.I tried running it, don't know what's wrong with it. This is what I'm doing:
    Code:
    #include <stdio.h>
    
    void main()
               {  int X=2, Y=11;
                  func(X,&Y);
                  printf("%d %d\n",X,Y);
               }  
               
    void func(int P, int *Q)
                { int X;
                  X=(*Q);
                  P=X/3;
                 (*Q)=P+1;
                }
    Please be gentle, I'v only just properly started learning it.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ÉireKarl View Post
    Damn I forgot that, first thing they thought us too.I tried running it, don't know what's wrong with it. This is what I'm doing:

    In your code editor, set your options to use spaces, instead of tabs, and use 2 to 5 (I like 2 for almost everything. Forum software doesn't handle tabs very well.

    Code:
    #include <stdio.h>
    
    void func(int P, int *Q);  //function prototype
    
    int main()             //void main is NOT standard C. I know books have it sometimes
    {                      //but that's just being lazy and wrong on their part
       int X=2, Y=11;
       func(X,&Y);
       printf("%d %d\n",X,Y);
       return 0;
    }  
               
    void func(int P, int *Q)
    { 
       int X;
       X=(*Q);
       P=X/3;
       (*Q)=P+1;
    }
    Please be gentle, I'v only just properly started learning it.
    Oh don't worry - even if I'm cussing you out, I'm always cussing you out, GENTLY - makes all the difference, I know. < ROFL! >

    Try that - I haven't.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by ÉireKarl View Post
    Damn I forgot that, first thing they thought us too.I tried running it, don't know what's wrong with it. This is what I'm doing:
    Code:
    #include <stdio.h>
    
    void main()
               {  int X=2, Y=11;
                  func(X,&Y);
                  printf("%d %d\n",X,Y);
               }  
               
    void func(int P, int *Q)
                { int X;
                  X=(*Q);
                  P=X/3;
                 (*Q)=P+1;
                }
    Please be gentle, I'v only just properly started learning it.
    A few things:

    1) look at Salem's user picture to have a laugh over why void main is bad. You should always use int main() and return 0 at the end of your main function.

    2) The problem is that you are declaring and implementing void func() after main so main() has no idea what func() is.

    Either move the function above main, or type:

    void func(int P, int *Q);

    above main. This basically tells the main function : "hey, if you see a function called func, this is what it should look like, I have it implemented somewhere else".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    That's great, thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  3. sort problem for an exam
    By rjeff1804 in forum C Programming
    Replies: 10
    Last Post: 02-12-2003, 09:33 PM
  4. another exam question
    By rjeff1804 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 09:29 PM
  5. The AP Exam.....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 02-10-2003, 08:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21