SE250:lab-1:sbas046

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

SOFTENG 250 – LAB 1 4500567


I started by reading the Task Sheet at which point I realized I didn’t remember anything about C. I started Visual Studios. I had problems figuring out how to start a project in Visual Studios and finally got it through trial and error. I then decided that Visual Basics was too complicated and decided to try use emacs. After loading emacs however I promptly reassessed my hasty decision and and went back to using Visual Studio. It took me a while to get started as recalling even the basic commands seemed beyond my ability. I finally wrote something that resembled a for-loop with an addition inside and two clock functions before and after it. This was done with significant use of the Visual Studios help files as well as the internet. The first time I compiled and ran it I got some minor syntax errors which I fixed. When it did run the answer that came up was 0. I figured out that this was because I was printing the time taken for each addition and the long variable does not support decimals. I concluded that the way around it was to just print the time taken for a large number of calculations. My program at this point looked like this:

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

int main(void)
{
	long t, t1, tfinal;
	int i, number;

	number = 100000000;
	t = clock();

	for (i=0; i<number; i++)
	{
	}

	t1= clock();
	tfinal = ((t1 - t));
	printf ("The time taken %d for addition is %ld clicks\n", number, tfinal);
	return 0;
}

This was 1 and a half hours of work. I was very proud.


The next was to modify the program so that it calculated the time taken for the calculation excluding the time taken for the loop. I did this my performing two loops and timing both. One of the loops included an addition calculation. I then subtracted the time taken for only the loop from the time for loop and the addition. This gave me a time for addition independent of the time taken for the loop. My final program was as follows:

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

int main(void)
{
	long t, t1, t2, t3, tfinal;
	int i, number;

	number = 100000000;


	t = clock();
	for (i=0; i<number; i++)
	{
		i++;
	}
	t1= clock();

	t2 = clock();
	for (i=0; i<number; i++)
	{
	}
	t3= clock();

	tfinal = ((t1 - t)) - ((t2 - t3)) ;
	printf ("The time taken for %d addition calculations is %ld\n", number, tfinal);
	return 0;
}



I concluded from this lab task that I need to recap on what I learnt last year.