Write a function to compute mean, variance, Standard Deviation, sorting of n elements in a single dimension array.
Write a function to compute mean, variance, Standard Deviation, sorting of n elements in a single dimension array.
Algorithm
start
step1:
step2:
step3:
step4:
step5:
step6:
step7:
step8:
step9:
step10:
stop
Flowchart
C Programming
//To compute mean, variance, Standard Deviation, sorting of n elements in a single dimension array
#include <stdio.h>
#include <math.h>
// Function prototypes
double computeMean(int arr[], int n);
double computeVariance(int arr[], int n, double mean);
double computeStandardDeviation(double variance);
void sortArray(int arr[], int n);
int main() {
int n,arr[10],i;
double mean,variance,stddev;
// Read the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Read the elements of the array
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Compute mean, variance, and standard deviation
mean = computeMean(arr, n);
variance = computeVariance(arr, n, mean);
stddev = computeStandardDeviation(variance);
// Sort the array
sortArray(arr, n);
// Print the results
printf("Mean: %.2f\n", mean);
printf("Variance: %.2f\n", variance);
printf("Standard Deviation: %.2f\n", stddev);
printf("Sorted Array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
// Function to compute mean
double computeMean(int arr[], int n) {
double sum = 0;int i;
for (i = 0; i < n; i++) {
sum += arr[i];
}
return sum / n;
}
// Function to compute variance
double computeVariance(int arr[], int n, double mean) {
double sum = 0; int i;
for (i = 0; i < n; i++) {
sum += (arr[i] - mean) * (arr[i] - mean);
}
return sum / n;
}
// Function to compute standard deviation
double computeStandardDeviation(double variance) {
return sqrt(variance);
}
// Function to sort the array using bubble sort
void sortArray(int arr[], int n) {
int i,j,temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
OUTPUT
Enter the number of elements in the array: 5
Enter 5 integers:
20
10
15
5
2
Mean: 10.40
Variance: 42.64
Standard Deviation: 6.53
Sorted Array: 2 5 10 15 20
๐ป Online C Compiler
Run and test this C program online. You can also edit the code before executing it.
๐ก Edit the program in the compiler and click
Copy Program
to copy the latest edited code.
Related Lab Programs
๐ป
Practice Sessions
1.
Write a simple program that prints the results of all the operators available in C.
View Solution โ
2.
Write a simple program that converts one given data type to another using auto conversion and casting.
View Solution โ
๐ข
Simple Numeric Problems
3.
Write a program to display the class awarded based on the percentage obtained by a student.
View Solution โ
5.
Write a program that shows the binary equivalent of a given positive number between 0 to 255.
View Solution โ
โ
Expression Evaluation
4.
Write a C program to find the sum of individual digits and test whether the number is palindrome.
View Solution โ
๐งฎ
Arrays, Pointers and Functions
1.
Write a C program to find the minimum, maximum and average in an array of integers.
View Solution โ
2.
Write a C program to find the mean, variance and standard deviation of an array and sort the array.
Current
4.
Write a C program that uses functions to perform Multiplication of two matrices.
View Solution โ
5.
Write a C program to find the transpose of a matrix using dynamically allocated memory.
6.
Write a C program to find factorial using recursive and non-recursive functions.
View Solution โ
7.
Write a C program to find the greatest common divisor using recursive and non-recursive functions.
View Solution โ
8.
Write a C program to calculate x raised to the power of n using recursive and non-recursive functions.
View Solution โ
๐
Files
2.
Write a C program which copies one file to another, replacing lowercase characters with uppercase equivalents.
3.
Write a C program to count occurrences of a character in a text file using command line arguments.
4.
Write a C program to create and update a binary file using fseek.
๐ค
Strings
1.
Write a C program to convert a Roman numeral in the range I to L into its decimal equivalent.
2.
Write a C program to convert a number between 1 and 50 into its Roman numeral equivalent.
3.
Write a C program that uses functions to insert a sub-string into a given main string.
View Solution โ
4.
Write a C program that uses functions to delete n characters from a given position.
View Solution โ
๐
Miscellaneous
1.
Write a menu-driven program to find the smallest, largest, sum and average.
2.
Write a C program to construct pyramids using stars or numbers.
๐