1 / 5

Array Examples

Array Examples. Statistical Analysis. Statistical Analysis. Read age statistics of people in a small building (which is in a file called Ages.txt) and calculate: x m , Mean x min , Youngest x max , Oldest S, Standard Deviation. Statistics. fp = fopen ("Ages.txt", "r");

Download Presentation

Array Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array Examples Statistical Analysis

  2. Statistical Analysis • Read age statistics of people in a small building (which is in a file called Ages.txt) and calculate: • xm, Mean • xmin, Youngest • xmax, Oldest • S, Standard Deviation

  3. Statistics • fp = fopen("Ages.txt", "r"); • Sz = FillAry(fp, Ages); • PrntAry(Ages, Sz); • printf("The Minimum is\t:%3d\n", FindMax(Ages, Sz)); • printf("The Maximum is\t:%3d\n", FindMin(Ages, Sz)); • printf("The Average is\t:%6.2f\n", FindAve(Ages, Sz)); • printf("The Std. Dev is\t:%6.2f\n", FindStd(Ages, Sz)); • fclose(fp); • return(0); • } • #include <stdio.h> • #include <math.h> • intFillAry(FILE *flp, intarr[]); • void PrntAry(intarr[], int n); • intFindMax(intarr[], int n); • intFindMin(intarr[], int n); • double FindAve(intarr[], int n); • double FindStd(intarr[], int n); • int main(void) { • FILE*fp; • intAges[100]; • intSz;

  4. Statistics • intFindMax(intarr[], int n) { • inti, max; • max = arr[0]; • for(i=1; i<n; i++) • if(arr[i] > max) max=arr[i]; • return(max); • } • int FindMin(int arr[], int n) { • inti, min; • min = gr[0]; • for(i=1; i<n; i++) • if(arr[i] < min) min=arr[i]; • return(min); • } • intFillAry(FILE *flp, intarr[]) { • int Status, i=0; • Status = fscanf(flp, "%d", &arr[i]); • while(Status != EOF) { • i++; • Status = fscanf(flp, "%d", &arr[i]); • } • return(i); • } • void PrntAry(intarr[], int n) { • inti; • for(i=0; i<n; i++) • printf("%d\n", arr[i]); • }

  5. Statistics • double FindAve(intarr[], int n) { • double ave = 0.0; • inti; • for(i=0; i<n; i++) ave+=arr[i]; • ave=ave/n; • return(ave); • } • double FindStd(intarr[], int n) { • double Sdev = 0.0; • double ave; • inti; • ave = FindAve(arr, n); • for(i=0; i<n; i++) • Sdev += pow((arr[i] - ave), 2); • Sdev = sqrt(Sdev/(n-1)); • return(Sdev); • }

More Related