Trying to figure what I'm doing wrong in this program
I trying to do a program that allows you to enter 5 numbers in an array. Then it takes the values of the array calculates the sum, average, and median of the values. Then shows the values you enter into the code and display the results.
In the getData function my for loop seem correct with enter the value this is place into the index memory slots from 0 to 4. Then it returns the values received it the array. The calculateValues function then takes the value then adds the sums of all the index values. Then that value is divided by the number of index slots in the array. Haven't figure out the median yet but when I print the results of the sum and average my values are not correct.
For example my result will look like this:
Code:
Enter the array elements
25
36
21
23
25
Index Item
0 32 à ■ 0.00
1 32 à ■ 0.00
2 32 à ■ 0.00
3 32 à ■ 0.00
4 32 à ■ 0.00
sum of array elements === 1077477376
average of array elements === 0.000000
The median number is 0.000000
Press any key to continue . . .
Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int number, values, print;
number = getData();
values = calculateValues();
print = printdisplay();
}
// Get Data values
int getData()
{
int i, sum = 0, n;
float avg;
int a[5];
printf("Enter the array elements\n");
for (i = 0; i < 5; i++)
{
if (scanf("%d", &a[i]) != 1)
break;
}
return(a[i]);
}
int calculateValues()
{
double median = 0;
float avg;
int n;
int sum;
int i;
int a[5];
n = i;
for (i = 0; i < 5; i++)
{
// Compute the sum of array
sum=sum+a[i];
// Compute the average of array
avg=(float)sum/i;
median = a[i];
}
return 0;
}
int printdisplay()
{
double median = 0;
int sum;
float avg;
int i;
// Display the array
printf(" Index Item \n");
for (i = 0; i < 5; i++)
printf("%3d%4d%9.2c%5c%9.2f\n", i, ' ');
// Display the results of the array
printf("sum of array elements === %d\n", sum);
printf("average of array elements === %f\n", avg);
printf("The median number is %f\n", median);
system("Pause");
}