LAB REPORT
Course Code: CSE 1202
Course Title: Structured Programming
Submitted By:
Name: Nusrat Jahan Khushbu
Student Id: 242014025
Department: CSE
1. Count the Total Number of Duplicate Elements
#include <stdio.h>
int main() {
int n, i, j, count = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[100];
printf("Enter the elements of the array:\\\\n");
for (i = 0; i < n; i++) {
printf("Element %d: ", i);
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
break; // To avoid counting the same duplicate again
}
}
}
printf("Total duplicate elements: %d\\\\n", count);
return 0;
}
2. Find Maximum and Minimum Elements
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[100];
printf("Enter the elements of the array:\\\\n");
for (i = 0; i < n; i++) {
printf("Element %d: ", i);
scanf("%d", &arr[i]);
}
int max = arr[0];
int min = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
printf("Maximum: %d\\\\n", max);
printf("Minimum: %d\\\\n", min);
return 0;
}
3. Separate Odd and Even Integers
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[100], even[100], odd[100];
int e = 0, o = 0;
printf("Enter the elements of the array:\\\\n");
for (i = 0; i < n; i++) {
printf("Element %d: ", i);
scanf("%d", &arr[i]);
if (arr[i] % 2 == 0) {
even[e] = arr[i];
e++;
} else {
odd[o] = arr[i];
o++;
}
}
printf("Even numbers: ");
for (i = 0; i < e; i++) {
printf("%d ", even[i]);
}
printf("\\\\n");
printf("Odd numbers: ");
for (i = 0; i < o; i++) {
printf("%d ", odd[i]);
}
printf("\\\\n");
return 0;
}
4. Insert Values in the Array
#include <stdio.h>
int main() {
int n, pos, value, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[100];
printf("Enter %d elements in ascending order:\\\\n", n);
for (i = 0; i < n; i++) {
printf("Element %d: ", i);
scanf("%d", &arr[i]);
}
printf("Enter the value to insert: ");
scanf("%d", &value);
printf("Enter the position to insert (1 to %d): ", n + 1);
scanf("%d", &pos);
for (i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = value;
printf("Array after insertion:\\\\n");
for (i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
printf("\\\\n");
return 0;
}
5. Adding Two Matrices
#include <stdio.h>
int main() {
int size, i, j;
printf("Enter the size of the square matrix: ");
scanf("%d", &size);
int mat1[10][10], mat2[10][10], result[10][10];
printf("Enter elements of the first matrix:\\\\n");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of the second matrix:\\\\n");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &mat2[i][j]);
}
}
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
printf("Resultant matrix:\\\\n");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("%d ", result[i][j]);
}
printf("\\\\n");
}
return 0;
}
6. Find the Row with Maximum Number of 1s
#include <stdio.h>
int main() {
int rows = 5, cols = 5;
int matrix[5][5] = {
{0, 1, 0, 1, 1},
{1, 1, 1, 1, 1},
{1, 0, 0, 1, 0},
{0, 0, 0, 0, 0},
{1, 0, 0, 0, 1}
};
int i, j, max1s = 0, rowIndex = -1;
for (i = 0; i < rows; i++) {
int count = 0;
for (j = 0; j < cols; j++) {
if (matrix[i][j] == 1) {
count++;
}
}
if (count > max1s) {
max1s = count;
rowIndex = i;
}
}
printf("Row with maximum 1s: %d\\\\n", rowIndex);
return 0;
}
7. Sum of Left Diagonal Elements
#include <stdio.h>
int main() {
int size, i, j, sum = 0;
printf("Enter the size of the square matrix: ");
scanf("%d", &size);
int matrix[10][10];
printf("Enter elements of the matrix:\\\\n");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
if (i == j) {
sum += matrix[i][j];
}
}
}
printf("Sum of left diagonal: %d\\\\n", sum);
return 0;
}