0% found this document useful (0 votes)
8 views13 pages

Student Name Student Registration Number Class &section: AIML Study Level: UG/PG Year &term: Subject Name Name of The Assessment Date of Submission

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Student Name Student Registration Number Class &section: AIML Study Level: UG/PG Year &term: Subject Name Name of The Assessment Date of Submission

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Student Name Pavan Lal

Student Registration 241U1R2070 Class &Section: AIML-1C


Number
Study Level : UG/PG UG Year &Term: 1st&2nd
Subject Name C - Programming

Name of the Assessment LAB REPORT

Date of Submission 9-1-2025

1)
AIM:- TO Write a C program to find the minimum, maximum and average in an array of integers

Algorithm

1. Start : The program begins.


2. Input Number of Elements (n): Prompt the user to enter the number of elements.
3. Initialize Array: Declare an array of size `n`.
4. Input Elements: Use a loop to input `n` integers into the array.
- Inside the loop:
- Add to Sum: Each integer is added to `sum`.
- First Element Check: If it's the first element, initialize `min` and `max`.
- Update Min/Max: For subsequent elements, update `min` and `max` if necessary.
5. Calculate Average: Compute the average by dividing `sum` by `n`.
6. Output Min, Max, Average: Print the results.
7. End: The program concludes.
Flowchart

[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

// Input number of elements


PRINT "Enter number of elements:"
READ n

// Initialize array of size n


DECLARE arr[n] AS INTEGER

// Input elements into the array


PRINT "Enter n integers:"
FOR i FROM 0 TO n-1 DO
READ arr[i] sum
= sum + arr[i]

IF i == 0 THEN
min = arr[i] max
= arr[i]
ELSE
IF arr[i] < min THEN
min = arr[i]
ENDIF

IF arr[i] > max THEN


max = arr[i]
ENDIF
ENDIF
END FOR

// 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;

// Ask the user for the number of elements


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

// Declare the array


int arr[n];

// Input array elements


printf("Enter %d integers:\n", n); for
(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Initialize min, max, and sum


min = arr[0];
max = arr[0];
sum = arr[0];

// Find min, max, and calculate sum


for (i = 1; i < n; i++) { if (arr[i] <
min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
sum += arr[i];
}
// Calculate average
avg = sum / n;

// Display results printf("Minimum


value: %d\n", min); printf("Maximum
value: %d\n", max); printf("Average
value: %.2f\n", avg); return 0;
}

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 |
+----------------+

Matrix Multiplication Flowchart

+----------------+
| 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

Matrix Addition 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

Matrix Multiplication Pseudocode

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 matrixAddition(int rows, int cols, int mat1[rows][cols], int


mat2[rows][cols], int result[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}

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;

// Example int mat1[2][2] =


{{1, 2}, {3, 4}}; int mat2[2][2] =
{{5, 6}, {7, 8}}; int
resultAdd[2][2]; int
resultMul[2][2];

// Perform matrix addition

matrixAddition(rows1, cols1, mat1, mat2, resultAdd);

printf("Matrix Addition Result:\n");


for (int i = 0; i < rows1; i++) { for
(int j = 0; j < cols1; j++) {
printf("%d ", resultAdd[i][j]);
}
printf("\n");
}

// Perform matrix multiplication


matrixMultiplication(rows1, cols1, mat1, rows2, cols2, mat2, resultMul);

printf("Matrix Multiplication Result:\n");


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d ", resultMul[i][j]);
}
printf("\n");
}

return 0;
}

RESULT

You might also like