Mathematical Foundation of Computer Science

(Chris Devlin) #1
DHARM

54 MATHEMATICAL FOUNDATION OF COMPUTER SCIENCE


(ii) //function of summation of n numbers using recursion
int Sum2(int A[ ], int n)
{
if(n > 0)
return Sum2(A,n - 1) + A[n-1];
return 0;
}
(iii) // a function that adds two matrices A[0,n-1][0,m-1]
and B[0,n-1][0,m-1] and result stored in the same size matrix C.
Void Add(int **A, int **B, int **C, int n, int m)
{
for (int I = 0; I < n; I ++)
for (int J = 0; J < m; J ++)
C[I][J] = A[I][J]+B[I][J]
}
(iv) //a function that multiplies two matrices A[0,n-1] [0,n-1]
and B[0,n-1][0,n-1] and result stored in the same size matrix C.
Void Multiply(int **A, int **B, int **C, int n)
{
for (int I = 0; I < n; I ++)
for (int J = 0; J < n; J ++)
{
int sum = 0;
for (int K = 0; K < n; K ++)
sum = sum + A[I][K]+B[K][J]
C[I][J] = sum;
}
}
(v) //a function to evaluate the polynomial of degree n i.e.,

p(x) = i0Σ

n
=ai x

n

int Poly1(int a[],int n, const &x)
{
int T = 1, val = a[0];
for (int I = 1; I = n; I++)
{
T = T * x;
val = val + T * a[I];
}
return val;
}
Free download pdf