Thread: Quick Explanation

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    Quick Explanation

    I cant figure how to get the correct output from this code. Can someone explain. Thanks.

    Code:
    #include <stdio.h>
    
    int main() {
        
    int a[] = {8,1,6,3,5,7,2,4,10,9};
    
    int i;
    
    for (i=0;i<10;i++)
        a[i] = a[a[i]-1];
    for (i=0; i<10; i++)
        printf("%d ", a[i]);
        
       getchar();
        
        return 0;
         
               }
    The answer is 4 4 7 7 5 2 4 7 9 9

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should explain why the correct output is the correct output, i.e., what you are trying to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    a[ 0 ] = a[ a[ 0 ] - 1 ]
    a[ 0 ] = a[ 7 ]
    a[ 0 ] = 4

    That's what the first pass evaluates through. Using what I've shown above, walk through the rest on paper and you'll see your answer.


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

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Value of array elements is changing on the fly; so write out all intermediate results on paper before evaluating the final expression.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Im just trying to figure out why that prints out the answer. how it works basically. why it prints out that answer. How that array is changed

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    ok thanks i got it

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    How does this program work? I cant get the right output from the code

    Code:
    #include <stdio.h>
    
    int f(int a, int b, int c);
    
    int main() {
        int a = 3, b = 5, c = 2;
        b = f(b, c, a);
        printf("a = %d, b = %d, c = %d\n", a, b, c);
        a = f(c, a, b);
        printf("a = %d, b = %d, c = %d\n", a, b, c);    
        return 0;
    }
    
    int f(int a, int b, int c) {
        int t = b+c;
        a = 2*t - 7;
        b = 3*a%13 + 1;
        c = b - a;
        printf("a = %d, b = %d, c = %d\n", a, b, c);
        return a + b + c;
    }
    The output looks like this

    a = 3, b = 10, c = 7
    a = 3, b = 20, c = 2
    a = 39, b = 1, c = -38
    a = 2, b = 20, c = 2 (1 pt each)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remember that the a, b, and c inside f don't match the a, b, and c inside main -- watch the order that the parameters are given inside the function call.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Should i figure out f first then go though main

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by harkroad View Post
    Should i figure out f first then go though main
    I don't think that makes sense. f is not run independently of main; it is only run when main calls upon it. So you'll have to go through main until yous ee a function call, then work through f (with the specific values that main called it with), then return to main.

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I would pay more attention to what laserlight and quzah stated in your first problem, that will still apply for your second. Run through your program on paper, perhaps creating a table for your variables and jot down their values at each step in the table.

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by harkroad View Post
    The output looks like this

    a = 3, b = 10, c = 7
    a = 3, b = 20, c = 2
    a = 39, b = 1, c = -38
    a = 2, b = 20, c = 2 (1 pt each)
    Is your test/homework open book, open notes, open professionals living on programming support pages?

    Just curious.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Its just a Review for a Test.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    the first line get printed bc...

    t= 2+3
    a= 2*5 - 7 = 3
    b= 3* 3%13 +1 = 10
    c= 10 - 3 = 7

    How does the Second get those values?

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Close this Thread I Figured it out thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 09:17 PM
  2. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  3. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM