Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Could you optimized this program......

Status
Not open for further replies.

nhongz

Vendor
Nov 13, 2003
2
TH
I am begining with C and have exercise with a program that will display the duration in hours and minutes of the journey (assumed to take place all in one day) so i write this:

#include<stdio.h>
void main(void)
{
int DptTime,ArrTime,hours,mins,Dmins,Amins;
float DHours,AHours;
printf(&quot;Type in Departure Time:&quot;);scanf(&quot;%i&quot;,&DptTime);
printf(&quot;Type in Arrival Time:&quot;);scanf(&quot;%i&quot;,&ArrTime);
hours=DptTime/100; mins=DptTime%100;
DHours=hours+mins/60.0;
Dmins=DHours*60+0.5;
hours=ArrTime/100; mins=ArrTime%100;
AHours=hours+mins/60.0;
Amins=AHours*60+0.5;
mins=Amins-Dmins;
hours=mins/60;
mins=mins%60;
printf(&quot;%i:%i\n&quot;,hours,mins);
}


--------------------------------------------------------------------------------

it's work but could i find another code to write this program
 
Well can you be a bit more specific in terms of the optimization. i.e. what kind of optimizations are you expecting.??

Well ont thing you can do is:

printf(&quot;Enter the Departure Time HH:MM\n&quot;);
scanf(&quot;%2lu:%2lu&quot;, &Hours, &Min);

and similarly for the Departure time.

Well this will save you from all the processing of converting from 1620 to 4:20 PM etc..

Hope this helps
 
> void main(void)
main returns an int - never void

Code:
int main ( ) {
    /* your code here */
    return 0; /* program completed successfully */
}

There is very little you can do to improve the run-time performance of this. There are no loops or complex algorithms. The vast majority of the time (which itself is pretty small) will be spent inside the printf and scanf functions.

Apart from that, you could &quot;optimise&quot; the code by making it easier to read, say by adding a couple of blank lines to separate input, calculation and output, and perhaps a couple of comments.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top