C Over-speed Problem
A bus was designed by a company such that it has a console which monitors real time activities performed by bus driver as well as its conductor for monitoring purposes. The data of console was to store in a log file (say flat text file). To control the high speed accidents, a monitoring mechanism was in the bus in which time and speed of bus was logged in text file when bus overshoots the prescribed limit. The mechanism also logs the duration where driver was overspeeding the bus. Now there is a problem that you have to write a program in C, to log speed and time of bus when bus driver overshoot the prescribed limit and return to the prescribed limit. Note, the log should be entered when bus speed crosses the limit upward and when it returns to its normal speed.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char *os; //Time Stamp String
clock_t ost; //Seconds since 1 Jan 1976
int s; //Speed of bus
int osf = 0; //Over speed flag
int osfw = 0;//Over speed log written
int ps = 60; //Prescribed speed of bus
int main(int argc, char** argv) {
while (1) {
printf("Enter the speed : ");
scanf("%d", &s);
if (s > ps) { //Set the overspeed flag
osf = 1;
} else if (s < ps) {//Reset overspeed flag
osf = 0;
}
if (osf == 1 && osfw == 0) {//If overspeed Flag & output not written
ost = time(NULL); //Time
os = ctime(&ost); //Time string
printf("High Speed %d, at %s", s, os);
osfw = 1; //Set write flag
} else if (osf == 0 && osfw == 1) {//otherwise
ost = time(NULL); //Time
os = ctime(&ost); //Time string
printf("Normal Speed %d, at %s", s, os);
osfw = 0; //Reset write flag
}
}
return 0;
}