Thread: needs a correction

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    needs a correction

    Code:
    #include<stdio.h>
    
    int level(int n);
    
    main ()
    {
    	int a, i;                                          /* i is used for the FOR-LOOP */
       for(i=0;i<15;i++)                                       /* i is counted to 15 */
    	printf("this displays simple function calls:\n");  
       printf("%d", level(i));                                 /* this line calls an integer, calling the value of i */
       scanf("%d", a);
    }
    int level(int n)                                           /* the value of i is copied to n */
    {
       	n=n+2;                                             /* the value of n is increased by 2 */
       return n;                                               /* n is returned to the function call, replacing i */
    }
    please correct me, if the comments are wrong and i understood the code the wrong way.

    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your scanf call is incorrect. You need to use the address of the variable (ie: a pointer) to use scanf. As such:

    scanf( "%d", &a );

    That's what you want. You are also slightly misunderstanding your return value:
    Code:
    int func( int n )
    {
        return n+2;
    }
    Here you're not replacing anything. You are simply returning a value. What you do with that value is up to you. If you want it to end up "replacing" the parameter you pass to it, you need to actually assign the value back to the variable you pass to it. This is how you do that:

    int i;
    i = 5;

    i = func( i );

    Thus, you pass the value in 'i' to the function, and when it is returned, it gets assigned back to 'i'.

    Quzah.
    Last edited by quzah; 10-16-2002 at 05:32 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: needs a correction

    Originally posted by threahdead
    Code:
    #include<stdio.h>
    
    int level(int n);
    
    main ()
    {
    	int a, i;                                          /* i is used for the FOR-LOOP */
       for(i=0;i<15;i++)                                       /* i is counted to 15 */
    	printf("this displays simple function calls:\n");  
       printf("%d", level(i));                                 /* this line calls an integer, calling the value of i */
       scanf("%d", a);
    }
    int level(int n)                                           /* the value of i is copied to n */
    {
       	n=n+2;                                             /* the value of n is increased by 2 */
       return n;                                               /* n is returned to the function call, replacing i */
    }
    please correct me, if the comments are wrong and i understood the code the wrong way.

    thanks
    because it has no braces of its own, the for loop only runs the highlighted statement 15 times, then it continues on and runs everything else once. i'm not sure if this is what you want.
    hello, internet!

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i needed to know if i understood the lines correctly, because its a program of my own.
    i have written down my thoughts to this program in the comments, i wanted you to correct these lines if they are not correct.

    thanks for the help i noticed something new

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    threadead, that's not how you want to comment code. You want to put in comments such as what the program/function is trying to accomplish. Comments such as "n is increased by 2" has no meaning and anyone who reads the line n=n+2 already knows that.

    Anna, you can do a search and find your old thread. Or you can search for your previous posts. You only have 8 of them so how hard would it be to find it?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Cshot
    threadead, that's not how you want to comment code. You want to put in comments such as what the program/function is trying to accomplish. Comments such as "n is increased by 2" has no meaning and anyone who reads the line n=n+2 already knows that.
    I think they were only commenting that way to tell us what they thought the code did, and were asking if what they thought it did was correct. I don't think they were commenting that way for the sake of general comments.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Want correction
    By Russian_Thia in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2006, 03:09 PM
  2. Error Detection and Correction
    By daisy_polly in forum C Programming
    Replies: 5
    Last Post: 02-22-2006, 06:13 PM
  3. Correction
    By Paninaro in forum Game Programming
    Replies: 0
    Last Post: 06-27-2002, 01:39 AM
  4. Encryption/Decryption -- Correction
    By Abdi in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2002, 08:00 AM
  5. pls help with the correction...
    By leena_teoh in forum C Programming
    Replies: 1
    Last Post: 03-20-2002, 05:18 AM