Write a program that asks the user to enter time of day. The program will conver
ID: 3756394 • Letter: W
Question
Write a program that asks the user to enter time of day. The program will convert from 12-hour format to the 24-hour format, or vice-versa. First write out your strategy as pseudocode in a .txt file. Include the inputs/outputs you expect each function to have. Here are the program specs: 1. Ask for an input of hour, minute, and seconds. 2. Then ask the user if the time entered was in the 24-hour or 12-hour format. a. If the user responds with 12-hour, ask whether entered time was AM or PM. Otherwise, ask no more questions. 3. Convert the time to the other time format (If user entered in 12-hr format, convert to 24-hr format. If user entered in 24-hr format, convert to 12-hr format.) 4. Check each of the user entries to ensure they are in the appropriate range. For example, none of the values should be negative, minutes should be in the range of 0 to 60, if user indicated 12-hour clock then value should be b/w 1 and 12, etc.. If any entry is incorrect, use error() functions to display meaningful messages for each error situation(these functions will also end the program execution). 5. Display the converted time stating clearly the new format. 6. Modularize your code. Remember that in MATLAB you can have functions with multiple inputs and/or outputs. (Sections 6.1 and 6.2 in the text.) a. Have a function that takes in all the inputs for part 2. Your error checking should be implemented within this function. (getInputs) b. Have a conversion function for part 3. (convertTime) c. Have a display function for part 4. (displayTime)
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define BOOL int
#define TRUE 1
#define FALSE 0
/*
4. Check each of the user entries to ensure they are in the appropriate range. For example, none of the values should be negative, minutes should be in the range of 0 to 60, if user indicated 12-hour clock then value should be b/w 1 and 12, etc.. If any entry is incorrect, use error() functions to display meaningful messages for each error situation(these functions will also end the program execution).
5. Display the converted time stating clearly the new format.
6. Modularize your code. Remember that in MATLAB you can have functions with multiple inputs and/or outputs. (Sections 6.1 and 6.2 in the text.)
a. Have a function that takes in all the inputs for part
2. Your error checking should be implemented within this function. (getInputs)
b. Have a conversion function for part
3. (convertTime)
c. Have a display function for part
4. (displayTime)*/
typedef struct Time {
int h, m, s; // variables to represent hours, minutes and seconds
BOOL is12;
BOOL isAM;
} Time;
void error(char* msg) {
printf("ERROR : %s", msg);
exit(1);
}
BOOL verifyTime(Time t) {
return TRUE;
}
Time readTime() {
Time t;
printf("Enter hours : ");
scanf("%d", &t.h);
printf("Enter minutes : ");
scanf("%d", &t.m);
printf("Enter seconds : ");
scanf("%d", &t.s);
printf("Is the time in 12H format(1-yes, 0-No) : ");
scanf("%d", &t.is12);
if(t.is12 == TRUE) {
printf("Is the time in AM or PM(1-AM, 0-PM) : ");
scanf("%d", &t.isAM);
}
return t;
}
Time convertTo24(Time t) {
t.is12 = FALSE;
if(t.isPM) {
t.h = t.h + 12;
}
return t;
}
Time convertTo12(Time t) {
t.is12 = TRUE;
if(t.h > 12) {
t.h = t.h - 12;
t.isAM = FALSE;
} else {
t.isAM = TRUE;
}
return t;
}
int main() {
Time t = readTime();
if(t.is12) {
convertTo24(t);
} else {
convertTo12(t);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.