Student Name Student Registration Number Class §ion: AIML Study Level: UG/PG Year &term: Subject Name Name of The Assessment Date of Submission
Student Name Student Registration Number Class §ion: AIML Study Level: UG/PG Year &term: Subject Name Name of The Assessment Date of Submission
1)
AIM:- TO Write a C program to find the minimum, maximum and average in an array of integers
Algorithm
[Start]
|
v
[Input n]
|
v
[Initialize arr[n]]
|
v
[For i = 0 to n-1]
|
v
[Input arr[i]]
|
v
[sum += arr[i]]
|
v
[Is i == 0?] -- Yes --> [min = max = arr[i]]
|
No
| v
[Is arr[i] < min?] -- Yes --> [min = arr[i]]
| |
No No
| | v
v
[Is arr[i] > max?] -- Yes --> [max = arr[i]] -- No --> [Continue Loop]
|
No
| v
[Continue Loop]
|
v
[Calculate average]
|
v
[Output min, max, average]
|
v
[End] Pseudo
Code
plaintext
START
// Declare variables
DECLARE n, i, min, max AS INTEGER
DECLARE sum AS FLOAT = 0.0
IF i == 0 THEN
min = arr[i] max
= arr[i]
ELSE
IF arr[i] < min THEN
min = arr[i]
ENDIF
// Calculate average
average = sum / n
// Output results
PRINT "Min:", min, "Max:", max, "Average:", average
END
C Program
#include <stdio.h>
int main() {
int n, i;
int min, max;
float sum = 0, avg;
RESULT
2)
AIM :-TO Write a C program to perform addition and multiplication of two matrices
Algorithm
1. Input: Two matrices mat1 (size rows1 x cols1) and mat2 (size rows2 x cols2), w
2. here cols1 must equal rows2.
3. Output: A resultant matrix result of size rows1 x cols2.
4. Initialize: Loop through each row i from 0 to rows1 - 1.
5. Loop through Columns: For each row, loop through each column j from 0 to cols2 - 1.
6. Initialize Result Element: Set result[i][j] = 0.
7. Multiply and Accumulate: Loop through each element index k from 0 to cols1 - 1, and
update:
• result[i][j] += mat1[i][k] * mat2[k][j]
8. End: After completing the loops, the resultant matrix is ready.
Matrix Addition Flowchart
+----------------+
| Start |
+----------------+
|
v
+----------------+
| Initialize |
| matrices |
+----------------+
|
v
+----------------+
| For each row |
+----------------+
|
v
+----------------+
| For each col |
+----------------+
|
v
+----------------+
| result[i][j] = |
| mat1[i][j] + |
| mat2[i][j] |
+----------------+
|
v
+----------------+
| Output result |
+----------------+
|
v +---------------
-+
| End |
+----------------+
+----------------+
| Start |
+----------------+
|
v
+----------------+
| Initialize |
| matrices |
+----------------+
|
v
+----------------+
| For each row |
+----------------+
|
v
+--------------------+
| For each column |
+--------------------+
|
v
+--------------------+
| result[i][j] = 0 |
+--------------------+
|
v
+--------------------+
| For each k |
+--------------------+
|
v
+--------------------+
| result[i][j] += |
| mat1[i][k] * mat2[k][j]|
+--------------------+
|
v
+--------------------+
| Output result |
+--------------------+
|
v +-----
-----------+
| End |
+----------------+
Pseudocode
plaintext
FUNCTION matrixAddition(rows, cols, mat1, mat2, result)
FOR i FROM 0 TO rows - 1 DO
FOR j FROM 0 TO cols - 1 DO
result[i][j] = mat1[i][j] + mat2[i][j]
END FOR
END FOR
END FUNCTION
plaintext
FUNCTION matrixMultiplication(rows1, cols1, mat1, rows2, cols2, mat2, result)
FOR i FROM 0 TO rows1 - 1 DO
FOR j FROM 0 TO cols2 - 1 DO
result[i][j] = 0
FOR k FROM 0 TO cols1 - 1 DO
result[i][j] += mat1[i][k] * mat2[k][j]
END FOR
END FOR
END FOR
END FUNCTION
PROGRAM
#include <stdio.h>
void matrixMultiplication(int rows1, int cols1, int mat1[rows1][cols1], int rows2, int cols2, int
mat2[rows2][cols2], int result[rows1][cols2]) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
int main() {
int rows1, cols1, rows2, cols2;
// Example dimensions
rows1 = 2;
cols1 = 2; rows2
= 2;
cols2 = 2;
return 0;
}
RESULT