0

I have my program working and logic correct and I'm able to use another function and just fine, but my requirements are to pass the addresses of my three variable hour, min, sec, not the actual variable itself (which is declared 0 when it reaches the time() function) I've just made my code so it would work for the time being.

int main(int argc, char *argv[])
{
    int time(int, int, int, int);       //function protoype
    int seconds;
  
    printf("Enter the number of seconds:\n");
    scanf("%d", &seconds);
    
    int min, hour, sec = 0;

    time(seconds, hour, min, sec);

  system("PAUSE");	
  return 0;
}

int time(int seconds, int hour, int min, int sec)
{
    //get the minutes from the seconds input
    min = seconds / 60;
    //get hours from the minutes
    hour = min / 60;
    //get the seconds from the remainder of min
    sec = seconds % 60;
    //redeclare minutes form the remainder of hour 
    //any minutes over 60 needs to be an hour, not more than 60 minutes
    min = min % 60;
    
    printf("hour: %d\n", hour);
    printf("min: %d\n", min);
    printf("sec: %d\n", sec);     
}

I know in the method header AND method call I cannot accept or pass (respectively) addresses with the & prefix. Should I just make 3 new ints to hold the addresses?

EG int minAddr = &min; same for the rest and just pass those to the method header?

Afterwards would I have to change the equations to *minAddr = seconds / 60; ?

Modularity has always been tough for me, any help is appreciated.

1
Contributor
1
Reply
3
Views
5 Years
Discussion Span
Last Post by loserspearl
0

I believe I got it to work with this, If anyone sees something wrong let me know.

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
    int time(int, int *, int *, int *);       //function prototype
    int seconds;
  
    printf("Enter the number of seconds:\n");
    scanf("%d", &seconds);
    
    int min, hour, sec = 0;
    int *minAddr = &min;
    int *secAddr = &sec;
    int *hourAddr = &hour;

    time(seconds, &minAddr, &secAddr, &hourAddr);

  system("PAUSE");	
  return 0;
}

int time(int seconds, int *minAddr, int *secAddr, int *hourAddr)
{
    printf("hourAddr: %d\n", hourAddr);
    printf("minAddr: %d\n", minAddr);
    printf("secAddr: %d\n", secAddr);
    
    //get the minutes from the seconds input
    *minAddr = seconds / 60;
    //get hours from the minutes
    *hourAddr = *minAddr / 60;
    //get the seconds from the remainder of minutes
    *secAddr = seconds % 60;
    //redeclare minutes form the remainder of hour 
    //any minutes over 60 needs to be an hour, not more than 60 minutes
    *minAddr = *minAddr % 60;
    
    printf("hour: %d\n", *hourAddr);
    printf("min: %d\n", *minAddr);
    printf("sec: %d\n", *secAddr);  
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.