0% found this document useful (0 votes)
23 views9 pages

Demm

mmmfjjdkjj

Uploaded by

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

Demm

mmmfjjdkjj

Uploaded by

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

write a c program to find the minimum maximum and average in an array of integers

#include <stdio.h>

void findMinMaxAvg(int arr[], int size, int *min, int *max, float *avg) {
*min = arr[0];
*max = arr[0];
int sum = 0;

for (int i = 0; i < size; i++) {


if (arr[i] < *min) {
*min = arr[i];
}
if (arr[i] > *max) {
*max = arr[i];
}
sum += arr[i];
}

*avg = (float)sum / size;


}

int main() {
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

if (n <= 0) {
printf("Array size must be greater than 0.\n");
return 1;
}

int arr[n];

printf("Enter %d integers:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int min, max;


float avg;

findMinMaxAvg(arr, n, &min, &max, &avg);

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


printf("Maximum: %d\n", max);
printf("Average: %.2f\n", avg);

return 0;
}

INPUT:
Enter the number of elements in the array: 5
Enter 5 integers:
3 8 1 6 7

OUTPUT:
Minimum: 1
Maximum: 8
Average: 5.00

write a function to compute mean , variance , standard deviation, sorting of n


elements in a single dimension array

#include <stdio.h>
#include <math.h>

void computeStats(float arr[], int size, float *mean, float *variance, float
*stdDev);
void sortArray(float arr[], int size);

int main() {
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

if (n <= 0) {
printf("Array size must be greater than 0.\n");
return 1;
}

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

float mean, variance, stdDev;

computeStats(arr, n, &mean, &variance, &stdDev);

sortArray(arr, n);

printf("Mean: %.2f\n", mean);


printf("Variance: %.2f\n", variance);
printf("Standard Deviation: %.2f\n", stdDev);
printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%.2f ", arr[i]);
}
printf("\n");

return 0;
}

void computeStats(float arr[], int size, float *mean, float *variance, float
*stdDev) {
float sum = 0, sumSquaredDiff = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
*mean = sum / size;

for (int i = 0; i < size; i++) {


sumSquaredDiff += pow(arr[i] - *mean, 2);
}
*variance = sumSquaredDiff / size;

*stdDev = sqrt(*variance);
}

void sortArray(float arr[], int size) {


for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
float temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

INPUT:
Enter the number of elements in the array: 5
Enter 5 elements:
3.2 8.1 1.5 6.0 7.7

OUTPUT:
Mean: 5.30
Variance: 6.16
Standard Deviation: 2.48
Sorted Array: 1.50 3.20 6.00 7.70 8.10

3. Write a C program that uses functions to perform the


following:
i. Addition of Two Matrices ii. Multiplication of Two Matrices
iii. Transpose of a matrix.

#include <stdio.h>

// Function prototypes
void addMatrices(int rows, int cols, int mat1[rows][cols], int mat2[rows][cols],
int result[rows][cols]);
void multiplyMatrices(int r1, int c1, int mat1[r1][c1], int r2, int c2, int
mat2[r2][c2], int result[r1][c2]);
void transposeMatrix(int rows, int cols, int mat[rows][cols], int result[cols]
[rows]);
void displayMatrix(int rows, int cols, int mat[rows][cols]);

int main() {
int choice;

printf("Choose an operation:\n");
printf("1. Addition of Two Matrices\n");
printf("2. Multiplication of Two Matrices\n");
printf("3. Transpose of a Matrix\n");
scanf("%d", &choice);

if (choice == 1) {
// Addition of two matrices
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);

int mat1[rows][cols], mat2[rows][cols], result[rows][cols];

printf("Enter elements of first matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat1[i][j]);
}
}

printf("Enter elements of second matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat2[i][j]);
}
}

addMatrices(rows, cols, mat1, mat2, result);

printf("Result of matrix addition:\n");


displayMatrix(rows, cols, result);

} else if (choice == 2) {
// Multiplication of two matrices
int r1, c1, r2, c2;

printf("Enter rows and columns of first matrix: ");


scanf("%d %d", &r1, &c1);

printf("Enter rows and columns of second matrix: ");


scanf("%d %d", &r2, &c2);

if (c1 != r2) {
printf("Matrix multiplication not possible: Number of columns of first
matrix must equal number of rows of second matrix.\n");
return 1;
}

int mat1[r1][c1], mat2[r2][c2], result[r1][c2];

printf("Enter elements of first matrix:\n");


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
scanf("%d", &mat2[i][j]);
}
}

multiplyMatrices(r1, c1, mat1, r2, c2, mat2, result);

printf("Result of matrix multiplication:\n");


displayMatrix(r1, c2, result);

} else if (choice == 3) {
// Transpose of a matrix
int rows, cols;

printf("Enter the number of rows and columns: ");


scanf("%d %d", &rows, &cols);

int mat[rows][cols], result[cols][rows];

printf("Enter elements of the matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat[i][j]);
}
}

transposeMatrix(rows, cols, mat, result);

printf("Transpose of the matrix:\n");


displayMatrix(cols, rows, result);

} else {
printf("Invalid choice!\n");
}

return 0;
}

// Function to add two matrices


void addMatrices(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];
}
}
}

// Function to multiply two matrices


void multiplyMatrices(int r1, int c1, int mat1[r1][c1], int r2, int c2, int
mat2[r2][c2], int result[r1][c2]) {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
for (int k = 0; k < c1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

// Function to transpose a matrix


void transposeMatrix(int rows, int cols, int mat[rows][cols], int result[cols]
[rows]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = mat[i][j];
}
}
}

// Function to display a matrix


void displayMatrix(int rows, int cols, int mat[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}

INPUT:
Choose an operation:
1. Addition of Two Matrices
2. Multiplication of Two Matrices
3. Transpose of a Matrix
1
Enter the number of rows and columns: 2 2
Enter elements of first matrix:
1 2
3 4
Enter elements of second matrix:
5 6
7 8

OUTPUT:
Result of matrix addition:
6 8
10 12

4. Write C programs that use both recursive and non-recursive


functions

#include <stdio.h>

// Function prototypes
int factorialRecursive(int n);
int factorialNonRecursive(int n);
void fibonacciRecursive(int n, int a, int b, int count);
void fibonacciNonRecursive(int n);

int main() {
int choice, num;

printf("Choose an operation:\n");
printf("1. Factorial (Recursive and Non-Recursive)\n");
printf("2. Fibonacci (Recursive and Non-Recursive)\n");
scanf("%d", &choice);

if (choice == 1) {
// Factorial calculation
printf("Enter a number to calculate factorial: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 1;
}

printf("Factorial (Recursive): %d\n", factorialRecursive(num));


printf("Factorial (Non-Recursive): %d\n", factorialNonRecursive(num));

} else if (choice == 2) {
// Fibonacci sequence
printf("Enter the number of terms for Fibonacci sequence: ");
scanf("%d", &num);

if (num <= 0) {
printf("Please enter a positive number of terms.\n");
return 1;
}

printf("Fibonacci (Recursive): ");


fibonacciRecursive(num, 0, 1, 1);
printf("\n");

printf("Fibonacci (Non-Recursive): ");


fibonacciNonRecursive(num);
printf("\n");

} else {
printf("Invalid choice!\n");
}

return 0;
}

// Recursive function to calculate factorial


int factorialRecursive(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorialRecursive(n - 1);
}

// Non-recursive function to calculate factorial


int factorialNonRecursive(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}

// Recursive function to print Fibonacci sequence


void fibonacciRecursive(int n, int a, int b, int count) {
if (count > n)
return;

printf("%d ", a);


fibonacciRecursive(n, b, a + b, count + 1);
}

// Non-recursive function to print Fibonacci sequence


void fibonacciNonRecursive(int n) {
int a = 0, b = 1;

for (int i = 1; i <= n; i++) {


printf("%d ", a);
int next = a + b;
a = b;
b = next;
}
}

INPUT:
Choose an operation:
1. Factorial (Recursive and Non-Recursive)
2. Fibonacci (Recursive and Non-Recursive)
2
Enter the number of terms for Fibonacci sequence: 7

OUTPUT:
Fibonacci (Recursive): 0 1 1 2 3 5 8
Fibonacci (Non-Recursive): 0 1 1 2 3 5 8

5. To find the factorial of a given integer.

#include <stdio.h>

// Function prototypes
int factorialRecursive(int n);
int factorialNonRecursive(int n);

int main() {
int num, choice;

printf("Enter an integer to calculate its factorial: ");


scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative integers.\n");
return 1;
}

printf("Choose the method:\n");


printf("1. Recursive\n");
printf("2. Non-Recursive\n");
scanf("%d", &choice);

if (choice == 1) {
printf("Factorial (Recursive): %d\n", factorialRecursive(num));
} else if (choice == 2) {
printf("Factorial (Non-Recursive): %d\n", factorialNonRecursive(num));
} else {
printf("Invalid choice!\n");
}

return 0;
}

// Recursive function to calculate factorial


int factorialRecursive(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorialRecursive(n - 1);
}

// Non-recursive function to calculate factorial


int factorialNonRecursive(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}

INPUT:
Enter an integer to calculate its factorial: 5
Choose the method:
1. Recursive
2. Non-Recursive
2

OUTPUT:
Factorial (Non-Recursive): 120

You might also like