Is it possible a function that takes the two structures as argument in C?

130 views Asked by At

I've done this code, but I do not like it, I am trying to learn structures and I would like to make it simpler, is it possible to do it with one function that takes the two structures?

I've tried to do it in that way on my own, but I was unsuccessful and I would appreciate a suggestion as I was unable to pass two structures into a function...

my code:

#include <stdio.h>
int timeDifference (int startTime, int endTime);
int countSeconds (struct time time1);
int timeCount (int inputSeconds);
int abs(int number);

struct time
{
    int hours;
    int minutes;
    int seconds;
};

struct time time1 = {3,45,15};
struct time time2 = {9,44,03};


int main(int argc, char const *argv[])
{


    printf("\nStart Time in seconds = %d", countSeconds(time1));
    printf("\nEnd Time in seconds = %d", countSeconds(time2));
    printf("\nThe difference in seconds = %d",timeDifference(countSeconds(time2),countSeconds(time1)));
    timeCount(timeDifference(countSeconds(time2),countSeconds(time1)));


       return 0;
}

//The duration is 5 hours, 58 minutes and 48 seconds

int timeDifference (int startTime, int endTime)
{
 int diff;


 diff = abs(endTime - startTime); //diff in seconds

 return diff;

}

// function to change the time to seconds
int countSeconds (struct time time_)
{
    int count;

    count = time_.hours*60*60 + time_.minutes*60 + time_.seconds;


    return count;

}

//https://stackoverflow.com/questions/30829923/i-want-to-multiply-two-different-structures-in-c-language

int abs(int number)
{
 if (number < 0)
  number = number * -1;

  return number;
}

//program to count hours. min, seconds

int timeCount (int inputSeconds)
{
    int h,m,s; //hours, seconds, minutes
    int remainingSeconds, secondsInHour = 3600, secondsInMinute = 60;

    h = inputSeconds/secondsInHour;
    remainingSeconds = inputSeconds - (h * secondsInHour);
    m = remainingSeconds/secondsInMinute;
    remainingSeconds = remainingSeconds - (m*secondsInMinute);
    s = remainingSeconds;

    printf("\n%d hour, %d minutes and %d seconds",h,m,s);
}

I would appreciate an example so that I can work on it.

1

There are 1 answers

1
Thomaz Capra On

is it possible to do it with one function that takes the two structures

Yes, it is. If I may suggest, you could use a typedef to ease your structure use.

With my changes, the timeDifference's function is reveiving as parameter two structures.

#include <stdio.h>

struct time
{
    int hours;
    int minutes;
    int seconds;
};

typedef struct time TimeStructure;

int timeDifference (TimeStructure startTime, TimeStructure endTime);
int countSeconds (TimeStructure time1);
int timeCount (int inputSeconds);
int abs(int number);

TimeStructure time1 = {3,45,15};
TimeStructure time2 = {9,44,03};

int main(int argc, char const *argv[])
{
    printf("\nStart Time in seconds = %d", countSeconds(time1));
    printf("\nEnd Time in seconds = %d", countSeconds(time2));
    printf("\nThe difference in seconds = %d", timeDifference(time2, time1));
    timeCount(timeDifference(time2, time1));
    return 0;
}

//The duration is 5 hours, 58 minutes and 48 seconds

int timeDifference(TimeStructure startTime, TimeStructure endTime)
{
    return abs(countSeconds(endTime) - countSeconds(startTime));
}

// function to change the time to seconds
int countSeconds(TimeStructure time)
{
    return (time.hours * 60 * 60) 
            + (time.minutes * 60)
            + time.seconds;
}

//https://stackoverflow.com/questions/30829923/i-want-to-multiply-two-different-structures-in-c-language

int abs(int number)
{
    if (number < 0)
        number = number * (-1);
    return number;
}

//program to count hours. min, seconds

int timeCount(int inputSeconds)
{
    int h,m,s; //hours, seconds, minutes
    int remainingSeconds, secondsInHour = 3600, secondsInMinute = 60;

    h = inputSeconds/secondsInHour;
    remainingSeconds = inputSeconds - (h * secondsInHour);
    m = remainingSeconds/secondsInMinute;
    remainingSeconds = remainingSeconds - (m*secondsInMinute);
    s = remainingSeconds;

    printf("\n%d hour, %d minutes and %d seconds",h,m,s);
}

You can even run these changes in this online playground: https://code.sololearn.com/cx4w4AxDg4Lc