how to use the clock() function?

This is a discussion on how to use the clock() function? within the C Programming forums, part of the General Programming Boards category; hey i got to write a program which stores randoms numbers in an array use insertion sort and selection sort ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    56

    how to use the clock() function?

    hey i got to write a program which stores randoms numbers in an array use insertion sort and selection sort and time both of them witht the lcock fnction to see which is faster.how do i use the clock function?? where do i put it?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    void print(int[]);
    int random(int,int);
    void insertionsort(int[]);
    void selectionSort(int[]);
    int main(){
        int i,arr1[10000],arr2[10000];
        srand(time(0));
        for(i=0;i<10000;i++){
                             arr1[i]=random(1,1000000);
                             arr2[i]=random(1,1000000);
                             }
                             
        
        insertionsort(arr1);
        print(arr1);
        selectionSort(arr2);
        print(arr2);
        system("pause");
        return 0;
    }
    int random(int m,int n){
        int offset =rand()/(RAND_MAX + 1.0)*(n-m+1);
        return m+offset;
    }
    
    void insertionsort(int arr[]){
         int j,k,key;
         for(j=0;j<10000;j++){
                              key=arr[j];
                              k=j-1;
                              
         while(k>=0 && key<arr[k]){
                    arr[k+1]=arr[k];
                    --k;
                    }
                    arr[k+1]=key;
                    }
                    }
    
    void selectionSort(int numbers[])
    {
        int array_size=10000;
      int i, j;
      int min, temp;
    
      for (i = 0; i < array_size-1; i++)
      {
        min = i;
        for (j = i+1; j < array_size; j++)
        {
          if (numbers[j] < numbers[min])
            min = j;
        }
        temp = numbers[i];
        numbers[i] = numbers[min];
        numbers[min] = temp;
      }
    }
    
     
    
    
    
    void print(int arr[]){
         int j;
         for(j=0;j<10000;j++){
                              printf("%d  ",arr[j]);
                              }
                              }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include <clock.h>
    ....
    
    clock_t t;
    ...
    t = clock();
    // do something that takes a while here. 
    t = clock() - t;  // calculate difference from start to now. 
    ...
    printf("%f", (double) t / CLOCKS_PER_SEC);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Ran into a problem with clock the way you have it in that if you need to do some nul cycles using it's possible that clock will not increment. In which case you need to use time in which case you can't get tenths of a second. It was in my Go-Cart program that I ran into that problem.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 11:08 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21