Thread: pointers

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    pointers

    hello
    this is niharika
    i need help with an assignment.
    the question is

    Implement a function that converts a day of the year (a number between 1 and 366) into a month and day.

    int dateSplit(int dayOfYear, int year, int *day, int *month);
    The function should return 1 if the conversion is sucessful and 0 if dayOfYear and/or year are not valid.

    A sample program testing the function, along with its output, is given below.

    Your test program and your solution should be placed in separate files. Submit only the file dateSplit.c containing your solution function.

    Code:
    Sample Test Program 
    #include <stdio.h>
    
    int dateSplit(int dayOfYear, int year, int *day, int *month);
    
    void testDateSplit(int dayOfYear, int year)
    {
      int day, month;
    
      if (dateSplit (dayOfYear, year, &day, &month))
        printf ("&#37;d,%d => %d,%d\n", dayOfYear, year, day, month);
      else
        printf ("%d,%d => invalid\n", dayOfYear, year);
    }
    
    int main (void)
    {
      testDateSplit (100, 2007);
      testDateSplit (100, 2008);
      testDateSplit (1, 2007);
      testDateSplit (1, 2008);
      testDateSplit (365, 2007);
      testDateSplit (366, 2007);
      testDateSplit (366, 2008);
      testDateSplit (-1, -1);
    
      return 0;
    }
    Sample Output
    100,2007 => 10,4
    100,2008 => 9,4
    1,2007 => 1,1
    1,2008 => 1,1
    365,2007 => 31,12
    366,2007 => invalid
    366,2008 => 31,12
    -1,-1 => invalid
    here i need to write the main funtion for this program statement as the test program is already given. please help me. i nee the code. it is urgent
    Last edited by Salem; 11-03-2008 at 12:41 PM. Reason: Snip email address - you posted it here, you read it here

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    To change the value of a pointer you do this:

    *day = 44;

    Example:

    Code:
    int dateSplit(int dayOfYear, int year, int *day, int *month)
    {
      ...
      *day = 20;
      *month = 6;
      ...
    }
    
    void testDateSplit(int dayOfYear, int year)
    {
      int day, month;
    
      if (dateSplit (dayOfYear, year, &day, &month))
        printf ("&#37;d,%d => %d,%d\n", dayOfYear, year, day, month);
      else
        printf ("%d,%d => invalid\n", dayOfYear, year);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM