Re: Another Tricky Problem I am Messing With (Not Homework)



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');
}
.



Relevant Pages

  • Variable
    ... How would I use a variable to select a time period as shown below. ... example below returns error: Incorrect syntax near '@Timeperiod' ... Am i missing something here? ... Declare @Timeperiod as int ...
    (microsoft.public.sqlserver.programming)
  • Re: BS on ult hist bk - extras "the best"
    ... at least we're beginning to dialogue. ... You keep missing my ... > qualifier that they were the "best 7" during that time period. ...
    (rec.sport.disc)
  • Re: Stuck on Earth
    ... You might also note that the Doc is trying to get Tegan home for the ... first four stories and keeps missing the time period. ...
    (rec.arts.drwho)