SE250:lab-1:sbha077

From Marks Wiki
Jump to navigation Jump to search

Lab 1 Report

What was done

Not having done C for a few months I decided to use my my old Enggen 131 book and with the help of my mates the net, I finally managed to figure out what had to be done and what codes to use properly. Then, the clock() function. We've never used that before (atleast I haven't anyway) - used GOOGLE for it and figured out how to use that function. In the end I faced a couple of problems that needed addressing but were debugged eventually.

I also asked for a little bit of help from the lecturer as to why I was getting 0.000000 as my result. He suggested using a larger number instead as the pcs are really quick in doing the intended calculations.

Problems faced

1. I had made all the variables used in the code "local". That resulted errors of "uninitialised local variable x" (etc.)

Wrong way:

#include <stdio.h>
#include <time.h>
clock_t start, stop;
int main ()
{
int x;
double z;
float d;
double elapsed;

Right way:

#include <stdio.h>
#include <time.h>
int x;
double z;
float d;
clock_t start, stop;
double elapsed;
int main ()
{

2. I forgot to add the "/ CLOCKS_PER_SEC" bit during the total time calculation part which resulted in me not getting an answer in the first place.

  elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;

3. I used the wrong syntax for the FOR loop

Wrong way:

int x;
x = 0;
     for x < 10;
     /* code */

Right way:

for (x=0; x<5000; x++);

Code

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

int x;
double z;
float d;
clock_t start, stop;
double elapsed;

int main ()

{

start = clock();
for (x=0; (x<100000000); x++); {
z=z+z;
}

stop = clock();
elapsed = ((double) (stop - start)) / CLOCKS_PER_SE
printf("\nThe time taken for addition = %lf seconds\n",elapsed);

start = clock();
for (x=0; (x<100000000); x++); {
d=d+d;
}

stop = clock();
elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;
printf("\nThe time taken for addition = %lf seconds\n",elapsed);
return 0;
}

Results

The values for x < 100000000:

The time taken for addition = 0.375000 seconds

The values for x < 1000000000:

The time taken for addition = 3.843000 seconds

Suggestions

1. It's a good idea to GOOGLE things out that you are not clear about.

2. Use other programs like Visual Studio / Visual Studio Command Prompt to check your results and debug your program (its a bit easier).

3. Never hestitate to ask anyone for help!