Re: Another Tricky Problem I am Messing With (Not Homework)
- From: Miguel Guedes <miguel.a.guedes@xxxxxxxxxxxxxxx>
- Date: Mon, 10 Sep 2007 19:53:15 +0100
joebenjamin wrote:
This is a problem I was trying to help a few friends figure out for fun. I
am not sure how to go about this, but there is something I am missing
here. Here is what we want the output to be:
Need to read in a time period from the keyboard (for example 1.2 seconds,
3.4 seconds, or 8.37 seconds). Once the time has been read in, we want to
print out the word “TICK” and then wait the designated time period and
print the word “TICK” again. Repeat until there are 6 “TICK”s on the
screen. It would be real neat if we knew how to variate between TICK and
TOCK, however this might be the output.
Had nothing to do so I decided to give it a shot... :)
Made the code as simpler as I could; hopefully you'll understand what the code
does, as I find it self-explanatory.
It's been awhile since I've coded in C (since the late 80's) and am not sure
whether I've used any non-standard C features. Hopefully some of the regulars
here can spot'em and correct'em if I have.
#include <stdio.h>
#include <conio.h>
#include <time.h>
int main( )
{
clock_t start, target, end;
float timer;
int i, j;
char msg[][5] = {"TICK", "TOCK"};
do
{
printf("\nEnter any time in seconds: ");
scanf ("%f", &timer);
i = j = 0;
printf( "Start timing\n" );
start = clock();
while(i++ < 6)
{
target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);
while(clock() < target);
printf("%s ", msg[j]);
if(++j > 1)
j = 0;
}
end = clock() - start;
printf( "\nTotal Time Elapsed : %0.3f seconds, %0.3f/iteration\n",
(float)end/1000, (float)end/((i-1)*1000));
printf("Another go? y/[n] ");
} while(getch() == 'y');
}
.
- Follow-Ups:
- Re: Another Tricky Problem I am Messing With (Not Homework)
- From: Bart van Ingen Schenau
- Re: Another Tricky Problem I am Messing With (Not Homework)
- References:
- Another Tricky Problem I am Messing With (Not Homework)
- From: joebenjamin
- Another Tricky Problem I am Messing With (Not Homework)
- Prev by Date: Re: MISRA-C:2010 (version 3)
- Next by Date: Re: can't replace define to const
- Previous by thread: Another Tricky Problem I am Messing With (Not Homework)
- Next by thread: Re: Another Tricky Problem I am Messing With (Not Homework)
- Index(es):
Relevant Pages
|