Thread: Whats Works?

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    19

    Whats Works?

    I have been given the following question and I am very confused about the possible answers. I don't know what the desired output is, what would work, or what the correct answer is. If someone is to help me, can you please explain as well, rather than simply giving the answer? Or even better, can you please explain, so I can get the answer myself? Thank you very much.

    Which of the following is lines can be inserted into this program:

    Code:
                            int f( int n ) { return n; }
                            void main( void ) {
                                        int i = 10;
                                        int j;
      < HERE >                        }
     
              a) f( i ) = j;
                b) f[ i ] = j;
                c) j = f( i );
                d) j = f[ i ];
                e) f( j, i );
    
    

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The question is asking which of those lines can be inserted after the int j; in main().

    What is f()?

    Can you describe what each line a), b), ... e) seems to be trying to do and if what that line is trying to do is valid?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can always experiment

    start with original file and compile it using gcc with -Wall

    Fix errors in the original program and then add each of the lines proposed and check the errors and try to understand what is causing the errors
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    It's a silly question, designed to confuse rather than illuminate. It's also unsound, the answer to the last one depends which version of C you're using.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ihackress View Post
    I have been given the following question and I am very confused about the possible answers. I don't know what the desired output is, what would work, or what the correct answer is. If someone is to help me, can you please explain as well, rather than simply giving the answer? Or even better, can you please explain, so I can get the answer myself? Thank you very much.

    Which of the following is lines can be inserted into this program:

    Code:
                            int f( int n ) { return n; }
                            void main( void ) {
                                        int i = 10;
                                        int j;
      < HERE >                        }
     
              a) f( i ) = j;
                b) f[ i ] = j;
                c) j = f( i );
                d) j = f[ i ];
                e) f( j, i );
    
    
    This is the key:
    Code:
     int f( int n ) { return n; }
    My disclaimer: I've never seen a C function declared with a "{ return n;}", part on the end. It should be:
    Code:
    int f(int n);
    IMO.

    Choice 'a' is out because it has the return value and equal sign placed incorrectly.

    f() (the function call syntax), uses parenthesis, not square brackets, so choices b and d are out of contention.

    The function f() must have a single int parameter. Choice e doesn't have that, so it's out.

    So pick any remaining choice. < grin >
    Last edited by Adak; 07-08-2013 at 06:14 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My disclaimer: I've never seen a C function declared with a " {return n;}", part on the end. It should be:
    What? You've never seen a function that returns a value. Remember that one line is the same as:
    Code:
    int f( int n ) 
    { 
       return n; 
    }
    Which is a valid function.

    You can eliminate both b and d because f is a function not an array. Since the function only has one parameter you can eliminate e. So you're left with a and c. I'll leave you with which of these is the correct choice.

    Jim

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought perhaps it was some kind of new-fangled prototype that I hadn't run across. A function that does nothing - not even a comment for a stubbed function in a developing program - couldn't get my head wrapped around that.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    WAIT!!! i LOVE the
    Code:
    void main( void )
    time to school the teacher that this is NOT acceptable C programming.....next lessons GOTO!!!

    i love telling teachers they are wrong!

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    basically they are asking, to call function "f" how would you do it

    and because "f" returns a value, how to save that value as "j"

    Code:
    int f( int n ) 
     {
      return n;
     }
    int main( void )
     {
      int i = 10;
      int j;
      //call function f here, sending i and saving return in j//
      return 0;
     }
    Last edited by Crossfire; 07-08-2013 at 05:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use c++ to do my works on web
    By mozee in forum C++ Programming
    Replies: 7
    Last Post: 09-18-2010, 09:09 PM
  2. Why it works?
    By forumuser in forum C Programming
    Replies: 1
    Last Post: 02-12-2008, 08:02 AM
  3. How come this works?
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 02-11-2006, 05:15 PM
  4. Whats the fastest api for 2d and whats the best for 3d?
    By Josh Kasten in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2003, 09:48 PM
  5. Let's see how this works out
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-02-2001, 12:36 AM

Tags for this Thread