1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define file_name "my_data.txt"
/************************** MESSAGE FUNCTION ****************************/
void
msg1(void)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
printf("\n The function of this program is to compute\n" );
printf("\n the mean and standard deviation of a data set.\n");
printf("\n Data file [not taken from keyboard], upper \n");
printf("\n bound, lower bound. Mean of data, standard \n");
printf("\n deviation (if 30 or more data points) \n");
printf("\n or sample standard deviation (if fewer than 30)\n ");
printf("\n data points. \n\n\n\n\n\n\n\n ");
}
/* SOMETIMES YOU WILL PUT OTHER FUNCTIONS HERE */
/*************************** MAIN FUNCTION *******************************/
int
main(void)
{
/********************* DECLARATION OF VARIABLES **************************/
double sum=0, mean, sdev, loop=1,values[1000],count=(0),upper,lower;
double limitu,limitl,lim=0, std=0,input;
double dat=0;
int SIZE, k, n, x=0;
FILE *data;
data=fopen (file_name,"r");
char unit,u,ans='Y';
/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/
/******* Print the Opening Message *******/
msg1();
/******* Computate ************/
while(ans=='Y' || ans=='y')
{
if(data==NULL)
{
printf("Error opening file.\n\n");
}
else
{
while((fscanf(data,"%lf",&values[x]))==1)
{
input=values[x];
x++;
sum=sum+input;
count++;
loop++;
}
}
printf("\n Please enter your upper bound.\n\n");
scanf("%lf",&upper);
printf("\n Please enter your lower bound.\n\n");
scanf("%lf",&lower);
/*mean*/
loop--;
x=0;
mean=(sum/loop);
printf("\n You have a total of %0.0f inputs.\n\n",loop);
printf("\n Your mean value is %0.3f in %c units.\n\n",mean,unit,u);
/** Standard Deviation Calculations **/
if (count>=30)
{
while (count>x)
{
std=std+(values[x]-mean)*(values[x]-mean);
x++;
}
std=sqrt(std/count);
printf("\nStandard Deviation equals +/- %f.\n\n",std,unit,u);
}
/** Sample Standard Deviation Calculations **/
if (count<30)
{
while (count>x)
{
std=std+(values[x]-mean)*(values[x]-mean);
x++;
}
std=sqrt(std/(count-1));
printf("\nThe Sample Standard Deviation equals %f.\n\n",std,unit,u);
}
limitu=mean+3*std;
limitl=mean-3*std;
if(limitu>upper)
{
printf("\nThe upper bound is exceeded.\n\n");
lim++;
}
if(limitl<lower)
{
printf("\nThe lower bound is exceeded.\n\n");
lim++;
}
if(lim==0)
{
printf("\nThe data fits within the bounds.\n\n");
}
printf("\nWould you like to perform another calculation with new bounds? (Y/N)\n\n");
printf("\nNote: Any input other than 'Y' or 'y' will end the program.\n\n");
scanf("%c %c",&ans,&ans);
}
return 0;
}
|