Having trouble with simple variable assignment calculations. Beginner Question.

This is a discussion on Having trouble with simple variable assignment calculations. Beginner Question. within the C Programming forums, part of the General Programming Boards category; Hello, this is a function I dont understand. This is what I am having trouble with. suppose that a=4 and ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Having trouble with simple variable assignment calculations. Beginner Question.

    Hello, this is a function I dont understand.

    This is what I am having trouble with.
    suppose that a=4 and b=7

    Code:
    int f(int a, int b) {
      int c;
      c = 2*a%b;
      a = c + (2*b%a);
      b = c - (2*b%a);
      printf("a = %d, b = %d\n",a,b);
      return c;
    }
    Once you plug in the initial a and b values, the correct answer is a=3 b=0. I dont get how it is achieved.

    This is my logic and why I dont understand it:

    c=2*a%b = 2*4%7 =1
    a=c+(2*b%a) =1+(2*7%4) =3
    b=c - (2*b%a)=1- (2*7%4)= -1

    So I got a=3 and b=-1 which is wrong. Can someone please explain the logic behind the correct answer?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    No. a = 3, b = -1 is the correct answer just like you worked out. I ran the program.

    It may not work for other values though depending on how the formulas are supposed to be interpreted. I wonder if the assigned values a and b should be separate from the passed values so that they are not corrupted. Assign to aa and bb, and print those out instead. I would guess.
    Last edited by nonoob; 03-26-2010 at 03:37 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Shouldn't you use the new value of a? (Granted, this doesn't change the end value, since 1 - (2*7%3) is also -1.)

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Look ....
    When I run my entire program, which is :

    Code:
    #include <stdio.h>
    
    int f(int a, int b);
    
    int main() {
    
      int a=4, b=7;
    
      if (f(b,a))
        printf("a = %d, b = %d\n",a,b);
      else
        printf("Zero is the answer.\n");
    
      b = f(3*a+b, 3*b-a);
    
      printf("a = %d, b = %d\n",a,b);
      system("PAUSE");
      return 0;
    }
    
    int f(int a, int b) {
      int c;
      c = 2*a%b;
      a = c + (2*b%a);
      b = c - (2*b%a);
      printf("a = %d, b = %d\n",a,b);
      return c;
    }
    The output is different, it is not a=3 and b=-1 like I would expect, can someone explain why?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Follow the program to understand why printf prints what it does.

    When you get to here
    b = f(3*a+b, 3*b-a);

    we can work out what arguments f has and what b is as a result of the call to f.

    3*4+7 = 19
    3*7-4 = 17

    so b = f(19 , 17);

    Then in f, this is calculated

    c = 2*a%b
    c = 2 * 19 % 17
    c = 38 % 17
    c = 4

    c is assigned to b...
    Last edited by whiteflags; 03-26-2010 at 04:44 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There's a difference between (4, 7) and (7, 4).

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thank for the replies everyone i finally get it!

    can someone please help me with segment of code too?

    Code:
    #include <stdio.h>
    
    int f(int a, int b);
    
    int main() {
    
    int a[5];
    int i;
    for (i=0; i<5; i++)
      a[i] = (2*i+3)%5;
    int t = a[0];
    a[0] = a[1] + a[2];
    a[3] = a[3] + a[4];
    a[4] = t;
    printf("a[0] = %d\n", a[0]);
    printf("a[1] = %d\n", a[1]);
    printf("a[3] = %d\n", a[3]);
    printf("a[4] = %d\n", a[4]);
    
    system("PAUSE");
      return 0;
    }
    I understand how they got a[0]=2, a[1]= 0, a[3]=5 for the output but I dont understand how they got a[4]=3 ..........I keep getting 2 for my a[4] value.

    Thanks again

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    t is assigned the value of a[0] at the time the assignment happens -- later changes to a[0] do not affect t.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Code:
    #include <stdio.h>
    
    int f(int a, int b);
    
    int main() {
    
      int a=4, b=7;
    
      if (f(b,a))
        printf("a = %d, b = %d\n",a,b);
      else
        printf("Zero is the answer.\n");
    
      b = f(3*a+b, 3*b-a);
    
      printf("a = %d, b = %d\n",a,b);
      system("PAUSE");
      return 0;
    }
    
    int f(int a, int b) {
      int c;
      c = 2*a%b;
      a = c + (2*b%a);
      b = c - (2*b%a);
      printf("a = %d, b = %d\n",a,b);
      return c;
    }
    thanks for the reply labstop. I have another question reguarding the code above.

    1)How do you know how many times it is going to print?? When I run the program, the final values in main are a=4, b=4, why wouldn't I continue to plug those values in for a and b in the function?

    2)I understand why all the numbers print out besides why a=4 and b=4 for the final values. Can someone please explain? Also, at the end of the function it says "return c" in order for b=4 as the final value, wouldnt the function need to say "return b"?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Execution starts at the beginning of the program and goes to the end. That's how you know how often things happen, and in what order.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Quote Originally Posted by matthayzon89 View Post
    2)I understand why all the numbers print out besides why a=4 and b=4 for the final values. Can someone please explain? Also, at the end of the function it says "return c" in order for b=4 as the final value, wouldnt the function need to say "return b"?
    I explained how you got those numbers, so I'm not going over that again. The point of the return is that you want to assign f's c variable to main's b. If you wrote "return b" you would assign f's b variable to main's b variable. By returning, you take a variable from inside one function and store its value in a variable from the calling function. This is the simplest way for functions to share the results of computations without breaking scope rules. Scope rules explain how main's b is separate from f's b, and where each of them are "in scope" and are going to be used. According to C, it doesn't make sense to use just one b everywhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable value not assignment to function argument
    By Almsoo7 in forum C Programming
    Replies: 2
    Last Post: 02-28-2010, 11:06 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  5. A simple c++ question..Im a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2001, 03:25 PM

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