SE250:lab-1:apra102

From Marks Wiki
Revision as of 05:18, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (2 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This lab helped me to recollect all the pervious stuff which I did in 1st year. Initially when I looked at the lab task, I found it easy to figure how to code for addition. But later coming to the time taken to xecute millions of addition was quite confusing. Later with the help, when the tutor explained me the whole thing what I need to do I figured out the solution. I used for loop to find the values of time. In the final code I noticed that it has three main things: code for addition, ‘for’ loop and a clock function. Finally I wrote a code to get 10 values of the time for the loop to execute million additions so that I can calculate the average time taken by adding the 10 values and dividing it by 10. When I consider the output, 10 values of time were approximately same. I even checked for long, int and double, all the values were approximately same. Also the value of time is different in different computer, which is strange. Mistake I made was I didn't use curly brackets in an appropriate place. The main problem I faced was how to use this clock function and learnt this in this session. I sought out with the clock function with the help of tutor. The main thing I came to know by this lab is I need more practice so that I can try to figure this kind of problems in another way next time. My code is:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <time.h>

int main(void)

{

       int b = 0;
       int i, j;
       int addnum = 10000000;
       clock_t t0 = clock();
               
       for (j=0; j < 10; j++ ){
               clock_t t0 = clock();
               for (i=1; i < addnum; i++ ){
                    b++;        
          }
          printf("%ld\n", clock() - t0);
       }
       return 0;
       

}