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

C Style Standard Deviation and Average Using Arrays

This C program calculates the standard deviation of a set of 10 numbers entered by the user. It defines a function called calculateSD that takes an array as a parameter, calculates the mean and standard deviation using formulas, and returns the standard deviation. Main gets 10 inputs from the user, calls calculateSD to find the standard deviation and prints the result. A second program gets an array of numbers of any size from 1-100 from the user, calculates the sum and average, and prints the average.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

C Style Standard Deviation and Average Using Arrays

This C program calculates the standard deviation of a set of 10 numbers entered by the user. It defines a function called calculateSD that takes an array as a parameter, calculates the mean and standard deviation using formulas, and returns the standard deviation. Main gets 10 inputs from the user, calls calculateSD to find the standard deviation and prints the result. A second program gets an array of numbers of any size from 1-100 from the user, calculates the sum and average, and prints the average.

Uploaded by

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

// Population Standard Deviation

// SD of a population
#include <math.h>
#include <stdio.h>
float calculateSD(float data[]);
int main() {
int i;
float data[10];
printf("Enter 10 elements: ");
for (i = 0; i < 10; ++i)
scanf("%f", &data[i]);
printf("\nStandard Deviation = %.6f", calculateSD(data));
return 0;
}

float calculateSD(float data[]) {


float sum = 0.0, mean, SD = 0.0;
int i;
for (i = 0; i < 10; ++i) {
sum += data[i];
}
mean = sum / 10;
for (i = 0; i < 10; ++i) {
SD += pow(data[i] - mean, 2);
}
return sqrt(SD / 10);
}

// Store Numbers and Calculate Average Using Arrays

#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;

printf("Enter the numbers of elements: ");


scanf("%d", &n);

while (n > 100 || n < 1) {


printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}

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


printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}

avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}

You might also like