473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

any good

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;
}

Nov 14 '05 #1
7 1265
On Sat, 21 Aug 2004 13:06:35 +0000 (UTC), Darklight
<ng********@netscape.net> wrote:
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;
A little white space between names would not hurt. In a larger
program, it would be better to put each in its own statement.
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;
The way integer arithmetic truncates you could combine these two
statements into hourout = time24/MAX.

If time24 is 1245:
In your code, hour24 becomes 1200 and hourout is 12.
In my code, hourout becomes 12 directly.
time[0] = hourout; /* hours start */
time[1] = mins; /* minutes start */
}while(time[0] >= HOUR || time[1] >= MINS);
This will repeat the loop if the user enters an invalid time but it
doesn't tell him what he did wrong. For example, some people use 2400
to indicate midnight and would have no idea why you don't accept it.

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];
hourtotal is a strange name for a calculation involving minutes.
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))
As a matter of style, indent the range of an if (which you do) but
don't indent the if itself. When you start writing larger programs,
this will make life much easier. {
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;
}


<<Remove the del for email>>
Nov 14 '05 #2
Barry Schwarz wrote:>>
mins = time24 % MAX;
hour24 = time24 - mins;
hourout = hour24 / MAX;
The way integer arithmetic truncates you could combine these two
statements into hourout = time24/MAX.

If time24 is 1245:
In your code, hour24 becomes 1200 and hourout is 12.
In my code, hourout becomes 12 directly.


Thanks for that
time[0] = hourout; /* hours start */
time[1] = mins; /* minutes start */
}while(time[0] >= HOUR || time[1] >= MINS);


This will repeat the loop if the user enters an invalid time but it
doesn't tell him what he did wrong. For example, some people use 2400
to indicate midnight and would have no idea why you don't accept it.

To print error message i have inserted a function here
and changed hourout to hours:

time[0] = hours; /* hours start */
time[1] = mins; /* minutes start */
ErrorPrint(hours,mins);
}while(time[0] >= HOUR || time[1] >= MINS);


The function is:
int ErrorPrint(int hour, int min)
{
if(hour >= HOUR || min >= MINS)
printf("Error incorrect value: Enter value less than 2400\n");
return(hour,min);
}

Nov 14 '05 #3
On Sat, 21 Aug 2004 18:48:10 +0000 (UTC), Darklight
<ng********@netscape.net> wrote:
Barry Schwarz wrote:>>
mins = time24 % MAX;
hour24 = time24 - mins;
hourout = hour24 / MAX;
The way integer arithmetic truncates you could combine these two
statements into hourout = time24/MAX.

If time24 is 1245:
In your code, hour24 becomes 1200 and hourout is 12.
In my code, hourout becomes 12 directly.


Thanks for that
time[0] = hourout; /* hours start */
time[1] = mins; /* minutes start */
}while(time[0] >= HOUR || time[1] >= MINS);


This will repeat the loop if the user enters an invalid time but it
doesn't tell him what he did wrong. For example, some people use 2400
to indicate midnight and would have no idea why you don't accept it.

To print error message i have inserted a function here
and changed hourout to hours:

time[0] = hours; /* hours start */
time[1] = mins; /* minutes start */
ErrorPrint(hours,mins);
}while(time[0] >= HOUR || time[1] >= MINS);


The function is:
int ErrorPrint(int hour, int min)
{
if(hour >= HOUR || min >= MINS)
printf("Error incorrect value: Enter value less than 2400\n");
return(hour,min);


What do you thing this does? Rest assured it doesn't.
}


<<Remove the del for email>>
Nov 14 '05 #4
Barry Schwarz wrote:>>>> }while(time[0] >= HOUR || time[1] >= MINS);

The function is:
int ErrorPrint(int hour, int min)
{
if(hour >= HOUR || min >= MINS)
printf("Error incorrect value: Enter value less than 2400\n");
return(hour,min);


What do you thing this does? Rest assured it doesn't.


It does do what it's meant to, not unless you see something i don't

Nov 14 '05 #5
Darklight <ng********@netscape.net> wrote:
Barry Schwarz wrote:>>>> }while(time[0] >= HOUR || time[1] >= MINS);

The function is:
int ErrorPrint(int hour, int min)
{
if(hour >= HOUR || min >= MINS)
printf("Error incorrect value: Enter value less than 2400\n");
return(hour,min);
What do you thing this does? Rest assured it doesn't.

It does do what it's meant to, not unless you see something i don't


You can't return two values, i.e.

return(hour,min);

will just return min, not hours. 'return' is not a function, even if
you put everything after it in (superfluous) parentheses, so the
comma here acts as the comma operator - and the comma operator makes
the program discard hour and return min.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #6
Je***********@physik.fu-berlin.de wrote:
Darklight <ng********@netscape.net> wrote:
Barry Schwarz wrote:>>>> }while(time[0] >= HOUR || time[1] >= MINS);

The function is:
int ErrorPrint(int hour, int min)
{
if(hour >= HOUR || min >= MINS)
printf("Error incorrect value: Enter value less than 2400\n");
return(hour,min);

What do you thing this does? Rest assured it doesn't.

It does do what it's meant to, not unless you see something i don't


You can't return two values, i.e.

return(hour,min);

will just return min, not hours. 'return' is not a function, even if
you put everything after it in (superfluous) parentheses, so the
comma here acts as the comma operator - and the comma operator makes
the program discard hour and return min.

Regards, Jens


so would it be better to change the function to void
i just done that and it works, is that the right thing to do
thanks for your comments

Nov 14 '05 #7
Darklight <ng********@netscape.net> wrote:

To print error message i have inserted a function here
and changed hourout to hours:

time[0] = hours; /* hours start */
time[1] = mins; /* minutes start */
ErrorPrint(hours,mins);
}while(time[0] >= HOUR || time[1] >= MINS);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The function is:
int ErrorPrint(int hour, int min)
{
if(hour >= HOUR || min >= MINS) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ printf("Error incorrect value: Enter value less than 2400\n");
return(hour,min);
}


You have "code duplication" here which is generally a bad idea; you
could make the error function return a value to indicate whether there
was an error, eg:

do ... while (error_check(hours, mins));

int error_check(int hours, int mins)
{
if(hours < HOUR && mins < MINS)
return 0;

printf("Error incorrect value: Enter value less than 2400\n");
return 1;
}

Also, there's no point in assigning time[0] and time[1] until the
loop has actually exited (because if the loop goes around again
then their values were wasted).
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: KN | last post by:
I know both are pretty much the same and it comes down to personal choice. But I have to make the choice for the team. Things so far that I am considering 1. XML documentation in C# -- thats...
29
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
59
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
17
by: Brett | last post by:
I'd like references on where to find some good (quality) icons to use for form and application icons (for the EXE). Most of the free stuff isn't that great looking and there isn't a good...
15
by: Alex L Pavluck | last post by:
I am new to programming other than SAS. I read that C# is a good starting language and I have started to create some simple programs with C# 2005 express edition. Can someone let me know if this...
6
by: Jamiil | last post by:
I am not a programmer by any means, but a dedicated aficionado. I have good understanding of Java and C/C++, and now I would like to learn javascript->ajax, but I don't know where to start. My HTML...
30
by: mistral | last post by:
Neeed good javascript unescape encoder, to protect javascript code. Some advices? Online tool, or ready javascript only. Any opinions about the Javascript Obfuscator:...
244
by: Ajinkya | last post by:
Can anyone suggest me a good compiler for(c/cpp) for windows? I tried dev cpp but its debugging facility is very poor.
76
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.