0% found this document useful (0 votes)
5 views7 pages

programing-class-assignment1SF

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views7 pages

programing-class-assignment1SF

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

1.

Count the Total Number of Duplicate Elements

#include <stdio.h>

int main() {

int n, i, j, count = 0;

printf("Input the number of elements to be stored in the array:


");

scanf("%d", &n);

int arr[n];

printf("Input %d elements in the array:\n", 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;

printf("Total number of duplicate elements found in the array is:


%d\n", count);
return 0;

2. Find Maximum and Minimum Elements

#include <stdio.h>

int main() {

int n, i, max, min;

printf("Input the number of elements to be stored in the array:


");

scanf("%d", &n);

int arr[n];

printf("Input %d elements in the array:\n", n);

for (i = 0; i < n; i++) {

printf("element - %d: ", i);

scanf("%d", &arr[i]);

max = 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 element is: %d\n", max);

printf("Minimum element is: %d\n", min);


return 0;

3. Separate Odd and Even Integers

#include <stdio.h>

int main() {

int n, i;

printf("Input the number of elements to be stored in the array:


");

scanf("%d", &n);

int arr[n], even[n], odd[n], e = 0, o = 0;

printf("Input %d elements in the array:\n", n);

for (i = 0; i < n; i++) {

printf("element - %d: ", i);

scanf("%d", &arr[i]);

if (arr[i] % 2 == 0)

even[e++] = arr[i];

else

odd[o++] = arr[i];

printf("The Even elements are:\n");

for (i = 0; i < e; i++) printf("%d ", even[i]);

printf("\nThe Odd elements are:\n");


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("Input the size of array: ");
scanf("%d", &n);
int arr[n + 1];
printf("Input %d elements in the array in ascending order:\n", n);
for (i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr[i]);
}
printf("Input the value to be inserted: ");
scanf("%d", &value);
printf("Input the position, where the value to be inserted: ");
scanf("%d", &pos);

for (i = n; i >= pos; i--)


arr[i] = arr[i - 1];
arr[pos - 1] = value;

printf("The current list of the array:\n");


for (i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\nAfter inserting the element, the new list is:\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("Input the size of the square matrix (less than 5): ");
scanf("%d", &size);
int mat1[size][size], mat2[size][size], result[size][size];

printf("Input elements in 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("Input elements in 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]);
}

printf("The First matrix is:\n");


for (i = 0; i < size; i++) {
for (j = 0; j < size; j++)
printf("%d ", mat1[i][j]);
printf("\n");
}

printf("The Second matrix is:\n");


for (i = 0; i < size; i++) {
for (j = 0; j < size; j++)
printf("%d ", mat2[i][j]);
printf("\n");
}

printf("The Addition of two matrices is:\n");


for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
result[i][j] = mat1[i][j] + mat2[i][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, i, j, max1s = 0, rowIndex = -1;
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}
};
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("The index of row with maximum 1s is: %d\n", rowIndex);
return 0;
}

7. Sum of Left Diagonal Elements


#include <stdio.h>
int main() {
int size, i, j, sum = 0;
printf("Input the size of the square matrix: ");
scanf("%d", &size);
int matrix[size][size];
printf("Input elements in 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("The matrix is:\n");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++)
printf("%d ", matrix[i][j]);
printf("\n");
}
printf("Addition of the left diagonal elements is: %d\n", sum);
return 0;
}

You might also like