1. ## Array Program

I am hoping some one can help me out. I am trying to write a program that can read in 10 float numbers and print out their values to the screen. I also need to determine the largest, smallest, sum, average. I need to use an array. I have typed up alot of the code already, but I am having problems trying to compile what I have coded so far. Below is my code. Any input would be appreciated.

Code:
```#include <stdio.h>  // for library function: printf

// function main begins program execution
main()
{
int i;  // counter
float n[10]; // n is an array of 10 integers
float sum = 0; // sum of the array
float average, smallest, largest;

printf( "Enter 10 float numbers:\n" );
scanf( "%f", &n[10] );

// initialise elements of array to 0
for ( i = 0; i < 10; i++ )
{
n[i] = 0;  // set element at location i to 0
}  // end for

// sum of contents of array n
for ( i = 0; i < 10; i++ )
{
sum += n[i];
}  // end for

// average of contents of array n
for ( i = 0; i < 10; i++ )
{
sum / n[i];
}  // end for

// arrange array in ascending order
for ( i = 0; i < n - 1; i++ )
{
for j = 0; j < n - 1 - i; j++ )
{
inOrder( &num[j], &num[j+1] );

}  // end for

// smallest number in array n
**I have not done this as yet**

// largest numnber in array n
**I have not done this as yet**

printf( "The sum of array n is %f\n", sum );
printf( "The avarage of array n is %f\n", average );
printf( "The array in ascending order is %f\n", n[10] );
printf( "The smallest number of array n is %f\n", smallest );
printf( "The largest number of array n is %f\n", largest );

return 0; // indicates successful termination
}  // end function main```

2. >>// n is an array of 10 integers
No, n is an array of floats

>>scanf( "%f", &n[10] );
Learn how to get numbers from the user

>>// initialise elements of array to 0
You read into the array from the user, then 0 out everything?? ::

>>sum / n[i];
That won't compute the average and store it. The average is total/count, or in your case sum/10.

>>for ( i = 0; i < n - 1; i++ )
"n - 1" isn't correct. Think about how many times you want to the loop to run, and where that number is going to come from

>>for j = 0; j < n - 1 - i; j++ )
Your compiler will tell you what's wrong with this one (there are three things).

Write and compile your program in small steps to ensure each part works.

I am still having trouble - I read the article that you provided the link for and I have been working on this for quite some time now (about 6 hours since I got the reply - but in that time I have done some research and more reading). I have altered my code but I still have a problem - I cannot figure out what is wrong with the line:

Code:
```
// arrange array in ascending order
for ( int i = 0; i < n; i++ )```
I get the message for this line: ISO C++ forbids comparison between (this is in Dev C++)

here is my full code I have so far now:
Code:
```#include <stdio.h>  // for library function: printf

// function main begins program execution
main()
{
int i;  // counter
int swap = 0;
float n[10]; // n is an array of floats
float sum = 0; // sum of the array
float temp, average, smallest, largest;

// initialise elements of array to 0
for ( i = 0; i < 10; i++ )
{
n[i] = 0;  // set element at location i to 0
}  // end for

printf( "Enter 10 float numbers:\n" );
scanf( "%f", &n[10] );

// sum of contents of array n
for ( i = 0; i < 10; i++ )
{
sum += n[i];
}  // end for

// average of contents of array n
for ( i = 0; i < 10; i++ )
{
sum / 10;
}  // end for

// arrange array in ascending order
for ( int i = 0; i < n; i++; )  //  second attempt
// 1st try - for ( int i = 0; i < (n - 1); i++ )
{
if ( n[i] > n[i + 1] )
{
temp = n[i];
n[i] = n[i + 1];
n[i + 1] = temp;
swap = 1;
}  // end if
while (swap !=0);

}  // end for

// smallest number in array n
//**I have not done this as yet**

// largest numnber in array n
//**I have not done this as yet**

printf( "The sum of array n is %f\n", sum );
printf( "The avarage of array n is %f\n", average );
printf( "The array in ascending order is %f\n", n[10] );
//printf( "The smallest number of array n is %f\n", smallest );
//printf( "The largest number of array n is %f\n", largest );

return 0; // indicates successful termination
}  // end function main```
Any input would be appreciated
Thankyou

4. Originally posted by emmx
I cannot figure out what is wrong with the line:

Code:
``` // arrange array in ascending order
for ( int i = 0; i < n; i++ )```
Code:
```int i;  // counter
float n[10]; // n is an array of floats```
Your complier rightly complains about trying to compare an int value with an array of floats.

There's plenty more going wrong here, though.
Code:
```   printf( "Enter 10 float numbers:\n" );
scanf( "%f", &n[10] );```
This doesn't automagically read an array for you. Use a loop.
Code:
```    // average of contents of array n
for ( i = 0; i < 10; i++ )
{
sum / 10;
}  // end for```
Ten times you calculate sum / 10 and throw the result away. But at least you got the loop condition right. (But you don't need a loop to calculate an average if you have a sum and the number of values.)
Code:
```    for ( int i = 0; i < n; i++; )  //  second attempt
// 1st try - for ( int i = 0; i < (n - 1); i++ )
{
if ( n[i] > n[i + 1] )```
Syntax error.
Either C++ or C99.
But more serious is the out-of-bounds array access, which is undefined behavior. (The first attempt was closer.)
Code:
`        while (swap !=0);`
So if swap is non-zero, just loop here forever? Google for sorting algorithms (perhaps specifically bubble sort).
Code:
`   printf( "The array in ascending order is %f\n", n[10] );`
This doesn't automagically print an array for you. Use a loop.

Other ideas: