I wouldn't store it all in one single array. First of all, worry about reading the number of cities. After you know that, you can allocate 2 arrays: one of them holds a structure with the coordinates for each city, and the other is a 2D array where you store the costs.
This assumes that you look at the cities and count them: in your example, the city with coordinates 1.3 4.2 will be city 0 (stored in position 0 of the array); the city with 1.6 -3.5 will be on position 1, etc. So, basically you'll be doing:
- Read number of cities, x;
- Read whether it's euclidean or not, store that in some variable;
- Allocate an array cities of x elements, and another
bi-dimensional array costs of x by x;
- Read each city's coordinates, and store them in cities[i] (cities
shall be an array of a structure with 2 floats to store the
coordinates);
- For each line i (i starting on 0) and for each column j in that line
(also starting on 0), set cost[i][j] to whatever value is in the
input.
Here's the code that implements this approach:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 32
struct city {
float c1;
float c2;
};
int main(void) {
int citiesNo, i, j;
struct city *cities;
float **cost;
char line[BUFSIZE], euclidean[BUFSIZE];
fgets(line, BUFSIZE, stdin);
citiesNo = atoi(line);
fgets(line, BUFSIZE, stdin);
strcpy(euclidean, line);
cities = malloc(sizeof(struct city)*citiesNo);
cost = malloc(sizeof(float *)*citiesNo);
for (i = 0; i < citiesNo; i++)
cost[i] = malloc(sizeof(float)*citiesNo);
/* Read coordinates */
for (i = 0; i < citiesNo; i++)
scanf("%f %f", &(cities[i].c1), &(cities[i].c2));
/* Read costs */
for (i = 0; i < citiesNo; i++)
for (j = 0; j < citiesNo; j++)
scanf("%f", &(cost[i][j]));
/* Everything is stored now... */
return 0;
}
fgets was used in the beginning because scanf("%d", &citiesNo) would leave a newline in the buffer, and the subsequent fgets() call would return an empty line. If you prefer, you can replace both fgets() by scanf(), I just didn't read the euclidean string with scanf because it makes no buffer size checking. If your input is always well formed, this is not a problem.
After this code runs, you have a string with "euclidean" or "not euclidean" stored in the variable euclidean.