diff --git a/lab2/game_of_life-serial.c b/lab2/game_of_life-serial.c index 692f6d970969f228601d6a052a741b7bcd4bd387..02c0cf900e35672b170b1bdc56890e7663e391cd 100644 --- a/lab2/game_of_life-serial.c +++ b/lab2/game_of_life-serial.c @@ -16,8 +16,8 @@ serial version #define NSTEPS 500 /* number of time steps */ -int main(int argc, char *argv[]) { - +int main(int argc, char *argv[]) +{ int i, j, n, im, ip, jm, jp, ni, nj, nsum, isum; int **old, **new; float x; @@ -34,15 +34,15 @@ int main(int argc, char *argv[]) { new[i] = malloc(nj*sizeof(int)); } -/* initialize elements of old to 0 or 1 */ + /* initialize elements of old to 0 or 1 */ for(i=1; i<=NI; i++){ for(j=1; j<=NJ; j++){ x = rand()/((float)RAND_MAX + 1); if(x<0.5){ - old[i][j] = 0; + old[i][j] = 0; } else { - old[i][j] = 1; + old[i][j] = 1; } } } @@ -72,28 +72,28 @@ int main(int argc, char *argv[]) { for(i=1; i<=NI; i++){ for(j=1; j<=NJ; j++){ - im = i-1; - ip = i+1; - jm = j-1; - jp = j+1; + im = i-1; + ip = i+1; + jm = j-1; + jp = j+1; - nsum = old[im][jp] + old[i][jp] + old[ip][jp] - + old[im][j ] + old[ip][j ] - + old[im][jm] + old[i][jm] + old[ip][jm]; + nsum = old[im][jp] + old[i][jp] + old[ip][jp] + + old[im][j ] + old[ip][j ] + + old[im][jm] + old[i][jm] + old[ip][jm]; - switch(nsum){ + switch(nsum){ - case 3: - new[i][j] = 1; - break; + case 3: + new[i][j] = 1; + break; - case 2: - new[i][j] = old[i][j]; - break; + case 2: + new[i][j] = old[i][j]; + break; - default: - new[i][j] = 0; - } + default: + new[i][j] = 0; + } } } @@ -101,7 +101,7 @@ int main(int argc, char *argv[]) { for(i=1; i<=NI; i++){ for(j=1; j<=NJ; j++){ - old[i][j] = new[i][j]; + old[i][j] = new[i][j]; } } } diff --git a/lab2/parallel_search-serial.c b/lab2/parallel_search-serial.c index 99635fa154929051702a7260d56f8f56dedcb496..ce08025f8a630a7c3d0eba2835b17f7c896a0df5 100644 --- a/lab2/parallel_search-serial.c +++ b/lab2/parallel_search-serial.c @@ -1,6 +1,7 @@ #include <stdio.h> -int main (int argc, char *argv[]) { +int main (int argc, char *argv[]) +{ const int N=300; int i,target; int b[N]; @@ -29,6 +30,5 @@ int main (int argc, char *argv[]) { } fclose(outfile); - return 0; + return 0; } - diff --git a/lab2/pi_serial.c b/lab2/pi_serial.c index b2f8c145fb95c566771418ecac756e289ebb5f3b..bf205725370bf45231b4d66c70826aa0df986a1c 100644 --- a/lab2/pi_serial.c +++ b/lab2/pi_serial.c @@ -7,30 +7,29 @@ double dboard (int darts); #define ROUNDS 10 /* number of times "darts" is iterated */ #define MASTER 0 /* task ID of master task */ -int main (int argc, char *argv[]) { - -double homepi, /* value of pi calculated by current task */ - pi, /* average of pi after "darts" is thrown */ - avepi, /* average pi value for all iterations */ - pirecv, /* pi received from worker */ - pisum; /* sum of workers pi values */ -int i, n; - - - srandom (0); - - avepi = 0; - for (i = 0; i < ROUNDS; i++) { - pi = dboard(DARTS); - - /* Master calculates the average value of pi over all iterations */ - avepi = ((avepi * i) + pi)/(i + 1); - printf(" After %8d throws, average value of pi = %10.8f\n", - (DARTS * (i + 1)),avepi); - - } - - return 0; +int main (int argc, char *argv[]) +{ + double homepi, /* value of pi calculated by current task */ + pi, /* average of pi after "darts" is thrown */ + avepi, /* average pi value for all iterations */ + pirecv, /* pi received from worker */ + pisum; /* sum of workers pi values */ + int i, n; + + srandom (0); + + avepi = 0; + for (i = 0; i < ROUNDS; i++) { + pi = dboard(DARTS); + + /* Master calculates the average value of pi over all iterations */ + avepi = ((avepi * i) + pi)/(i + 1); + printf(" After %8d throws, average value of pi = %10.8f\n", + (DARTS * (i + 1)),avepi); + + } + + return 0; } @@ -66,11 +65,11 @@ Explanation of constants and variables used in this function: #include <stdio.h> #include <stdlib.h> -#define sqr(x) ((x)*(x)) +#define sqr(x) ((x)*(x)) long random(void); double dboard(int darts) - { +{ double x_coord, y_coord, pi, r; int score, n; unsigned int cconst; /* must be 4-bytes in size */ @@ -82,12 +81,12 @@ double dboard(int darts) printf("Wrong data size for cconst variable in dboard routine!\n"); printf("See comments in source file. Quitting.\n"); exit(1); - } + } cconst = 2 << (31 - 1); score = 0; /* "throw darts at board" */ - for (n = 1; n <= darts; n++) { + for (n = 1; n <= darts; n++) { /* generate random numbers for x and y coordinates */ r = (double)random()/cconst; x_coord = (2.0 * r) - 1.0; @@ -96,10 +95,10 @@ double dboard(int darts) /* if dart lands in circle, increment score */ if ((sqr(x_coord) + sqr(y_coord)) <= 1.0) - score++; - } + score++; + } /* calculate pi */ pi = 4.0 * (double)score/(double)darts; return(pi); - } +}