Just Started C in University, first time doing it and the first program i had to write was one that required someone to be able to enter 5 random numbers and get the average of the 5 numbers, the highest/lowest of the 5 numbers and then finally the standard deviation of the 5 numbers.

i managed to get the mean/highest/lowest working but cant seem to get the standard deviation to work, any help is greatly appretiated, but remember im still a noob at C :)

Here is my code:

``````#include#include <stdio.h>
#include <cmath>

void main()
{
int x;                     //this is the counter
int myarray[5];
int deviationmean[5];
int highestvalue=0;
int nothighest=0;
int lowestvalue;
int notlowest;
int total;
int mean;
int standardtotal=0;
int z=0;
float standarddeviation=0;

for (x=0;x<5;x++)
{
printf("Enter number %d: ",x);
scanf("%d",&myarray[x]);                       //Enter Numbers

}

total=myarray[0]+myarray[1]+myarray[2]+myarray[3]+myarray[4];
mean=total/5;

printf("\n\nThe Average of the given numbers is %d", mean);          //This is the Average

for (x=0;x<5;x++)
{
if(myarray[x]>highestvalue)
{
highestvalue=myarray[x];                       //Highest Number
}
else
{
nothighest=myarray[x];
}
}

printf("\nThe Highest Value is %d", highestvalue);

lowestvalue=highestvalue;

for (x=0;x<5;x++)
{
if(myarray[x]<lowestvalue)
{
lowestvalue=myarray[x];                                //Lowest Number
}
else
{
notlowest=myarray[x];
}
}

printf("\nThe Lowest Value is %d\n", lowestvalue);

for(x=0;x<5;x++)
{
deviationmean[x]=myarray[x]-mean;
}

standardtotal=deviationmean[0]+deviationmean[1]+deviationmean[2]+deviationmean[3]+deviationmean[4]+deviationmean[5];
z=standardtotal/5;
z=(float)z;

if(z<0)
{
standarddeviation=sqrt((float)-z);
}
else
{
standarddeviation=sqrt((float)z);
}

printf("The Standard Deviation is %f\n\n", standarddeviation);

}``````

Just Started C in University, first time doing it and the first program i had to write was one that required someone to be able to enter 5 random numbers and get the average of the 5 numbers, the highest/lowest of the 5 numbers and then finally the standard deviation …

## All 2 Replies

Just Started C in University, first time doing it and the first program i had to write was one that required someone to be able to enter 5 random numbers and get the average of the 5 numbers, the highest/lowest of the 5 numbers and then finally the standard deviation of the 5 numbers.

Ok. I think this could work, but I havent done statistics in a while.

``````#include<stdio.h>
#include<math.h>
#define MIN 0
#define MAX 5
int main(void)
{
int i;
int biggest,smallest;
int stdtot;
int sum;
float mean;
float sigma;
int array[MAX];   //it has 5 elements

for(i=sum=MIN;i<MAX;i++)
{
printf("Enter %d number : ",i+1);
scanf("%d",&array[i]);
sum+=array[i];          //lets sum it up ASAP
}

mean=sum/MAX;      //calculate mean via formula

for(i=MIN;i<MAX;i++)
{
if(i==MIN)
{
biggest=array[i];     //lets assume that the first array element the biggest
smallest=array[i];    //and also the smallest
}
else if(biggest<array[i])
{
biggest=array[i];
}
else if(smallest>array[i])
{
smallest=array[i];
}
}

for(i=stdtot=MIN;i<MAX;i++)
{
stdtot+=pow((array[i]-mean),2);    //compute the difference of each array element
}
//from the mean, and square the result to lose negative nums
printf("\n%hd",stdtot);
sigma=sqrt(stdtot/MAX);            //calculate standard deviation sigma
printf("\nMean is : %.2f\n"
"Biggest is : %hd\n"
"Smallest is %hd\n"
"Standard deviation is %.2f",
mean,biggest,smallest,sigma);

return 0;
}``````

i think i sorted it now, thanks for the help :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.