Question

C please In this question, you will find the difference between two time durations. You are...

C please

In this question, you will find the difference between two time durations. You are given the following struct definitions:

typedef struct _duration {
    int hours;
    int minutes;
} Duration;

Design a function to find the difference between two durations:

Duration* subtract(Duration* duration1, Duration* duration2) {

}

Return a pointer to a new duration struct that contains the difference between the two.

It may be useful to reduce the durations to a single unit (example: minutes), find the difference, and then convert it back to hours and minutes.

Duration 1 will always be longer than duration 2.

Your answer should be in reduced form. For example, a time of "0 hours and 130 minutes" would be incorrect. The expected result, in this case, would be "2 hours and 10 minutes".

You must include the main function in your submission (provided in the template) so the code will compile. Note that only the return value of the subtract function will be tested.

Examples

Example 1 (provided in template)

Given duration1 is a pointer to a duration struct with:

5 hours and 30 minutes

And given duration2 is a pointer to a duration struct with:

1 hours and 55 minutes

If you call Duration* difference = subtract(duration1, duration2)

difference should be a pointer to a struct containing 3 hours and 35 minutes.

Example 2

Given duration1 is a pointer to a duration struct with:

6 hours and 45 minutes

And given duration2 is a pointer to a duration struct with:

2 hours and 15 minutes

If you call Duration* difference = subtract(duration1, duration2)

difference should be a pointer to a struct containing 4 hours and 30 minutes.

TEMPLATE

#include <stdio.h>

#include <stdlib.h>

typedef struct _duration {

int hours;

int minutes;

} Duration;

Duration* subtract(Duration* d1, Duration* d2) {

// CODE HERE

}

// Example main. You only need to write 'subtract', but this is here to help you test.

int main() {

Duration a = {5, 30}; // Struct shorthand for 5 hours, 30 minutes

Duration b = {1, 55}; // Struct shorthand for 1 hour, 55 minutes

Duration* d = subtract(&a, &b);

printf("%d:%d\n", d->hours, d->minutes);

// Expected output:

// 3:35

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <stdio.h>

#include <stdlib.h>

typedef struct _duration {

int hours;

int minutes;

} Duration;

Duration *subtract(Duration *d1, Duration *d2) {

Duration *temp = (Duration *)malloc(sizeof(Duration));

int tot_mins = (d1->hours*60 + d1->minutes) - (d2->hours*60 + d2->minutes);

temp->hours = tot_mins/60;

temp->minutes = tot_mins%60;

return temp;

}

// Example main. You only need to write 'subtract', but this is here to help you

// test.

int main() {

Duration a = {5, 30}; // Struct shorthand for 5 hours, 30 minutes

Duration b = {1, 55}; // Struct shorthand for 1 hour, 55 minutes

Duration *d = subtract(&a, &b);

printf("%d:%d\n", d->hours, d->minutes);

// Expected output:

// 3:35

}

Add a comment
Know the answer?
Add Answer to:
C please In this question, you will find the difference between two time durations. You are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define a type which comprises a struct called "Maxima". In this struct contains two int values...

    Define a type which comprises a struct called "Maxima". In this struct contains two int values a and b. The purpose of this struct is to store the largest two int values among a set of integers. The value a is the largest number and the value b is the second largest number. In order to accomplish this task, you need to write the following functions: allzero( struct pointer ): This function sets a and b values in a given...

  • Please in C Language Thank you! The following program source code is incomplete 1 #include <stdio.h> 2 3 // TODO:...

    Please in C Language Thank you! The following program source code is incomplete 1 #include <stdio.h> 2 3 // TODO: 1) Add the typedef here: 5// TODO: 2) Modify the parameter of repeat to add irn the function pointer for the function to be called repeatedly: 8 void repeat (int times) for (int k 0; k < times; ++k) 12 // TODO: 3) Add the call to the function pointer here: 14 15 17 void test (void) 18 printf("Test!\n"); 19...

  • I need help on this Systems review please! it's due by midnight monday. Question 1 Not...

    I need help on this Systems review please! it's due by midnight monday. Question 1 Not yet answered Points out of 1.00 Flag question Question text Using these declarations: int * numberPointers[3]; int * pointer; int number; Which of the following statements would generate a warning or error? Select one: a. number = pointer; b. *pointer = number; c. pointer = numberPointers; d. numberPointers[2] = &number; e. a., b., and d. f. a. and c. Question 2 Not yet answered...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • modify the code for timer_test_02.c to allow the time delay between events to be pseudo- random...

    modify the code for timer_test_02.c to allow the time delay between events to be pseudo- random exponential, with a mean time between arrivals of 0.1 second. Change the limit in the time_stamps() function from 5 time-stamps to 10, so that the mean run-time will be about 10*0.1 = 1.0 seconds. Once this is working, you should be able to generate 10 events with a pseudo-random exponential arrival process. The code is: #include <stdio.h> #include <stdint.h> #include <time.h> #include <unistd.h> #include...

  • IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node;...

    IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node; typedef struct { int size; // Number of items on user’s list Node *head, *tail; } List; //In addition to creating an empty list, this function will also create two dummy nodes; one for the head, and the other for the tail. List* createList(); //This function creates a node containing the provided item, then inserts it into the list pointed by the provided list...

  • Part 5: C++ Programming (25/100) 13) (5 marks) Explain the difference between a call by value...

    Part 5: C++ Programming (25/100) 13) (5 marks) Explain the difference between a call by value and a call by reference. 14) (5 marks) What value of y will be displayed when the following ++ program is run: tinclude <iostream int main) double x 3.0, y: y1/3 x16.0) std::cout <<y<"n" return (0): Show the details step by step as how the computer would perform the calculation and justify your resuits. Also, what will the data type of y be? For...

  • How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity =...

    How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity = 8 0 5 10 15 20 Capacity = 16 0 5 10 15 20 25 30 35 40 Capacity = 32 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125...

  • Write a complete C program for an automatic teller machine that dispenses money. The user should...

    Write a complete C program for an automatic teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispenses are 50s, 20s, and 10s. Write a function that determines how many of each kind of bill to dispense. When writing your function begin to pass your variables using pointers (or rather "pointing" to your data". Use the TimeSpace program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT