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
|
#include<iostream>
#include<iomanip>
#include <cmath>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<conio.h>
using namespace std;
//Prototypes
double Mean (int [], int, double &, double &);
double standardDeviation (int [], int, double &, double &, double &, double &);
int findMedian (int [ ] , int, double&);
void Sort(int * , int);
int main() //Main Function
{
int ar[100],n,i,min=0,max=0; //Initializing variables
double avg=0.0,median=0.0, sum, sum2, sD;
printf( "How many numbers do you want to use? "); //Gets the size of the array
scanf("%d",&n);
printf("\nPlease enter each number and press the enter key. \n"); //Gets the contents of the array
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
min=ar[0]; //Traverses through the array and finds the smallest number
for(i=0;i<n;i++)
{
if(ar[i]<=min)
{
min=ar[i];
}
}
max=ar[0]; //Traverses through the array and finds the largest number
for(i=0;i<n;i++)
{
if(ar[i]>=max)
{
max=ar[i];
}
}
printf("\n\t The smallest number is: %d",min);
printf("\n\t The largest number is: %d",max);
Mean(ar, n, sum, avg);
printf("\n\t The mean is: %lf",avg);
sD = standardDeviation(ar,n,sum,sum2,avg,sD);
printf("\n\t The standard deviation is: %lf",sD);
findMedian(ar, n, median);
printf("\n\t The median of your unsorted list is: %lf",median);
Sort(ar, n);
printf("\n\t The numbers listed in ascending order are: ");
for(i=0;i<n;i++)
{
printf("%d", ar[i]);
printf(" ");
}
getch();
}
double Mean (int ar[], int n, double &sum, double &avg)
{
for(int i=0;i<n;i++) //Finds the mean of the array
{
sum= sum+ ar[i];
avg= sum/ float(n);
}
return avg;
}
double standardDeviation (int ar[], int n, double &sum, double &sum2, double &avg, double &sD)
{
for(int i=0;i<n;i++) //Finds the standard deviation of the array
{
sum= sum+ ar[i];
}
avg= sum/ float(n);
for ( int i = 0; i <=n; i++ )
{
sum2 += pow(ar[i]-avg,2);
}
sD= sqrt(sum2/(n-1));
return sD;
}
int findMedian(int ar[ ] , int n, double &median)
{
if (n%2==1)
{ // if there is an odd number of elements I want n/2
median = ar[n/2];
return median;
}
else
median = ( ar[n/2] + ar[n/2 - 1] ) / 2;
return median; //otherwise I want the average of n/2 -1 and n/2
}
void Sort(int *array, int n)
{ //Sorts the array in ascending order
bool swapped = true;
int j = 0;
int tmp;
while (swapped)
{
swapped = false;
j++;
for (int i = 0; i < n - j; i++)
{
if (array[i] > array[i + 1])
{
tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
swapped = true;
}
}
}
}
|