Question

A. Write C code to create a structure called time_of_day, which stores the current time in...

A. Write C code to create a structure called time_of_day, which stores the current time in hours, minutes, and seconds. All the fields should be integers except for seconds, which should be a floating point value.

B. Write a C function called check_time, which takes a pointer to a time_of_day structure as input, and return 1 if the time is valid (0 to 23 hours, 0 to 59 minutes, 0 to 59.999999 seconds) and 0 otherwise. Assume that times are stored in 24 hour format, with 00:00:00 being midnight and 23:59:59.9999 being the last millisecond of the day.

C. Write a main program which reads a time from the user in the normal hh:mm:ss.sss format and stores it in a time_of_day structure. Your program should then call the check_time function to determine if it is a valid time or not, and display "valid time" or "invalid time" to the screen.

Homework Answers

Answer #1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct time{
    int hr;
    int min;
    double sec;
}time;

int check_time(time t)
{
    if((t.hr>=0 && t.hr<=23) && (t.min>=0 && t.min<=59) && (t.sec>=0.0 && t.sec<60.0))
        return 1;
    else 
        return 0;
}

int main()
{
    time t;
    printf("Enter time : hh:mm:ss.sss\n");
    char c[30],h[10];
    scanf("%s",c);
    int i;
    for(i=0;c[i]!=':';i++)
    {
        strncat(h,c+i,1);
    }
    t.hr=atoi(h);
    h[0]=0;

    for(i=i+1;c[i]!=':';i++)
    {
        strncat(h,c+i,1);
    }
    t.min=atoi(h);
    h[0]=0;

    for(i=i+1;c[i]!='\0';i++)
    {
        strncat(h,c+i,1);
    }
    t.sec=atof(h);


    if(check_time(t))printf("valid time\n");
    else
    {
        printf("invalid time\n");
    }
    return 0;
}
Know the answer?
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
Similar Questions
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56). If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one...
Write a program that prompts the user to enter time in 12-hour notation. The program then...
Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your program must contain three exception classes: invalidHr, invalidMin, and invalidSec. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object. Follow similar conventions for the invalid values of minutes and seconds. This needs to be done in C++ There needs to be a separate header file for each...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM. (A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below: void input(int& hours24, int& minutes); //Precondition: input(hours, minutes) is called with //arguments capable of being assigned values. //Postcondition: // user is prompted for time in 24 hour format: // HH:MM, where...
FOR PYTHON Rewrite your pay computation with time-and-a-half for overtime and create a function called compute_pay...
FOR PYTHON Rewrite your pay computation with time-and-a-half for overtime and create a function called compute_pay which takes two parameters (hours and rate). Enter Hours: 45 Enter Rate: 10 Pay: 475.0 YOU WILL NEED THREE FUNCTIONS: the_hours, the_rate = get_input() the_pay = compute_pay(the_hours, the_rate) print_output(the_pay) Call the functions and passing the arguments in the "main" function. Example: def main(): the_hours, the_rate = get_input() the_pay = compute_pay(the_hours, the_rate) print_output(the_pay) main() ----- Testing the outputs of your code: 1 for the valid...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE USER ENTERS A VALID INTEGER. printDouble should accept one value and should print the value of that number times 2. printEndMessage should not accept any values and should print a message indicating that the program has finished. def main(): num1 = int(input("Please enter your number ")) printDouble(num1) printEndMessage() main()
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and...
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and b (where a<b). Then simply RETURNS a string containing all the prime numbers between a and b (or if there are none, the string "No Primes"). You should check that a and b are valid inputs, that is, that a and b are integers such that a<b (otherwise, the function should print “No Primes”). Three sample calls of your function (in IDLE) should produce...
using Matlab 1) use the if/else structure to solve these problems (a,b) a) write a programme...
using Matlab 1) use the if/else structure to solve these problems (a,b) a) write a programme that reads an integer from the user. then your program should display a message whether the integer is even or add. b) many solid-fuel rocket motors consist of three stages. Once the first stages burns out, it separates from the missile and the second stage lights. then the second stages burns out and separate and the third stage light.Finally, once the third stages burns...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT