Code:
Descrition: Implement a menu managing array data such that the program asks multiple questions to the user and executes the operation using the method he chooses.
*/
#include <stdio.h>
#include <math.h>
/*function definitions*/
double sumData (double x[], int n);//returns the sum of the given set.
double medianData (double x[], int n);//returns the median of the given set.
double stdData (double x[], int n);// returns the standad derivation of the given set.
double rangeData (double x[], int n);//returns the range of the given set.
void freqData (double x[], int n);//displays the frequency distribution in the given ranges.
double meanData(double x[], int n);//returns the mean of the data set.
void setData(double x[], int n);// returns the array as a whole.
void bubble_sort(double x[], int n);//to search the array.
int main()
{
int size=100;
double array[size];
int i, x, c;
printf ("Please enter the size of you array:");
scanf ("%d", &size);
for (i=0; i<size; i++) {
printf("Enter element %d of the array:", i);
scanf("%lf", &array[i]);
}
printf("Please select a number among the following operations:\n");
printf("1.Display the data set\n2.Calculate the mean and");
printf("sum of the data set\n3.Calculate the median of the data set\n");
printf("4.Calculate the standard deviation of the data set\n5.");
printf("Find range of the data set\n6.Display a frequency distribution\n");
printf("7.Quit\n");
scanf("%d", &c);
while (c<1 || c>7) {
printf("\aYou have entered an ivalid number; try again.\n");
printf("Please select a number among the following operations:\n");
printf("1.Display the data set\n2.Calculate the mean and");
printf("sum of the data set\n3.Calculate the median of the data set\n");
printf("4.Calculate the standard deviation of the data set\n5.");
printf("Find range of the data set\n6.Display a frequency distribution\n");
printf("7.Quit\n");
scanf("%d", &c);
}
if (c==1) {
setData (array, size); //call the function to display the array.
}
else if (c==2){// displays both the mean and sum.
meanData(array, size);
sumData(array, size);
printf("mean of the data set:%f\n the sum is:%f\n", meanData(array, size), sumData(array, size));
}
else if (c==3){//displays median
medianData(array, size);
printf("median of the data set: %f\n", medianData(array, size));
}
else if (c==4){
stdData(array, size); //to display the deviation.
printf("The standard deviation of the data set:%f\n", stdData(array, size));
}
else if (c==5){// Printing the range on the screen.
rangeData(array, size);
printf("The range of this data set is:%f\n", rangeData(array, size));
}
else if (c==6){//returning the frequency in interval ranges.
freqData(array, size);
}
else if (c==7){//Quit the program.
printf("The program ends here.\n");
}
return 0;
}
/*function definitions*/
void setData (double x[], int n)/* function to display the array pr set data*/
{
int i;
for (i=0; i<n; i++){
printf("%f ", x[i]);
}
}
double meanData( double x[], int n)/* function to calculate the mean*/
{
int i;
double sum =0;
for (i=0; i<n; i++){
sum = sum + x[i];
}
return (sum/n);
}
double sumData( double x[], int n)/*function to calculate the sum*/
{
int i;
double sum = 0;
for (i=0 ;i<n; i++){
sum = sum + x[i];
}
return (sum);
}
void bubble_sort( double x[], int n)/* function to sort data in increasing order */
{
int i,j;
double temp;
for (i=0; i<n-1; i++){
for (j=i+1; j<n; j++){
if ( x[i] > x[j]){
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
}
double medianData (double x[], int n) /*function to calculate the median of the data set */
{
double Middle;
int h1, h2;
bubble_sort(x,n);
if (n%2 == 0){/*if n is even */
h1 = n/2;
h2 = (n/2) - 1;
Middle = ((x[h1] + x[h2])/2);
}
else { //if n is odd
Middle = x[n/2];
}
return ( Middle );
}
double stdData (double x[], int n)/* function to find the standard deviation */
{
int i;
double sum = 0, sum1 = 0;
double sum2, std;
for (i=0; i<n; i++){
sum1 = sum1 + (x[i]*x[i]);
sum = sum + x[i];
}
sum2 = (sum * sum)/(n*n);
std = sqrt((sum1/n) - sum2);
return (std);
}
double rangeData(double x[], int n)/* calculting the range of the data set */
{
int i;
double max, min, range;
max = x[0];
min = x[0];
for (i = 0; i<n; i++){
if( x[i] < min){//searching for the minimum element
min = x[i];
}
if (x[i] > max){//searching for the maximum element
max = x[i];
}
}
range = max - min;//calculating the range
return (range);
}
void freqData (double x[], int n)/*displaying the frequency */
{
int i;
int freq1 = 0;
int freq2 = 0;
int freq3 = 0;
int freq4 = 0;
bubble_sort(x,n);/*sorting the array values */
for (i=0; i<n; i++){
if (x[i] < 0){
freq1 = freq1 + 1;
}
else if (x[i]>= 0 && x[i]<10){
freq2 = freq2 + 1;
}
else if (x[i]>= 10 && x[i]<100){
freq3 = freq3 + 1;
}
else {
freq4 = freq4 +1;
}
}//displaying ranges.
printf ("%s%13s\n", "Interval", "Frequency" );
printf ("(-inf , 0)%13d\n", freq1);
printf ("[0, 10)%10d\n", freq2);
printf ("[10, 100)%9d\n", freq3);
printf ("[100,+inf )%11d\n", freq4);
}