Thread: Exam question help

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

    Post Exam question help

    Ok so I got this question in one of our exams and I havn't a clue on how to answer it. Can someone please explain what the question is asking?
    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?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The question is asking what the output would be if you ran this program. It is mostly a basic arithmetic question.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    have a read through it and work out what each line is doing to break it down into easier to understand points.
    you may find you are over-complicating it, read the question and the functions carefully.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Hint, all of this:

    Code:
                  X=(*Q);
                  P=X/3;
                 (*Q)=P+1;
    is just meant to confuse you.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Ok thanks, but I'm still not really understanding how the first function has anything to do with the second. C does not click with me.
    Is it saying Q is pointing to x from the first function?
    Does X = 5/3?
    And what does the
    Code:
    func(X,&Y);
    mean?
    Last edited by ÉireKarl; 04-28-2010 at 10:49 AM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    pointers are very important and if you don't fully understand them then now is the best time to sit down and make sure you do.
    have a look at;
    Cprogramming.com Tutorial: Pointers
    this explains about pointers and how they operate, after that see if it gives you any more insight.
    if that doesn't help then just shout for help and im sure someone will try to expand on it.
    use it to refer to your example from the question to see how it relates to it

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Quote Originally Posted by Terrak View Post
    pointers are very important and if you don't fully understand them then now is the best time to sit down and make sure you do.
    have a look at;
    Cprogramming.com Tutorial: Pointers
    this explains about pointers and how they operate, after that see if it gives you any more insight.
    if that doesn't help then just shout for help and im sure someone will try to expand on it.
    use it to refer to your example from the question to see how it relates to it
    Yeah I'm gonna give that tutorial another look over.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    So, anyone got an answer for me?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Work through it on paper. What part is confusing to you?


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

  10. #10
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Oh oh. I know. I know.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Quote Originally Posted by quzah View Post
    Work through it on paper. What part is confusing to you?


    Quzah.
    Mainly the func(....) part. I just don't understand what it does.

  12. #12
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by ÉireKarl View Post
    Mainly the func(....) part. I just don't understand what it does.
    Well gosh, that is like saying reality confuses Quzah. We don't know exactly which part gives him the problem(s).

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Quote Originally Posted by Overworked_PhD View Post
    Well gosh, that is like saying reality confuses Quzah. We don't know exactly which part gives him the problem(s).
    Fine then you want me to code it ?

    Code:
    func(X,&Y);

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You know that P and Q are just copies of X and Y, right?

    so in func(), why don't you print up the values of P and Q and X and whatever else interests you.

    See what these variables are all doing.

    You know that since *Q is the address of Y, that whatever you do to it, will be affecting Y, but that's not true with X since the func() is not getting the address for it.

    If a function is to change the value of a variable, it has to know the address of that variable.

    You see that in simple things like scanf("%d", &number). Since scanf() is going to change the value of number, you have to give it the address of number.

    But: printf("%d", number) - no address needed, since printf() is not going to change the value of the variable number.

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

    Thumbs up

    Quote Originally Posted by Adak View Post
    You know that P and Q are just copies of X and Y, right?

    so in func(), why don't you print up the values of P and Q and X and whatever else interests you.

    See what these variables are all doing.

    You know that since *Q is the address of Y, that whatever you do to it, will be affecting Y, but that's not true with X since the func() is not getting the address for it.

    If a function is to change the value of a variable, it has to know the address of that variable.

    You see that in simple things like scanf("%d", &number). Since scanf() is going to change the value of number, you have to give it the address of number.

    But: printf("%d", number) - no address needed, since printf() is not going to change the value of the variable number.
    Ahh, ok thanks for narrowing that down!

    So... does X=11 and Y=14/3 d(*.*)b?
    Last edited by ÉireKarl; 04-29-2010 at 11:25 AM.

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, 09:22 PM
  3. sort problem for an exam
    By rjeff1804 in forum C Programming
    Replies: 10
    Last Post: 02-12-2003, 10:33 PM
  4. another exam question
    By rjeff1804 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 10:29 PM
  5. The AP Exam.....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 02-10-2003, 09:46 PM

Tags for this Thread