Assignment # 1 Solution



A note of warning: Start work on assignments as soon as they are given. Do not underestimate the demanding nature of this course. Expect the system to crash the night before your program is due. Aim to have it done the day before.

Submit the assignment on slate. Do not email me assignments after due date. It will not be accepted in any case. Students are required to submit actual content written in MS word or Pdf. Hand written/ Scanned assignments will not be accepted.

Compiling and Visualizing Results for Approximating Value of Pi

Your objective will be to run the following C-code that approximates the value of Pi.

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

int main()


int STEPS = 100; // Number of Steps int i, count = 0; double x, y, z, pi;

for (i = 0; i <= STEPS; i++)


  1. = rand()/(double)RAND_MAX;

  1. = rand()/(double)RAND_MAX;

  1. = x*x + y*y;

if (z <= 1)



pi = (double)count/STEPS*4;

printf(“N = %d\t”, STEPS);

printf(“Pi = %.20f\n”, pi);


Your objective would be to:

  1. Modify the code above so that the program is able to (a) obtain the “STEPS” variable from the shell, and (2) show the overall execution time of the program.

  1. Prepare a shell script that runs the above program a number of times (for increasing sizes of STEPS).

The output from the shell script should be in the following columns: [N], [Time], [Absolute Error], [Absolute Relative Error]

The first two columns (N, Time) are reported from within the program.

The last two columns should be computed using AWK as:

[Absolute Error] = | pi – acos(-1) |

[Absolute Relative Error] = | pi – acos(-1) | / | acos(-1) |

The above output should be passed by the shell script to GNUPLOT utility for plotting. two plots

  • Plot Execution Time (y-axis) vs Steps (X-axis)

  • Plot Absolute and Relative Error (y-axis) vs Steps (X-axis)

(Warning: May exhaust your available RAM, so consider choosing STEPS sizes of multiples of 10, 100, 1000, etc.):

Deliverable: The name of the shell-script should be your roll-number (e.g. Submit the shell script file to me on slate.

Best of Luck

error: Content is protected !!