D
Darklight
posted by ash4640 These question posted 15/8/04
How do I write these programs in c I am a beginner learning c.
1.
write a program to read 2 inetegers with the following signficance the
first integer value represents a time of day on a 24hour clock, so that
1245 represents quater to one mid-day for eg. The second integer
represents a time duration in a similar way, so that 345 represents
three hours & 45 minutes. This duration is to be added to the first
time, and the result printed out in the same notation, in this case
1630 which is the time 3hours and 45 minutes after 1245.
Typical output might be start time is 1415. Duration is 50 End time is
1505.
Here is my answer to the above question i would like to know if it
is any good or crap constructive critisms accepted
I am just a novice who plays around with c
/* TIME2.C TO ADD TWO 24 HOUR INPUTS */
#include<stdio.h>
#define MAX 100
#define MINS 60
#define HOUR 24
int main(void)
{
int time24,mins,hour24,hourout;
int hourtotal,hour,minstotal;
char line[20];
int time[7]; /* ARRAY TO HOLD DIFFERENT VALUES */
/* SEPERATE HOURS FROM MINUTES */
do{
printf("Enter 24 hour start time: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%d",&time24);
mins = time24 % MAX;
hour24 = time24 - mins;
hourout = hour24 / MAX;
time[0] = hourout; /* hours start */
time[1] = mins; /* minutes start */
}while(time[0] >= HOUR || time[1] >= MINS);
do{
printf("Enter duration: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%d",&time24);
mins = time24 % MAX;
hour24 = time24 - mins;
hourout = hour24 / MAX;
time[2] = hourout; /* hours end */
time[3] = mins; /* minutes end */
}while(time[2] >= HOUR || time[3] >= MINS);
/* ADD START AND END MINUTES */
hourtotal = time[1] + time[3];
if(hourtotal >= MINS)
{
hour = 1;
minstotal = hourtotal - MINS;
}
else
{
hour = 0;
minstotal = hourtotal;
}
/* CALCULATE AND DISPLAY 24 HOUR TIME */
time[5] = hour;
time[6] = (time[0] + time[2] + time[5]) * MAX ;
if(time[6] > (HOUR * MAX))
{
time[7] = (time[6] - (HOUR * MAX)) + minstotal;
printf("1 Day 24 hour time output = %d\n",time[7]);
}
else
{
time[7] = time[6] + minstotal;
printf("24 hour Time output = %d \n",time[7]);
}
return 0;
}
How do I write these programs in c I am a beginner learning c.
1.
write a program to read 2 inetegers with the following signficance the
first integer value represents a time of day on a 24hour clock, so that
1245 represents quater to one mid-day for eg. The second integer
represents a time duration in a similar way, so that 345 represents
three hours & 45 minutes. This duration is to be added to the first
time, and the result printed out in the same notation, in this case
1630 which is the time 3hours and 45 minutes after 1245.
Typical output might be start time is 1415. Duration is 50 End time is
1505.
Here is my answer to the above question i would like to know if it
is any good or crap constructive critisms accepted
I am just a novice who plays around with c
/* TIME2.C TO ADD TWO 24 HOUR INPUTS */
#include<stdio.h>
#define MAX 100
#define MINS 60
#define HOUR 24
int main(void)
{
int time24,mins,hour24,hourout;
int hourtotal,hour,minstotal;
char line[20];
int time[7]; /* ARRAY TO HOLD DIFFERENT VALUES */
/* SEPERATE HOURS FROM MINUTES */
do{
printf("Enter 24 hour start time: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%d",&time24);
mins = time24 % MAX;
hour24 = time24 - mins;
hourout = hour24 / MAX;
time[0] = hourout; /* hours start */
time[1] = mins; /* minutes start */
}while(time[0] >= HOUR || time[1] >= MINS);
do{
printf("Enter duration: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%d",&time24);
mins = time24 % MAX;
hour24 = time24 - mins;
hourout = hour24 / MAX;
time[2] = hourout; /* hours end */
time[3] = mins; /* minutes end */
}while(time[2] >= HOUR || time[3] >= MINS);
/* ADD START AND END MINUTES */
hourtotal = time[1] + time[3];
if(hourtotal >= MINS)
{
hour = 1;
minstotal = hourtotal - MINS;
}
else
{
hour = 0;
minstotal = hourtotal;
}
/* CALCULATE AND DISPLAY 24 HOUR TIME */
time[5] = hour;
time[6] = (time[0] + time[2] + time[5]) * MAX ;
if(time[6] > (HOUR * MAX))
{
time[7] = (time[6] - (HOUR * MAX)) + minstotal;
printf("1 Day 24 hour time output = %d\n",time[7]);
}
else
{
time[7] = time[6] + minstotal;
printf("24 hour Time output = %d \n",time[7]);
}
return 0;
}