0% found this document useful (0 votes)
43 views

C Programming Assign 7

The document contains 9 C programs that demonstrate various array and matrix operations: 1. Removing duplicate values from an array 2. Interchanging the largest and smallest numbers in an array 3. Filling a square matrix with values based on row/column position 4. Reading and displaying a 3D array 5. Calculating duplicate entries in an array 6. Calculating sum, mean, variance, and standard deviation of array values 7. Summing values above the main diagonal of a matrix 8. Calculating the result of XA + YB where A and B are matrices 9. Finding the kth smallest value in a 2D array

Uploaded by

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

C Programming Assign 7

The document contains 9 C programs that demonstrate various array and matrix operations: 1. Removing duplicate values from an array 2. Interchanging the largest and smallest numbers in an array 3. Filling a square matrix with values based on row/column position 4. Reading and displaying a 3D array 5. Calculating duplicate entries in an array 6. Calculating sum, mean, variance, and standard deviation of array values 7. Summing values above the main diagonal of a matrix 8. Calculating the result of XA + YB where A and B are matrices 9. Finding the kth smallest value in a 2D array

Uploaded by

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

1.Write a program to delete duplicate values from an array.

#include <stdio.h>
int removeDuplicates(int arr[], int n) {
if (n <= 1) {
return n;
}
int uniqueIndex = 1;
for (int i = 1; i < n; i++) {
int j;
for (j = 0; j < uniqueIndex; j++) {
if (arr[i] == arr[j]) {
break;
}
}
if (j == uniqueIndex) {
arr[uniqueIndex] = arr[i];
uniqueIndex++;
}
}
return uniqueIndex;
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Original array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
n = removeDuplicates(arr, n);
printf("\nArray after removing duplicates: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2.Write a program to interchange the largest and the smallest number in the array.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid array size. Please enter a valid size.\n");
return 1;
}
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int minIndex = 0;
int maxIndex = 0;
for (int i = 1; i < n; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
if (arr[i] > arr[maxIndex]) {
maxIndex = i;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[maxIndex];
arr[maxIndex] = temp;
printf("Array after interchanging the largest and smallest elements:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

3. Write a program to fill a square matrix with value 0 on the diagonals, 1 on the upper
right triangle, and -1 on the lower left triangle.

#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows and columns for the square matrix: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid matrix size. Please enter a valid size.\n");
return 1;
}
int matrix[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
matrix[i][j] = 0; // Diagonal
} else if (i < j) {
matrix[i][j] = 1; // Upper right triangle
} else {
matrix[i][j] = -1; // Lower left triangle
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}

4. Write a program to read and display a 2x2x2 array.

#include <stdio.h>
int main() {
int array[2][2][2];
printf("Enter 2x2x2 array elements:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("Enter element at array[%d][%d][%d]: ", i, j, k);
scanf("%d", &array[i][j][k]);
}
}
}
printf("\n2x2x2 Array:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("array[%d][%d][%d] = %d\n", i, j, k, array[i][j][k]);
}
}
}
return 0;
}

5. Write a program to calculate the number of duplicate entries in the array.

#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid array size. Please enter a valid size.\n");
return 1;
}
int arr[n];
printf("Enter %d elements for the array:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int duplicateCount = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
duplicateCount++;
break;
}
}
}
printf("Number of duplicate entries in the array: %d\n", duplicateCount);
return 0;
}

6. Given an array of integers, calculate the sum, mean, variance and standard deviation of
the numbers in the array.

#include <stdio.h>
#include <math.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid array size. Please enter a valid size.\n");
return 1;
}
int arr[n];
printf("Enter %d elements for the array:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Calculate the sum
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
// Calculate the mean
double mean = (double)sum / n;
// Calculate the variance
double variance = 0;
for (int i = 0; i < n; i++) {
variance += pow(arr[i] - mean, 2);
}
variance /= n;
// Calculate the standard deviation
double std_deviation = sqrt(variance);
printf("Sum: %d\n", sum);
printf("Mean: %.2lf\n", mean);
printf("Variance: %.2lf\n", variance);
printf("Standard Deviation: %.2lf\n", std_deviation);
return 0;
}

7.Write a program that reads a matrix and displays the sum of the elements above the
main diagonal.

#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows for the matrix: ");
scanf("%d", &rows);
printf("Enter the number of columns for the matrix: ");
scanf("%d", &cols);
if (rows <= 0 || cols <= 0) {
printf("Invalid matrix size. Please enter valid sizes.\n");
return 1;
}
int matrix[rows][cols];
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at matrix[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\nMatrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < cols; j++) {
sum += matrix[i][j];
}
}
printf("\nSum of elements above the main diagonal: %d\n", sum);
return 0;
}

8. Write a program to calculate XA + YB where A and B are matrices and X=2, and Y=3.

#include <stdio.h>
int main() {
int rowsA, colsA, rowsB, colsB;
int X = 2, Y = 3;
printf("Enter the number of rows for matrix A: ");
scanf("%d", &rowsA);
printf("Enter the number of columns for matrix A: ");
scanf("%d", &colsA);
printf("Enter the number of rows for matrix B: ");
scanf("%d", &rowsB);
printf("Enter the number of columns for matrix B: ");
scanf("%d", &colsB);
if (colsA != rowsB) {
printf("Matrix multiplication is not possible. Columns of A must equal rows of B.\n");
return 1;
}
int A[rowsA][colsA], B[rowsB][colsB], result[rowsA][colsB];
printf("Enter values for matrix A:\n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
printf("Enter values for matrix B:\n");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
printf("B[%d][%d]: ", i, j);
scanf("%d", &B[i][j]);
}
}
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = X * A[i][j] + Y * B[i][j];
}
}
printf("Result (XA + YB):\n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}

9. Write a program to find the kth smallest value of the 2-dimensional mXn array.

#include <stdio.h>
int main() {
int m, n;
printf("Enter the number of rows for the 2D array: ");
scanf("%d", &m);
printf("Enter the number of columns for the 2D array: ");
scanf("%d", &n);
if (m <= 0 || n <= 0) {
printf("Invalid array dimensions. Please enter valid values.\n");
return 1;
}
int array[100][100];
printf("Enter %dx%d array elements:\n", m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &array[i][j]);
}
}
int smallest = array[0][0];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (array[i][j] < smallest) {
smallest = array[i][j];
}
}
}
printf("The smallest value in the 2D array is: %d\n", smallest);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy