RE: [All Languages] Recursion Challenge 03-30-2014, 06:43 PM
#2
I don't think you know what a 2D array is... This is a jagged array, not a 2D array:
Java actually doesn't even support *true* 2D arrays, so a jagged array is all you've got.
And if this is a multi-language challenge, you shouldn't restrict it to a class. C doesn't have classes for instance, and same with other languages as well.
Here's my solution in C:
Output:
Code:
private double [][] view;Java actually doesn't even support *true* 2D arrays, so a jagged array is all you've got.
And if this is a multi-language challenge, you shouldn't restrict it to a class. C doesn't have classes for instance, and same with other languages as well.
Here's my solution in C:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
float arr[] =
{
0.3, 0.7, 0.8,
0.4, 1.4, 1.1,
0.2, 0.5, 0.1,
1.6, 0.6, 0.9
};
void transform_array(float **dest, float *src, size_t r, size_t rows, size_t columns)
{
if (r >= rows) return;
for (size_t i = 0; i < columns; ++i)
{
dest[r][i] = *(src + (r * columns) + (r & 1 ? columns - i - 1 : i));
}
transform_array(dest, src, r + 1, rows, columns);
}
int main()
{
// initialization
size_t rows = 4, columns = 3;
size_t len = sizeof(arr[0]);
float **ptr = malloc(rows * len);
for (size_t i = 0; i < rows; ++i)
ptr[i] = malloc(columns * len);
transform_array(ptr, &arr[0], 0, 4, 3);
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < columns; ++j)
printf("%.1f ", ptr[i][j]);
printf("\n");
free(ptr[i]);
}
}Output:
Code:
0.3 0.7 0.8
1.1 1.4 0.4
0.2 0.5 0.1
0.9 0.6 1.6
(This post was last modified: 03-30-2014, 07:18 PM by 0xDEAD10CC.)
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)