Task: Implement a function in standard C that constructs a jagged array J from a linear array W. You must adhere to the following specifications and constraints:
Input: The function takes a single input, the array W. You can assume that global variables A, B, L, C, and NUM_PARAMS are already defined and accessible.
Output: The function returns J, a jagged array consisting of L+1 matrices of varying dimensions, as described below:
V_1: A matrix of dimensions A x B, filled with the first A*B elements from W, arranged in row-major order.
V_2 to V_L: Each is a matrix of size B x B, filled sequentially from W with B^2 elements, also in row-major order.
V_{L+1}: A matrix of dimensions B x C, filled with the last B*C elements from W, in row-major order.
Constraints:
You may not use dynamic memory allocation.
You may not use sparse arrays or maximum expected values.
Example:
If A = 4, B = 2, C = 3, L = 5, and W consists of integers from 1 to 30, then the resulting jagged array J would be structured as follows:
J = [V_1, V_2, V_3, V_4, V_5, V_6], where
J = [
[[1, 2], [3, 4], [5, 6], [7, 8]] // V_1 is a 4x2 matrix
[[9, 10], [11, 12]], // V_2 is a 2x2 matrix
[[13, 14], [15, 16]], // V_3 is a 2x2 matrix
[[17, 18], [19, 20]], // V_4 is a 2x2 matrix
[[21, 22], [23, 24]], // V_5 is a 2x2 matrix
[[25, 26, 27], [28, 29, 30]] // V_6 is a 2x3 matrix
]
Hint: The size of W is determined by the formula for NUM_PARAMS, which is NUM_PARAMS = A*B + (L-1)B^2 + BC.
The following is my attempt after pieces a bunch of different things together I found online. I will be honest, I do not completely understand every step very well and am just a beginner. The problem seems pretty easy to solve on paper, but I am struggling with the array of pointers aspect. My solution is almost correct, and I cant seem to find my mistake. Additionally, if I am doing very bad practice or programming principles, I would appreciate these to be addressed.
#include <stdio.h>
// Define constants globally
#define A 4
#define B 2
#define C 3
#define L 5
#define NUM_PARAMS (A * B + (L - 1) * B * B + B * C)
// Example linear array W
int W[NUM_PARAMS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
// Function to construct the jagged array J from W
void constructJaggedArray(int **J) {
int currentIndex = 0;
J[0] = &W[currentIndex];
currentIndex += A * B;
for (int i = 1; i < L; ++i) {
J[i] = &W[currentIndex];
currentIndex += B * B;
}
J[L] = &W[currentIndex];
currentIndex += B * B;
J[L+1] = &W[currentIndex];
}
void printJaggedArray(int **J) {
// Print V_1
printf("V_1:\n");
for (int i = 0; i < A; i++) {
for (int j = 0; j < B; j++) {
printf("%d ", J[0][i * B + j]);
}
printf("\n");
}
// Print V_2 to V_L
for (int k = 1; k <= L; k++) {
printf("V_%d:\n", k + 1);
for (int i = 0; i < B; i++) {
for (int j = 0; j < B; j++) {
printf("%d ", J[k][i * B + j]);
}
printf("\n");
}
}
// Print V_{L+1}
printf("V_%d:\n", L + 2);
for (int i = 0; i < B; i++) {
for (int j = 0; j < C; j++) {
printf("%d ", J[L+1][i * C + j]);
}
printf("\n");
}
}
int main() {
int *J[L+1]; // Array of pointers to represent the jagged array
constructJaggedArray(J);
printJaggedArray(J);
return 0;
}
OUTPUT (GCC):
V_1: 1 2
3 4
5 6
7 8
V_2:
9 10
11 12
V_3:
13 14
15 16
V_4:
17 18
19 20
V_5:
21 22
23 24
V_6:
25 26
27 28
V_7:
29 30 0
0 -1856930224 32758
J[L+1] = &W[currentIndex];That is illegal.Jcan only hold elements0..Lfor (int k = 1; k <= L; k++)should befor (int k = 1; k < L; k++)And also the last part of the print function is off by 1 in the same way