Assignment 6
Problem 1: WAP in C that simply takes elements of the array from the user and finds the sum of these elements.
sol.
#include <stdio.h>
int main() {
int A[100], n, i, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 0 || n > 100) {
printf("Invalid input for the number of elements.\n");
return 1;
}
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &A[i]);
sum += A[i];
}
printf("The sum of the elements is: %d\n", sum);
return 0;
}
Problem 2: WAP in C that inputs two arrays and saves sum of corresponding elements of these arrays in a third array and prints them.
sol.
#include <stdio.h>
int main() {
int A[100], B[100], C[100], size, i;
printf("Enter the size of the arrays: ");
scanf("%d", &size);
printf("\nEnter elements of the first array:\n");
for (i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &A[i]);
}
printf("\nEnter elements of the second array:\n");
for (i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &B[i]);
}
// Adding corresponding elements and storing in C array
for (i = 0; i < size; i++) {
C[i] = A[i] + B[i];
}
printf("\nThe sum of corresponding elements:\n");
for (i = 0; i < size; i++) {
printf("%d + %d = %d\n", A[i], B[i], C[i]);
}
return 0;
}
Problem 3: WAP in C to find the minimum and maximum element of the array
sol.
#include <stdio.h>
int main() {
int A[100], size, i, min, max;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of the array:\n");
for (i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &A[i]);
}
min = max = A[0]; // Assuming first element as minimum and maximum
for (i = 1; i < size; i++) {
// Finding minimum and maximum elements
if (A[i] < min) {
min = A[i];
}
if (A[i] > max) {
max = A[i];
}
}
printf("The minimum element in the array is: %d\n", min);
printf("The maximum element in the array is: %d\n", max);
return 0;
}
Problem 4: WAP in C to find the second largest element in an array.
sol.
#include <stdio.h>
int main() {
int A[100], size, i, firstMax, secondMax;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of the array:\n");
for (i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &A[i]);
}
firstMax = secondMax = A[0];
for (i = 0; i < size; i++) {
if (A[i] > firstMax) {
secondMax = firstMax;
firstMax = A[i];
} else if (A[i] > secondMax && A[i] != firstMax) {
secondMax = A[i];
}
}
printf("The second largest element in the array is: %d\n", secondMax);
return 0;
}
Problem 5: WAP in C to count total number of even and odd elements in an array.
sol.
#include <stdio.h>
int main() {
int A[100], size, i, countEven = 0, countOdd = 0;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of the array:\n");
for (i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &A[i]);
}
for (i = 0; i < size; i++) {
if (A[i] % 2 == 0) {
countEven++;
} else {
countOdd++;
}
}
printf("Total even elements: %d\n", countEven);
printf("Total odd elements: %d\n", countOdd);
return 0;
}
Problem 6: WAP in C to insert an element in an array.
sol.
#include <stdio.h>
int main() {
int ar[50], i, elem, n;
printf("Enter size of array: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &ar[i]);
}
printf("Insert Element in array: ");
scanf("%d", &elem);
ar[n] = elem;
printf("Array after insertion: ");
for (i = 0; i < n + 1; i++) {
printf("\t%d\t", ar[i]);
}
return 0;
}
Problem 7: WAP in C to delete an element from an array at specified position.
sol.
#include <stdio.h>
int main() {
int arr[100],size, pos,i;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of the array:\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the position to delete an element: ");
scanf("%d", &pos);
if (pos < 0 || pos >= size) {
printf("Invalid position\n");
} else {
for (i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
printf("Element at position %d deleted successfully\n", pos);
printf("Updated array: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
Problem 8: Write a program in C to count frequency of each element in an array.
sol.
#include<stdio.h>
int main() {
int i, n, j, count,flag[50],a[50];
printf("Enter the number of elements in the array: \n");
scanf("%d", &n);
// inputing elements by user
printf("Enter the elements for the array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
flag[i] = 0; //flag array
}
printf("\nArray:\n");
for(i = 0; i < n; i++) {
printf("%d\t", a[i]);
}
printf("\n");
// Counting frequency of each element
for(i = 0; i < n; i++) {
if(flag[i] == 0) {
count = 1;
for(j = i + 1; j < n; j++) {
if(a[i] == a[j] && flag[j] == 0) {
count++;
flag[j] = 1;
}
}
printf("\nFrequency of %d is: %d\n", a[i], count);
}
}
return 0;
}
Problem 9: Write a program in C to delete all duplicate elements from an array.
sol.
#include <stdio.h>
int main() {
int i, j, num,a[50],flag[50];
printf("Enter size of array: ");
scanf("%d", &num);
printf("Enter the elements in array:\n");
for (i=0; i<num; i++) {
scanf("%d", &a[i]);
}
for (i=0; i<num; i++) {
flag[i] = 0;
}
printf("after removing duplicate elements in array: \n");
for (i = 0; i<num; i++) {
for (j=i+1; j<num; j++) {
if (a[i] == a[j] && flag[i] ==0 ) {
flag[j] = 1;
}
}
if(flag[i] ==0)
printf("%d\t",a[i]);
}
return 0;
}
Problem 10: Write a program in C to merge two arrays into a third array.
sol.
#include<stdio.h>
int main( ){
int i,j,m,n,A[100],B[100],C[100];
printf("Enter the size of first array:");
scanf("%d",&m);
printf("Enter the size of second array:");
scanf("%d",&n);
printf("Enter elements in first array:");
for(i=0;i<m;i++){
scanf("%d",&A[i]);
}
printf("Enter elements in second array:");
for(i=0;i<n;i++){
scanf("%d",&B[i]);
}
int sizeofC = m+n;
for(i=0;i<m;i++){
C[i]=A[i];
}
for (i=0,j=m; j<sizeofC && i<n; i++,j++){
C[j]=B[i];
}
printf("The Merged array is:\n");
for(i=0;i<sizeofC;i++){
printf("%d\t",C[i]);
}
}
Problem 11: Write a program in C that finds the addition and multiplication of a m x n matrix.
sol.
#include <stdio.h>
int main() {
int i,j,k,n,A[5][5], B[5][5], sumMat[5][5], proMat[5][5];
// Input the size of the matrices
printf("Enter the size of matrices (n x n): ");
scanf("%d", &n);
// Inputing first and second matrix.
printf("Enter elements of matrix A:\n");
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
printf("Enter element A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
printf("Enter elements of matrix B:\n");
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
printf("Enter element B[%d][%d]: ", i, j);
scanf("%d", &B[i][j]);
}
}
//printing entered matrix A and B
printf("Given matrix A is:\n");
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
printf("%d\t",A[i][j]);
}printf("\n");
}
printf("Given matrix B is:\n");
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
printf("%d\t",B[i][j]);
}printf("\n");
}
printf("\nSum of matrices A and B:\n"); // Addition of matrices
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
sumMat[i][j] = A[i][j] + B[i][j];
printf("%d\t", sumMat[i][j]);
}
printf("\n");
}
printf("\nProduct of matrices A and B:\n"); // Multiplication of matrices
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
proMat[i][j] = 0;
for (k = 0; k < n; ++k) {
proMat[i][j] += A[i][k] * B[k][j];
}
printf("%d\t", proMat[i][j]);
}
printf("\n");
}
return 0;
}
Problem 12: Write a program in C to perform Scalar matrix multiplication.
sol.
#include <stdio.h>
int main() {
int rows,cols,scalar,i,j;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
int arr[rows][cols];
printf("Enter the elements of the matrix:\n");
for (i=0; i<rows; i++) {
for (j=0; j<cols; j++) {
scanf("%d", &arr[i][j]);
}
}
printf("Enter the scalar value: ");
scanf("%d", &scalar);
printf("Result of scalar matrix multiplication:\n");
for (i=0; i<rows; i++) {
for (j=0; j<cols; j++) {
printf("%d\t", arr[i][j] * scalar); //scalar matrix multiplication
}
printf("\n");
}
return 0;
}
Problem 13: Write a program in C that finds the sum of diagonal elements of a m x n matrix.
sol.
#include <stdio.h>
int main() {
int m,n,i,j,arr[5][5];
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &m, &n);
// checking the matrix is square or not for diagonal sum.
if (m != n) {
printf("Diagonal sum is not defined for a non-square matrix.\n");
return 1;
}
printf("Enter the elements of the matrix:\n");
for (i=0; i<m; i++) {
for (j = 0; j<n; j++) {
scanf("%d", &arr[i][j]);
}
}
printf("Entered matrix is:\n");
for (i=0; i<m; i++) {
for (j = 0; j<n; j++) {
printf("%d\t",arr[i][j]);
} printf("\n");
}
int diagonalSum = 0;
for (i=0; i<m; i++) {
diagonalSum += arr[i][i]; // sum of diagonal elements
}
printf("Sum of diagonal elements: %d\n", diagonalSum);
return 0;
}
Problem 14: Write a program in C to find and display the upper triangular matrix.
sol.
#include <stdio.h>
int main() {
int matrix[5][5],order, order,i,j;
printf("Enter the order of matrix: ");
scanf("%d", &order);
if (order <= 0 || order > 5) {
printf("Invalid input for order. Exiting...\n");
return 1;
}
printf("Enter the elements of the matrix:\n");
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
printf("Enter element matrix[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\nOriginal Matrix:\n"); //original matrix
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\nUpper Triangular Matrix:\n"); // Display the Upper triangular matrix
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
if (j >= i) {
printf("%d\t", matrix[i][j]);
} else {
printf("0\t");
}
}
printf("\n");
}
return 0;
}
Problem 15: Write a program in C to find the sum of lower triangular matrix.
sol.
#include<stdio.h>
int main() {
int matrix[5][5],order,i,j, sum = 0;
printf("Enter the number of order and columns : ");
scanf("%d", &order);
if (order <= 0 || order > 5) {
printf("Invalid input for order. Exiting...\n");
return 1;
}
printf("Enter the elements of the matrix:\n");
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
printf("Enter element matrix[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\nOriginal Matrix:\n"); //Original matrix
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Calculation of the sum of lower triangular matrix.
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
if (j <= i) {
sum += matrix[i][j];
}
}
}
printf("\nSum of Lower Triangular Matrix: %d\n", sum);
return 0;
}
Problem 16: Write a Program in C to find transpose of a matrix.
sol.
#include<stdio.h>
int main() {
int A[5][5], i, j, n;
printf("Enter the order of matrix: ");
scanf("%d", &n);
printf("Enter the elements in A matrix:\n");
for (i=0; i<n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
printf("The Matrix is:\n");
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("\n");
// Transposing the elements
for (i=0; i<n; i++) {
for (j=i+1; j<n; j++) {
// Swapping elements (using a temporary variable)
int temp = A[i][j];
A[i][j] = A[j][i];
A[j][i] = temp;
}
}
printf("The Transpose of Matrix is:\n");
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
return 0;
}
Problem 17: Write a program in C to find determinant of a matrix.
sol.
#include <stdio.h>
int main() {
int a[3][3],det=0,i,j;
printf("Enter the elements of the 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Entered matrix is:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
// Calculating the determinant
det + = (a[0][0] * ( a[1][1]*a[2][2] - a[2][1]*a[1][2] ));
det - = (a[0][1] * ( a[1][0]*a[2][2] - a[2][0]*a[1][2] ));
det + = (a[0][2] * ( a[1][0]*a[2][1] - a[2][0]*a[1][1] ));
printf("Determinant of the matrix: %d\n", det);
return 0;
}
Problem 18: Write a program in C to check if the given matrix is an Identity matrix or not.
sol.
#include <stdio.h>
int main() {
int m,n,i,j,arr[5][5],flag=0;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &m, &n);
// checking the matrix is square or not for identity matrix.
if (m != n) {
printf("the given matrix order is non-square matrix.\n");
return 1;
}
printf("Enter the elements of the matrix:\n");
for (i=0; i<m; i++) {
for (j = 0; j<n; j++) {
scanf("%d", &arr[i][j]);
}
}
printf("Entered matrix is:\n");
for (i=0; i<m; i++) {
for (j = 0; j<n; j++) {
printf("%d\t",arr[i][j]);
} printf("\n");
}
for (i=0; i<m; i++) {
for (j = 0; j<n; j++) {
if (arr[i][i]==1&&arr[i][j]==0) { //identity condition
flag=1;
}
}
}
if (flag==1){
printf("The matrix is Indentity matrix.");
}
else{
printf("The matrix is Not Indentity matrix.");
}
Problem 19: Write a program in C to check if the given matrix is Symmetric or not.
sol.
#include <stdio.h>
int main() {
int a[5][5], t[5][5], m, i, j, flag = 0;
printf("Enter the order of matrix: ");
scanf("%d", &m);
printf("Enter the matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
t[j][i] = a[i][j];
}
}
printf("Your Entered Matrix");
for(i=0;i<m;i++){
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
//checking symmetric condition (given matrix is equal to tranpose matrix or not) .
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
if (t[i][j] != a[i][j]) {
flag = 1;
break;
}
}
}
if (flag == 1)
printf("Matrix is not symmetric");
else
printf("Matrix is symmetric");
return 0;
}
Problem 20: Write a program in c to check matrix is sparse or not.
sol.
#include<stdio.h>
int main(){
int i,j,m,n,a[10][10],total=0;
printf("\n Please Enter order of m and n\t");
scanf("%d %d",&m,&n);
printf("\n Please Enter the Matrix Elements \n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
//counting number of zeroes in matrix
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(a[i][j]==0){
total++;
}
}
}
if (total>(m*n)/2){
printf("\n The Matrix that you entered is a Sparse Matrix");
}
else {
printf("\n The Matrix that you entered is a Not a Sparse Matrix");
}
}
Problem 21: Write a program in C to search an element in an array using Linear Search.
sol.
#include<stdio.h>
int main() {
int a[50], n, i, t = 0, m, p;
printf("Enter the size of n: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter the element that you want to search: ");
scanf("%d", &m);
for (i = 0; i < n; i++) {
if (m == a[i]) {
t = 1;
p = i;
break;
}
}
if (t == 1) {
printf("Element is found at %d position\n", p);
} else {
printf("Element not found\n");
}
}
Problem 22: Write a program in C to sort the elements of the array in ascending order using Bubble Sort technique.
sol.
#include<stdio.h>
int main(){
int arr[50],i,j,num,temp;
printf("Enter the value of num\n");
scanf("%d",&num);
printf("enter the elements one by one ");
for(i=0;i<num;i++){
scanf("%d",&arr[i]);
}
printf("Input array is\n");
for(i=0;i<num;i++){
printf("%d\t",arr[i]);
}
//bubble sort begins
for(i=0;i<num;i++){
for(j=0;j<(num-i-1);j++){
if (arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\nSorted array is \n");
for(i=0;i<num;i++){
printf("%d\t",arr[i]);
}
}
0 comments:
Post a Comment