Thread: Pointers, arrays , functions

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Dear Mangesh:

    The solution may be literally simple, but your explanation was not!

    Could you please rewrite the program above in the terms you described?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #17
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Just prettying up the code.... there's an awful lot of redundant veriable declarations here.

    Code:
    #include <stdio.h>
    
    /*  define year to 365 days and month to 30 days */
    
    #define YEAR 365
    #define MONTH_DAYS 30
     
    /*  prototypes   */
    
    // QC: Use descriptive variable names in your prototypes!
    void enter_data(int * pmonth, int * pday, int * pyear);
    void time_elapsed(int month, int day, int year, int * pdays, int * phours);
    
    
    main()
    {  
         /* QC: Unless you're using dynamic memory,
             you don't need to declare pointers. */
         int month, day, year, days, hours;
    
         printf("input current date as MM/DD/YY\n");
              
         //This function is right...
         enter_data(&month, &day, &year);
    
         // This one needed changed.
         time_elapsed(month, day, year, &days, &hours);
         
         printf("time elapsed since January 1, 2000\n" }; 
         printf("time elapsed in days", days);
         printf("time elapsed in hours", hours);
    
    }
    
    // QC: Changed the names for clarity.
    void enter_data(int * pmonth, int * pday, int * pyear)
    {  
        // QC: Don't use &month, &day... because these are
        //   already pointers.
        scanf("%d %d %d", pmonth, pday, pyear);
        // QC: But since they're pointers, if you want to change the 
        //   adressed value, use *year and not just year.
        *year -= 2000;
    }
    
    // QC: Changed it the same as last function...
    // QC: Also changed numbers to macros... macros are created to be used.
    void time_elapsed(int month, int day, int year, int * pdays, int * phours)
    {
          
          year *= YEAR;
          month *= MONTH_DAYS;
          *pdays = month+day+year;
          *phours = (*pdays) * 24;
    }
    I'm not sure everything works here, but it looks close. This is very very similar to what you had... but the changes in enter_date are critical.

  3. #18
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    thank you all

    Thanks for the help. Everyone.

    I think I need to clarify that the original post showed the parameters needed as defined by instructions of assignment. Although I see no real need to have a separate function to scan input in, that is what the assignment called for.

    I think I get the need for pointers now. Understanding the need for a pointer to pass integers values back so values don't reverst back to zero seems weird to me, but I get it.

    Sometimes things are the way they are. Even if they don't entirely make sense. Creating programs is a challenge for me.
    Hence, that is why I sign off as
    DAZED AND CONFUSED.

    If I have any problems with this one still after compiling, I shall write more.
    Sue B.

    dazed and confused


  4. #19
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    Code with explanation.

    Dear friends,

    I have tried to fit the code as per expected. Explanatin is also given at each step. I would love to solve the upcoming queries.

    Thanks and regards,
    Mangesh.

    #include <stdio.h>

    /* define year to 365 days and month to 30 days */

    #define YEAR 365
    #define MONTH_DAYS 30

    /* prototypes */
    void enter_data(int *, int *, int *);
    void time_elapsed(int, int, int, int *, int*);

    main()
    {
    int month, day, year;
    int DAYS, HOURS;

    printf("input current date as MM/DD/YY\n");

    /* The parameters passed should persist their values, hence address is passed. If you don't use pointers copies of these variables will be created in different memory space and those will not be persistant. */
    enter_data(&month,&day,&year);

    /* Here time_elapsed function is not manipulating month, day and year variables. But it is manipulating DAYS and HOURS variables. So, these variables should persist their values. Hence address is passed. */
    time_elapsed(month,day,year,&DAYS,&HOURS);

    printf("time elapsed since January 1, 2000\n" };
    printf("time elapsed in days", DAYS);
    printf("time elapsed in hours", HOURS);

    }


    /* This function is passed three pointer parameters because the values of year, month and day taken from user are being manipulated to calculate exact number of years, month and days. */
    void enter_data(int *month, int *day, int *year)
    {
    scanf("%d %d %d", &month, &day, &year);
    year = year - 2000;
    month = month - 1;
    day = day - 1;
    }

    /* This function doesn't require first three parameters as pointers because these variables are not required to print the time elapsed information in the main() function. Remember, main() requires only two variables 'DAYS' and 'HOURS' to print the time elapsed data. Hence, only these two variables : 'DAYS' and 'HOURS' should be passed as pointers. If we had defined these variables(DAYS/HOURS) as global, there was no necessity to pass them as pointers. When you declare a variable, the scope of the variable is limited to the block only. Also note that, it is not necessary to have the same veriable names as parameters to the functions. I mean, the following function will work same if you change the name of the parameters. These variables are totally different variables than the passed one.*/
    void time_elapsed(int month, int day, int year, int *DAYS, int *HOURS)
    {
    DAYS = day + month * MONTH_DAYS + year * YEAR;
    HOURS = DAYS * 24;
    }

  5. #20
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    extremely sorry, earlier code had some errors. please follow this.

    #include <stdio.h>

    /* define year to 365 days and month to 30 days */

    #define YEAR 365
    #define MONTH_DAYS 30

    /* prototypes */
    void enter_data(int *, int *, int *);
    void time_elapsed(int, int, int, int *, int*);

    main()
    {
    int month, day, year;
    int DAYS, HOURS;

    printf("input current date as MM DD YYYY\n");

    /* The parameters passed should persist their values, hence address is passed. If you don't use pointers copies of these variables will be created in different memory space and those will not be persistant. */
    enter_data(&month,&day,&year);

    /* Here time_elapsed function is not manipulating month, day and year variables. But it is manipulating DAYS and HOURS variables. So, these variables should persist their values. Hence address is passed. */
    time_elapsed(month,day,year,&DAYS,&HOURS);

    printf("time elapsed since January 1, 2000\n" );
    printf("time elapsed in days : %d", DAYS);
    printf("time elapsed in hours : %d", HOURS);

    }


    /* This function is passed three pointer parameters because the values of year, month and day taken from user are being manipulated to calculate exact number of years, month and days. */
    void enter_data(int *month, int *day, int *year)
    {
    scanf("%d %d %d", month, day, year);
    *year = *year - 2000;
    *month = *month - 1;
    *day = *day - 1;
    printf("\n%d %d %d",*year,*month,*day);

    }

    /* This function doeasn't require first three parameters as pointers because these variables are not required to print the time elapsed information in the main() function. Remember, main() requires only two variables 'DAYS' and 'HOURS' to print the time elapsed data. Hence, only these two variables : 'DAYS' and 'HOURS' should be passed as pointers. If we had defined these variables(DAYS/HOURS) as global, there was not necessity of pass them as pointers. When you declare a variable the scope of the variable is limited to the block only. Also note that, it is not necessary to have the same veriable names as parameters to the functions. I mean, the following function will work same if you change the name of the parameters. These variables are totally different variables than the passed one.*/
    void time_elapsed(int month, int day, int year, int *DAYS, int *HOURS)
    {
    *DAYS = day + month * MONTH_DAYS + year * YEAR;
    *HOURS = (*DAYS) * 24;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  2. Problem with arrays, pointers and functions when combined
    By The Wazaa in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2006, 10:44 AM
  3. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM