Data Structure (2130702)

BE | Semester-3   Summer-2017 | 05/31/2017

Q2) (a)

Write an algorithm for finding average of given numbers. Calculate time complexity.

AverageOfNumverst(A,n)
{

   average = 0
   total = 0
   for i = 0 to n-1
      total = total + A[i]
   average = total / n
   return average
}
Here, A is array, n is no of elements in array
Total Time Required by Algorithm = 1 + 1 + 2 (n+1) + 2n + 1 + 1
              = 6 + 4n = O(n)
Time complexity of finding average of given numbers is O(n)