SE250:lab-1:rata003

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

At first I tried assigning values to ‘a & b’ (variable names I initially used to address two values to be calculated) and then using another variable called ‘c’ to get the result. At that point I didn't know how to relate the clock function to this whole operation.

Then I tried using a while loop, declaring a value for ‘a’, and incrementing the value ‘b’. This operation was happening inside a loop, so there were countless outcomes till the condition was met.

I didn’t really know the purpose of using the clock function, so I asked for help and found out that I have to use the command line clock_t, and that clock() means the time at the moment.

Then I corrected the code, so that a while loop was used to calculate the time difference, but I still didn't know how the variable ‘c’ (which gave the final result of adding two no.s) relate to the whole loop.

So after consulting the tutor it was clear that the adding operation is already happening inside the loop, so I didn't have to separately do the adding operation elsewhere.

Then I tried using only one variable, but then when I increment that variable I had trouble with the type. As I was supposed to test it with different types, the incrementing was also changing as those types. But the no. was always meant to increase only by 1, because [c < max_num] the max_num is a int type so ‘c’ has to be of the same type for this loop to work. To address this issue I introduce another variable, so that it didn't affect by the types changing for testing. At this point I had to change from using a ‘while’ loop to a ‘for’ loop, because of the above problem.

After finishing the code, i had troubles debugging it. After a while it was apparent that it was because of the max_no i used in the condition of the for loop [for (c = 0; c <max_num; c++)] was too small for the final outcome to be practical.

Final code

  1. define _CRT_SECURE_NO_DEPRECATE
  1. include <stdio.h>
  2. include <time.h>

int main(void) { int c; int a = 0; clock_t t0;

long max_num = 10000000000;

t0 = clock( );

for (c = 0; c < max_num; c++){	

a++;

}
 printf("%ld\n", clock() - t0 );
 return 0;

}