The line
char* filearray = malloc(sizeof(float) * (159*360));
is wrong, for several reasons:
In that line, you are allocating space for 159*360 elements of type float. However, you are assigning that space to a char *. This does not make sense. If every element in the 2D array should be a float, then you should write this instead:
float (*filearray)[159] = malloc( sizeof(float) * (360*159) );
In the line above, it is necessary to write float (*filearray)[159] instead of float *filearray[159], because the latter declares an array of 159 elements in which every element is a float*, whereas the former declares one pointer to an array of 159 elements in which every element is a float.
Also, the number 159 seems wrong. If latitudes are numbers between -90 and +90 inclusive, then there are 181 possible numbers, so you should be using the number 181 instead of 159. For the same reason, you should probably be using the number 361 instead of 360 for the longitude.
However, later in your program, you use the line:
sprintf(filearray[i][j],"%s",buf);
This implies that you don't want every element of the 2D array to be a float, because you can't sprintf to a float. Instead, it appears that you want every element to be a string.
If you want every element of the 2D array to be a string, then you must allocate sufficient space for every string. Simply allocating sizeof(float) bytes for every string will not be sufficient.
Since the two numbers that follow the longitude and latitude in every line seem to be integers, it may be appropriate to make every element of the 2D array store these two numbers as a pair of int, instead of using strings. You could define the following type:
struct pair
{
int number1;
int number2;
};
You could then define your 2D array like this:
struct pair (*filearray)[181] = malloc( sizeof(struct pair) * (361*181) );
It is also worth noting that it is good programming practice to call free for every malloc call when you no longer need the memory (in this case at the end of your program).
At the time of this writing, you have not yet responded to my request for clarification on whether it is guaranteed that every possible longitude/latitude combination will appear exactly once, and whether it will appear in-order. Therefore, I will assume that this is not guaranteed.
The following program will read one line at a time from the file and add it to the appropriate position in the 2D array, unless that longitude/latitude combination has already been encountered in the file, in which case it will print an error message instead. When it has finished reading the input, it will output the entire array contents, skipping longitude/latitude combinations that were not encountered in the input file (otherwise it would print 361*181 lines of output).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct pair
{
//false if this longitude/latitude combination has not yet
//been found
bool found;
int number1;
int number2;
};
int main( void )
{
struct pair (*filearray)[181];
char line[100];
//allocate memory for 2D array
//this uses calloc instead of malloc, so that the memory
//is initialized to 0, so that the struct member "valid"
//is initialized to false
filearray = calloc( 361, sizeof *filearray );
if ( filearray == NULL )
{
fprintf( stderr, "allocation error!\n" );
exit( EXIT_FAILURE );
}
//read one line of input per loop iteration
while ( fgets( line, sizeof line, stdin ) != NULL )
{
int longitude, latitude, number1, number2;
//longitude and latitude converted to a valid positive index
int longitude_index, latitude_index;
//verify that an entire line was read
if ( strchr( line, '\n' ) == NULL && !feof( stdin ) )
{
fprintf( stderr, "line too long!\n" );
exit( EXIT_FAILURE );
}
//attempt to parse line
if (
sscanf(
line,
"%d %d %d %d",
&longitude, &latitude, &number1, &number2
)
!= 4
)
{
printf( "skipping line due to invalid input!\n" );
continue;
}
//verify that longitude is in proper range
if ( ! ( -180 <= longitude && longitude <= +180 ) )
{
printf( "skipping line due to invalid longitude!\n" );
continue;
}
//verify that latitude is in proper range
if ( ! ( -90 <= latitude && latitude <= +90 ) )
{
printf( "skipping line due to invalid latitude!\n" );
continue;
}
//convert longitude and latitude to a valid positive index
//in the appropriate range, because negative indexes are
//not permitted in C
longitude_index = longitude + 180;
latitude_index = latitude + 90;
//verify that longitude/latitude combination has not already
//been encountered
if ( filearray[longitude_index][latitude_index].found )
{
printf(
"combination %d %d has been encountered multiple times!\n",
longitude, latitude
);
continue;
}
//input is ok, so write pair into 2D array
filearray[longitude_index][latitude_index].found = true;
filearray[longitude_index][latitude_index].number1 = number1;
filearray[longitude_index][latitude_index].number2 = number2;
}
//print valid fields of 2D array
for ( int i = 0; i < 361; i++ )
for ( int j = 0; j < 181; j++ )
if ( filearray[i][j].found )
printf(
"long %d, lat %d : %d %d\n",
i - 180,
j - 90,
filearray[i][j].number1,
filearray[i][j].number2
);
free( filearray );
}
When running this program with the input
-180 -90 56 67
-179 -90 56 99
-178 -90 56 99
180 -90 45 45
-180 -89 45 45
which was taken from the question, this program has the following output:
long -180, lat -90 : 56 67
long -180, lat -89 : 45 45
long -179, lat -90 : 56 99
long -178, lat -90 : 56 99
long 180, lat -90 : 45 45
filearrayis achar *. The compiler is telling you thatfilearray[i][j]is not valid.FILE FILEOUT[150];?