Sie sind auf Seite 1von 5

Name: Enayatullah Atal

ID: K1S20MCS0003 Date: 4/12/2020


Class: 1st Semester of MCS-BU Subj: Advanced Algorithm D&A

Assignment 02:

 Write a pseudo code which find the sum of two 3*3 matrices and then calculate its running
time.
 Write a pseudo code which find the Multiplication of two 3*3 matrices and then calculate
its running time.
 Write a pseudo code which read a number N and print whether it is prime or not. After that,
calculate the run time complexity.
 Write an algorithm using dynamic array to store the result of student and display the result
than delete the array.

1. Write a pseudo code which find the sum of two 3*3 matrices and then calculate its running
time.

We will input the number of row and Column from keyboard and also input the elements of each
row and Column for two matrices.
Sum of two matrices
Step 1: start and also write the time function to calculate the running time
double time_spent =0.0
clock_t begin =clock ()
Step 2: assign four inter variable and three array
Int R, C, B, d, first [3][3], second [3][3], sum [3][3];
Step 3: read the number Row and Column
Read (R, C)
Step 4: read the number of elements for first matrix and used for loop.
for (B = 0; B < R; c++)
for (d = 0; d < C; d++)
read (first[B][d])
Step 5: read the number of second matrix and used for loop.
for (B = 0; B < R; c++)
for (d = 0; d < C; d++)
read (second[B][d])
Step 6: calculate the sum of two matrices
for (B = 0; B < R; c++)
for (d = 0; d < C; d++)
sum[B][d] = first[B][d] + second[B][d]
print (sum[B][d])

Step 7: end the time of running and show the elapsed time in seconds.
clock_t end=clock ()
time_spent +=(double) (end - begin)/ CLOCKS_PER_SEC
print (time spent)

Step 8: [finish]
Exit

2. Write a pseudo code which find the Multiplication of two 3*3 matrices and then calculate
its running time.

Multiplication of two matrices


Step 1: start and also write the time function to calculate the running time
double time_spent =0.0
clock_t begin =clock ()
Step 2: Assigned eight integer variable and three array
int m, n, p, q, c, d, k, sum = 0
int first [3] [3], second [3] [3], multiply [3] [3]
Step 3: read the number of Row and Column for first matrix
Read (m, n)
Step 4: read the number of elements (row and column) for first matrix and used for loop
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
Read (first[c][d])
Step 5: read the number of Row and Column for second matrix.
Read (p, q)
Step 6: used condition, if the number of row in first matrix is not equal to the number of column in
second matrix, multiplication is not possible. Else read the number of elements for second matrix.
If (n is not equal to p)
Print (The multiplication isn't possible)
Else
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
Read (second[c][d]);
for (c = 0; c < m; c++)
for (d = 0; d < q; d++)
for (k = 0; k < p; k++)
sum = sum + first[c][k] *second[k][d]
multiply[c][d] = sum
sum = 0
Step 7: product of matrices
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
print (multiply[c][d])
Step 8: end the time of running and show the elapsed time in seconds.
clock_t end=clock ()
time_spent +=(double) (end - begin)/ CLOCKS_PER_SEC
print (time spent)

Step 9: [finish]
Exit
3. Write a pseudo code which read a number N and print whether it is prime or not. After that,
calculate the run time complexity.

Find prime number

Step 1: start and also write the time function to calculate the running time
double time_spent =0.0
clock_t begin =clock ()

Int n, i, f=0

Step 2: read numbers


Read (n)
Step 3: used for loop and if conditions statement to check the number, is prime or not.
for (i=2; i<=n/2; i++)
if (n divided on i and remain 0)
print (Number is not prime)
f=true
break
if (f is false)
print (number is prime)
Step 4: end the time of running and show the elapsed time in seconds.
clock_t end=clock ()
time_spent +=(double) (end - begin)/ CLOCKS_PER_SEC
print (time spent)

Step 5: [finish]
Exit
4. Write an algorithm using dynamic array to store the result of student and display the result
than delete the array.

Store the results and display the results


Step 1: Start
Step 2: used Struct functions to student name, marks and roll number.
Struct student
Char name[size]
Int rollno, float marks
Step 3: read the number of record and call the Struct function.
Read (N of record)
Struct student stuarr[N]
Step 4: read the name, roll number and marks of student and record them.
for (i=0; i<n; i++)
print (record number = i+1);
Read (stuarr[i].name)
Read (stuarr[i]. rollno)
Read (stuarr[i]. marks)

Step 5: display the students recorded.


for (i=0; i<n; i++)
print (stuarr[i].name, stuarr[i]. rollno, stuarr[i]. marks)
Step 6: [finish]
Exit

Note: I tried hard to write deletion parts as well but I couldn’t. I hope to guide me in this section.

Das könnte Ihnen auch gefallen