Thread: Pls help me..really dunno where to start with this question..

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    Pls help me..really dunno where to start with this question..

    I think must use pointers in this prog but i dunno where to start..

    "you are given a time in seconds. You are required to write a full program consists of a function
    to convert it to hours, minutes and seconds. Your function should require three address parameters
    to return the values."

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It probably means something like this:

    You want to convert the seconds to hours, minutes and seconds. So 3601 secs would be 1h 0m 1s. So you have 3input and you want to give 1 output.

    In C there are two ways to actually store the outputs with a function. Those are
    Code:
    //1st
    int fun()
    {
       int output;
       ...
       output = value;
       ...
       return output;
    }
    //Call like
    int output;
    output = fun();
    
    //2nd
    void fun(int* output)
    {
        ...
        *output = value;
        ...
    }
    //Call like
    int output;
    fun(&output);
    considering that you want to save a value of type. You will save that value inside a variable names output.

    You will use the second method. To hint it should be like
    Code:
    void fun(in1, in2, in3, out);
    where in1, in2, in3 are you input and out your output, of course you would need to put types on them.

    If you need any clarifications just keep posting 'till you figure it out.

    EDIT: The & symbol means that you get the address. You could write the second like this to be more clear
    Code:
    int output;
    int* outPtr;
    outPtr = &output; //point to variable
    fun(outPtr);          //the variable output has the value you want, since it where outPtr points
    Last edited by C_ntua; 08-12-2010 at 09:24 PM.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    void fun(in1, in2, in3, out);
    Actually, the question only allows 3 inputs.
    So something like this:

    Code:
    void fun(int * secs, int * mins, int * hrs);

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The question asks for three parameters to return the result. So you'd have to a 4th to pass the actual inputted time to the user probably.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Example usage of your function will be something like:
    Code:
    int hour,sec,min;
    
    sec = 1000;
    foo(&hour,&min,&sec);       // name is just eg ONLY!
    printf("Hour = %d, min = %d, sec = %d\n",hour,min,sec);
    Post the code if you have any problem.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I honestly think this is better, Separate input from output.
    Code:
    int hour,sec,min;
    int total_time = 1000;
    
    foo(total_time, &hour,&min,&sec);       // name is just eg ONLY!
    printf("Hour = %d, min = %d, sec = %d\n",hour,min,sec);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I'd rather use struct.

    Code:
    typedef struct Time {
      int hour;
      int min;
      int sec;
    } Time;
    void foo(Time *t)
    {
      ...
    }
    
    Time t = { .sec = 1000 };
    foo(&t);
    print_time(&t);

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think there are enough variables to bother with that.
    Anyhow, it violates the requirements which is 3 variables out.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. A Stupid Question. Pls Help.
    By Antigloss in forum C Programming
    Replies: 11
    Last Post: 09-16-2005, 12:15 PM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. A question asked a million times, but where do I start?
    By Unregistered in forum Game Programming
    Replies: 10
    Last Post: 09-09-2001, 09:15 AM