0% found this document useful (0 votes)
53 views6 pages

C Programs

Uploaded by

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

C Programs

Uploaded by

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

Write a program to find standanrd deviation in discrete series

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

int main() {
int n;
printf("Enter the number of distinct data points: ");
scanf("%d", &n);

double x[n], f[n]; // Arrays to hold values and their frequencies


double sum_fx = 0.0, sum_f = 0.0, mean, variance = 0.0, stddev;

// Input data points and their frequencies


printf("Enter the values (x) and their frequencies (f):\n");
for (int i = 0; i < n; i++) {
printf("Value x[%d]: ", i + 1);
scanf("%lf", &x[i]);
printf("Frequency f[%d]: ", i + 1);
scanf("%lf", &f[i]);

sum_fx += x[i] * f[i]; // Sum of x * f


sum_f += f[i]; // Sum of frequencies
}

// Calculate mean
mean = sum_fx / sum_f;

// Calculate variance
for (int i = 0; i < n; i++) {
variance += f[i] * (x[i] - mean) * (x[i] - mean);
}
variance /= sum_f; // Divide by total frequency for variance

// Calculate standard deviation


stddev = sqrt(variance);

// Output the results


printf("Mean: %.2lf\n", mean);
printf("Standard Deviation: %.2lf\n", stddev);

return 0;
}

Write a program to find median of discrete series


#include <stdio.h>

int main() {
int n;
printf("Enter the number of distinct data points: ");
scanf("%d", &n);

double x[n], f[n]; // Arrays for values and their frequencies


int cumulative_frequency[n]; // Array for cumulative frequencies
int total_frequency = 0.0;

// Input data points and their frequencies


printf("Enter the values (x) and their frequencies (f):\n");
for (int i = 0; i < n; i++) {
printf("Value x[%d]: ", i + 1);
scanf("%lf", &x[i]);
printf("Frequency f[%d]: ", i + 1);
scanf("%lf", &f[i]);

total_frequency += f[i]; // Calculate total frequency


}

// Calculate cumulative frequencies


cumulative_frequency[0] = f[0];
for (int i = 1; i < n; i++) {
cumulative_frequency[i] = cumulative_frequency[i - 1] + f[i];
}

// Finding the median


double median;
double median_position = total_frequency / 2;

// Determine the median class


for (int i = 0; i < n; i++) {
if (cumulative_frequency[i] >= median_position)
{
if ((total_frequency % 2) == 0 && (cumulative_frequency[i] == median_position))
{
// If the total frequency is even and we hit the exact middle
median = (x[i] + x[i + 1]) / 2;
}
else
{
median = x[i];
}
break;
}
}

// Output the results


printf("Median: %.2lf\n", median);

return 0;
}

Write a program to find mean of continous series


#include <stdio.h>
int main() {
int n;
printf("Enter the number of class intervals: ");
scanf("%d", &n);

double lower_limit[n], upper_limit[n], f[n]; // Arrays for class limits and frequencies
double midpoints[n]; // Array for midpoints
double sum_fx = 0.0, sum_f = 0.0, mean;

// Input class intervals and their frequencies


printf("Enter the lower and upper limits of each class interval along with their frequencies:\n");
for (int i = 0; i < n; i++) {
printf("Class interval %d (lower limit- upper limit): ", i + 1);
scanf("%lf %lf", &lower_limit[i], &upper_limit[i]);
printf("Frequency f[%d]: ", i + 1);
scanf("%lf", &f[i]);

// Calculate midpoint of each class interval


midpoints[i] = (lower_limit[i] + upper_limit[i]) / 2.0;

// Calculate sum of f * midpoint


sum_fx += f[i] * midpoints[i];
sum_f += f[i]; // Calculate total frequency
}

// Calculate mean
mean = sum_fx / sum_f;

// Output the result


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

return 0;
}

Write a program to find median of continous series


#include <stdio.h>
int main() {
int n;
printf("Enter the number of class intervals: ");
scanf("%d", &n);

double lower_limit[n], upper_limit[n], f[n]; // Arrays for class limits and frequencies
double cumulative_frequency[n]; // Array for cumulative frequencies
double total_frequency = 0.0;

// Input class intervals and their frequencies


printf("Enter the lower and upper limits of each class interval along with their frequencies:\n");
for (int i = 0; i < n; i++) {
printf("Class interval %d (lower limit and upper limit): ", i + 1);
scanf("%lf %lf", &lower_limit[i], &upper_limit[i]);
printf("Frequency f[%d]: ", i + 1);
scanf("%lf", &f[i]);

total_frequency += f[i]; // Calculate total frequency


}

// Calculate cumulative frequencies


cumulative_frequency[0] = f[0];
for (int i = 1; i < n; i++) {
cumulative_frequency[i] = cumulative_frequency[i - 1] + f[i];
}

// Finding the median


double median;
double median_position = total_frequency / 2;

// Determine the median class


int median_class_index = -1;
for (int i = 0; i < n; i++) {
if (cumulative_frequency[i] >= median_position) {
median_class_index = i;
break;
}
}

// If median class found


if (median_class_index != -1) {
double L = lower_limit[median_class_index]; // Lower limit of the median class
double f_m = f[median_class_index]; // Frequency of the median class
double cumulative_f = median_class_index > 0 ? cumulative_frequency[median_class_index - 1] : 0;
// Cumulative frequency of the class before median class
double h = upper_limit[median_class_index] - lower_limit[median_class_index]; // Width of the
median class

// Calculate median
median = L + ((median_position - cumulative_f) / f_m) * h;
}

// Output the result


printf("Median: %.2lf\n", median);
return 0;
}
Write a program to find standard deviation using direct method
#include <stdio.h>
#include <math.h>

int main() {
int n;
printf("Enter the number of data points: ");
scanf("%d", &n);

double data[n];
double sum = 0.0, mean, sum_squared_diff = 0.0, stddev;

// Input data points


printf("Enter the data points:\n");
for (int i = 0; i < n; i++) {
printf("Data point %d: ", i + 1);
scanf("%lf", &data[i]);
sum += data[i]; // Calculate sum
}

// Calculate mean
mean = sum / n;

// Calculate the sum of squared differences from the mean


for (int i = 0; i < n; i++) {
sum_squared_diff += (data[i] - mean) * (data[i] - mean);
}

// Calculate standard deviation


stddev = sqrt(sum_squared_diff / n); // Population standard deviation

// Output the results


printf("Mean: %.2lf\n", mean);
printf("Standard Deviation: %.2lf\n", stddev);

return 0;
}
Write a program to find standard deviation of discrete series
#include <stdio.h>
#include <math.h>

int main() {
int n;
printf("Enter the number of distinct data points: ");
scanf("%d", &n);

double x[n], f[n]; // Arrays to hold values and their frequencies


double sum_fx = 0.0, sum_f = 0.0, mean, sum_squared_diff = 0.0, stddev;

// Input data points and their frequencies


printf("Enter the values (x) and their frequencies (f):\n");
for (int i = 0; i < n; i++) {
printf("Value x[%d]: ", i + 1);
scanf("%lf", &x[i]);
printf("Frequency f[%d]: ", i + 1);
scanf("%lf", &f[i]);

sum_fx += x[i] * f[i]; // Sum of x * f


sum_f += f[i]; // Sum of frequencies
}

// Calculate mean
mean = sum_fx / sum_f;

// Calculate the sum of squared differences from the mean


for (int i = 0; i < n; i++) {
sum_squared_diff += f[i] * (x[i] - mean) * (x[i] - mean);
}

// Calculate standard deviation


stddev = sqrt(sum_squared_diff / sum_f); // Population standard deviation

// Output the results


printf("Mean: %.2lf\n", mean);
printf("Standard Deviation: %.2lf\n", stddev);

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