Hi guys
I'm a junior student in IT. I am facing a problem with the output of the program. The idea of the program is that I should use functions to read an array of 10 elements, then get the average of the elements, then get the the max and min. I've got the max and min right but the average is showing weird stuff. Please check the code and tell me what should I do or help me in some way or another.
The out put is (notice that is requesting for 11 numbers instead of 10 and if I change the loop parameters to let it take only 10 then it's showing weird stuff
enter the group of integers numbers
1
2
3
4
5
6
7
8
9
0
9
1 2 3 4 5 6 7 8 9 0the avg is 3.500000
9
1Press any key to continue . . .
// func-sortarray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define size 10
void readarray(int []);
void average(int []);
void printArray(int []);
void max(int []);
void min(int []);
int _tmain(int argc, _TCHAR* argv[])
{
int sarray[size];
readarray(sarray);
printArray(sarray);
average(sarray);
max(sarray);
min(sarray);
return 0;
}
void readarray(int a[])
{
printf("enter the group of integers numbers\n");
for (int i=0; i<=size-1 ;i++)
scanf("%d\n",&a[i]);
}
void average(int a[])
{
int i;
double avg;
double total = 0;
for (i=0; i <= size-1; i++)
{
total = total + a[i];
}
avg = total /size-1;
printf("the avg is %f\n",avg);
}
void printArray(int a[])
{
int j;
for (j = 0; j <= size - 1; j++)
printf( "%2d", a[ j ]);
}
void max(int a[])
{
int ma =a[0];
for (int j=0;j<size-1;j++)
{
if (ma<a[j])
ma=a[j];
}
printf("%d",ma);
}
void min(int a[])
{
int mi =a[0];
for (int j=0;j<size-1;j++)
{
if (mi>a[j])
mi=a[j];
}
printf("\n%d",mi);
}
thanx in advance