# Thread: Trying to figure what I'm doing wrong in this program

1. ## 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");
}```

2. You need to give calculateValues() and printdisplay() access to the array. You can pass the array as a parameter or define it globally.

3. Code:
```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]);
}```
You're reading in all the values correctly, but then you're returning a[5], which: 1) Is out of bounds of the array, and 2) Even if it were in bounds, it would only be a single number. You want to return the entire array instead.

The easiest way to do this (unless you want to do dynamic memory allocation, which you don't), is to declare an array in main() and then pass the array to your getData() function which would then fill it. You don't even need a return value. The prototype would look like this: void getData(int numbers[]);

4. ## New code

Rewrote the program, now I keep getting invalid lvalue in assignment on line 162 and 163. Also get a error that says 'for' loop initial declaration used outside of C99 mode.

Code:
```#include <stdio.h>
#define size 10
float x[size];
#define SENTINEL -1.0

int getData(float x[], int arraysize);
float computeSum(int n, float x[]);
float computeMean(int n, float sum);
float computeMedian(int n, float x[]);

void sort(float x[], int n);
void swap(float * a, float * b);

int main()

{

int n;
float sum;
float mean;
float median;
int findixsmall;

n=getData(x, size);
sum=computeSum(n, x);
mean=computeMean(n, sum);
median=computeMedian(n, x);
findixsmall=computeMedian(n, x, start);

for (int a=0;a<n;a++)
printf("%f\n", x[a]);

}

int getData(float x[], int arraysize)

{

float item;  // Temp cell for each item read
int counter;    // Count of number of items read

if (arraysize <= 0) return (-2);

counter = 0;
// Execute the "priming" or initial read
printf("Enter the first data item: ");
scanf ("%f", &item);
// Repeat until no more data.
while (item != SENTINEL)
{
x[counter] = item;
counter++;
if (counter >= arraysize) return (0); // array full
// Execute the "update" or next read
printf("Enter the next data item: ");
scanf ("%f", &item);
}
if (counter == 0) return (-1); // file empty
else return (counter);
} // end getData

float computeSum(int n, float x[])
{
int sum;

sum = 0;
for (int i = 0; i < n; i++)

{

sum = sum + x[i];

}

return(sum);
}

float computeMean(int n, float sum)

{
float mean;

mean = sum/n;

return (mean);
}

//Find the index of the smallest number in an array x
//between x[start] and x[n-1].

float computeMedian(int n, float x[])
{

void sort(float x[], int n);
{

int ix//index of smallest item

for(i=0; i<n; i++)

{
ix=findixsmall(x, i, n);
swap(&x[ix], &x[i];
}

}

ix=findixsmall(x ,start + 1 , n);

int findixsmall(int n,    //IN: the number of items to be compared
float x[], //IN: array to be searched
int start) //IN: starting search point

{
int n

for(i=start + 1; x<n; i++)

{
if(x[ixsmall]< x[i])
ixsmall=i;
}

return ixsmall;
}

int i;
int ixsmall; //index of smallest item

ixsmall=0; //start

swap(& x[ix], & x[0]);

swap(x[start], & x[ixsmall])

void swap
(float * a, float * b)
{
float temp;
temp= & a;
& a= & b;
& b= temp;

} //end swap

}//end computeMedian```

5. I don't see line numbers but I think it's probably complaining about this part... It should be:
Code:
```        temp= *a;
*a = *b;
*b = temp;```

6. 1) You cannot nest functions inside functions.

2) Don't be giving us line numbers unless they're included in your post... Highlight the offending lines with color or boldface... I'm not going to sit here counting 160 lines, I have far better ways of wasting my time.

7. Thanks nonoob and CommonTater