Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Using C language Generate two random numbers (x_0 and x_1) between 0.0 and 100.

ID: 3877538 • Letter: #

Question

*Using C language

Generate two random numbers (x_0 and x_1) between 0.0 and 100.0 and compute their difference that is always positive. Repeat this for N times and find the average and the standard deviation of the differences. You'd better use the function rx_range(). Are there any relationship between the average (deviation) and the number of repetition N? Your program will read your input N from keyboard and use a while-loop statement for the repetition. Draw a chart showing the relationship between the number of iterations vs. average and standard deviation of the difference.

Explanation / Answer

C code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int abs(int n)
{
   if(n >= 0)
   {
      return n;
   }
   else
   {
      return -n;
   }
}

int main()
{
   int i, N;
   time_t t;
   srand((unsigned) time(&t));
   printf("Enter the value of N ");
   scanf("%d",&N);
   int array[N];
   int diff_sum = 0;
   for (int i = 0; i < N; ++i)
   {
         int x1 = rand() % 101;
         int x2 = rand() % 101;
         int diff = abs(x1 - x2);
         array[i] = diff;
         diff_sum = diff_sum + diff;
   }
   float average = (diff_sum*1.0)/(N*1.0);
   float std = 0;
   for (int i = 0; i < N; ++i)
   {
      std = std + ( (array[i])*1.0 - average )*( (array[i])*1.0 - average );
   }
   std = sqrt(std/N);

   printf("Average = %f STD = %f ", average,std);

   return(0);
}

Sample Output:

Enter the value of N 5
Average = 37.799999 STD = 23.335808

Table:

N = 5 Average = 37.799999 STD = 23.335808

N = 10 Average = 40.799999 STD = 25.285568

N = 25 Average = 39.480000 STD = 24.101650

N = 50 Average = 30.959999 STD = 19.931847

N = 100 Average = 32.009998 STD = 24.164642

N = 500 Average = 31.612000 STD = 23.005863

N = 1000 Average = 33.951000 STD = 23.302158

N = 5000 Average = 33.531799 STD = 23.763489