Jump to content

Incorrect program run time C

CJPowell27

Hey guys I tried to make my program keep track of how long it takes to completely finish, but when it is

run it says something stupid like the program only took .000594 seconds. Does anyone have any tips

as to why it is doing this? I'm also having trouble with srand not making it super random but I think I might

be able to find out how to do that. Any advice is much appreciated.

Spoiler

DpSwQdn.png?1

 

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

I fail to understand why you try to measure the time by CPU ticks. Care to elaborate?

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/10/2018 at 2:08 PM, fizzlesticks said:

Please post the code as text, not a screen shot. It makes it much easier to help.

 

On 1/11/2018 at 1:22 AM, Dat Guy said:

I fail to understand why you try to measure the time by CPU ticks. Care to elaborate?

Spoiler

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

char * phrase[] = {"the", "quick" ,"brown", "fox", "jumps", "over", "a", "lazy", "dog"};
int guess(char * phrase[]);

int main()
{
	clock_t time1;
	time1 = clock();
    clock_t begin= clock();	
    printf("This is a game that tests typing speed and accuracy");
	
    //Shuffles the words prior to asking for input
    for(int i = 0; i < 9; i++)
    {
        srand(time(0));
        int swapIndex = rand() % 9;
        char * temp = phrase[i];
        phrase[i] = phrase[swapIndex];
        phrase[swapIndex] = temp;
    }
    //The Program shuffles the initial phrase and then 
    //continues to traverse the phrase in order because it 
    //is already shuffled
    
    guess(phrase);

    clock_t end = clock();
	double timeTaken = (double)(end-begin)/CLOCKS_PER_SEC;
	printf("You took %f seconds to successfully type the phrase", timeTaken);
	return 0;

}

int guess(char * phrase[])
{

    for(int i = 0; i < 9; i++)
    {
        char guess[10];
        printf("\nType the word: %s\n", phrase[i]);
        scanf("%9s", guess);//Gets input
        while(strncmp(guess, phrase[i], 9) != 0)//Runs this loop if wrong
        {
            printf("\nTry again, the word was %s\n", phrase[i]);
            scanf("%9s",guess);
            strncmp(guess, phrase[i],9);
        }
    }


}

 

Here's the typed code.

I'm relatively new to C, what way would you recommend?

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Dat Guy said:

Using the actual time...?

Any documentation you'd recommend. Anything I find is using the Clocks per sec thing for some reason.

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, Dat Guy said:

Tried your C manual already?

Spoiler.

Wow that's much different than the book my professor has given says. I'll try something like what you sent and mark as solved if it works, thanks.

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×