Group Group Group Group Group Group Group Group Group

Return a 2D Array from a Func

Can anyone let me know what i can do here to return a 2D array? From this below i get: Consecutive declarations on a line must be separated by ‘;’ Thank you

func findLocationsForEachRange() -> [[Double]] {
//var myCount = myLocations.count
var locationsVisitedLatLong: [[Double]] = []
for myLocation in myLocations {
if (myLocation.date! >= WeeklySpan(increment: myValue)) {
locationsVisitedLatLong.append(contentsOf: [[myLocation.latitude, myLocation.longitude]])
}
}
}return locationsVistedLatLong[[Double]]

Hi @flmcl,
You already have the result array locationsVisitedLatLong as the outer array of type [[Double]]

Now, when you are appending, you need to append to this array only what you need, which is an array of Doubles, not an Array of Array of Doubles.

so, change your append to

locationsVisitedLatLong.append([myLocation.latitude, myLocation.longitude])

However, a small tip, I don’t know the reason for you using this particular structure, lat lon generally come in pairs, so instead if you use a tuple or a struct, you can have a simple array of lat/longs and manage them easier.

Cheers,

1 Like

In C, pointers and arrays are closely related. Also, you usually need to pass the size of an array as a separate variable. Let’s start you with:

#include <stdio.h>

float** createArray(int m, int n)
{
float* values = calloc(mn, sizeof(float));
float** rows = malloc(n
sizeof(float*));
for (int i=0; i<n; ++i)
{
rows[i] = values + i*m;
}
return rows;
}

void destroyArray(float** arr)
{
free(*arr);
free(arr);
}

void drawLine(const float** coords, int m, int n);

int main(void)
{
float** arr = createArray(2,2);
arr[0][0] = 1;
arr[0][1] = 1;
arr[1][0] = 2;
arr[1][1] = 2;
drawLine(arr, 2, 2);
destroyArray(arr);
}

1 Like