Hiya im writing a program that reads 20 floats into an array then sorts
them using bubble sort and prints them out in assending order.
But I am getting an error that says variable 'i' is undecared. ( I have marked the line with the error ). I delcared 'i' before the program inside main so I do not understand why it doesnt see that I have delcared it.
Code:
#include <stdio.h>
#define ARRAY_SIZE 21
/*main function - begins program execution -----------------------------------*/
int main ( void )
{
/*declare array and size*/
float data[ ARRAY_SIZE ];
int temp;
int i;
printf("Enter 20 floats: ");
for ( i = 0; i < ARRAY_SIZE; i++ )
{
scanf("%f", &data[ i ]);
}
/*sort array with bubble sort*/
for ( i = 0; i < ARRAY_SIZE -1; i++ )
{
for ( j = 0; j < ARRAY_SIZE -1 -i; j++ ) // line with error
if ( data[ j + 1 ] < data[ j ] )
{
int tmp = data[ j ];
data[ j ] = data[ j +1 ];
data[ j + 1 ] = tmp;
}
}
/*display sorted array*/
for ( i = 0; i < ARRAY_SIZE; i++ )
{
printf("%.2f", data[ i ]);
}
getchar(); /*freeze console output window*/
return 0; /*return value from int main*/
}