#include <stdio.h>
main(){
int A[3][2] = {0} ;
printf("A = \n");
for(int x = 0 ; x < 3 ; x++){
for (int y = 0 ; y < 2 ; y ++){
A[x][y] = (x+1)*1 + (x*1+3)*y ;
printf("A[%d][%d] = %d ", x , y ,A[x][y]);
}
printf("\n");
}
printf("\nAT = \n");
for (int p = 0 ; p < 2 ; p++){
for (int q = 0 ; q < 3 ; q++){
A[p][q] = A[q][p];
printf("A[%d][%d] = %d ", p ,q , A[p][q]);
}
printf("\n");
}
}
Why does AT[1][0] = 2 not 4?
I have tested it for an hour for this simple question, but I have no idea.
If you transpose a non-square matrix then the dimensions of the resulting matrix will also be transposed. Not only that, if you were going to transpose in-place you would need to swap pairs of elements, otherwise you would be overwriting some of the elements before they had been transposed.
So, to solve both these problems you should probably just use a second matrix with the correct dimensions for the transposed result, e.g.
#include <stdio.h>
int main()
{
int A[3][2] = {0};
int AT[2][3] = {0}; // <<<
printf("A = \n");
for(int x = 0 ; x < 3 ; x++){
for (int y = 0 ; y < 2 ; y ++){
A[x][y] = (x+1)*1 + (x*1+3)*y ;
printf("A[%d][%d] = %d ", x, y, A[x][y]);
}
printf("\n");
}
printf("\nAT = \n");
for (int p = 0 ; p < 2 ; p++){
for (int q = 0 ; q < 3 ; q++){
AT[p][q] = A[q][p]; // <<<
printf("A[%d][%d] = %d ", p, q, AT[p][q]);
}
printf("\n");
}
return 0;
}
LIVE DEMO
You are accessing the array out of bounds when the condition came,
A[0][2]=A[2][0]; // <--
Here You can access only until first location. So as like Paul R says Use the
separate array when you transpose the array.
For example consider your array is declared in memory like this.
Memory Address 125 126 127 128 129 130
^ ^ ^ ^ ^ ^
| | | | | |
Array accessing [0][0] [0][1] [1][0] [1][1] [2][0] [2][1]
what will happen when you are accessing the [0][2]. It will go to the [1][0].
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
Final Edit
When debugging this code, I would see the first loop line execute n times, but the following lines would execute only once. Only later I would notice that the expected value changes, though, were in fact being made: it was just the loop line was being executed at once, as if the loop went through all iterations in one single step. From the second loop on, Pressing F10 (I use VSCode) would skip straight to the next loop, instead of skipping to the next iteration. That's how the compiler behaved (MinGW64).
Now, formatting the loops like this:
for( ; x<n; x++) {
p[y][x] = h;
} x--;
for( ; y<n; y++) {
p[y][x] = h;
} y--;
etc...
did cause each iteration to be read separately: now from the second loop on, pressing F10 would just jump to the next iteration, not straight to the next loop.
Conclusion: both styles work the same, but debug differently.
Anyway, here's a version that's still pretty ugly, but now works as expected:
#include <stdio.h>
int main(){
int n,size,sqrs,start=0,h,y,x; scanf("%i",&n);
int p[n][n];
size = n;
if(n%2==0) sqrs = n/2;
else sqrs = n/2+1;
h = 1;
y = start;
x = start;
for(int t=0; t<sqrs; t++){
for( ; x<n; x++) p[y][x] = h; x--;
for( ; y<n; y++) p[y][x] = h; y--;
for( ; x>=start; x--) p[y][x] = h; x++;
for( ; y>=start; y--) p[y][x] = h; y++;
y++; x++; h++; start++; n--;
}
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
printf("%i ",p[i][j]);
} printf("\n");
}
}
Edit:
I'm trying to build a matrix where the numbers grow towards the center, i.e. a "pyramid".
As of yet, I haven't had any problem with single-statemente one-liner for loops in C. Now, placing them consecutively is causing them, except for the first loop, to be skipped.
As I see it, we have a `for` loop, then its single statement, and then a semicolon, which should (at least, I expected it to) terminate the line, then move on to the next piece of code, which just happens to be another `for` loop. The problem is the first `for` loop runs as expected, however, the following ones are just skipped, as they were at their ending condition.
I've stressed "not nested" because:
while searching for this issue, all I had found were about nesting loops and if statements regarding using or not braces;
each for loop/statement ends with a semicolon.
I've tried bracing the single statements, but it did not solve the problem.
What DID solve the problem though was adding braces AND newlines, like this:
for( ; y<n; y++) {
p[y][x] = h;
}
But still, it bothers me to be clueless of why these were required in C, in this case. For example, while debugging I had noticed the second loop is skipped because the value of y changes from 0 to 4. This alone is already a mystery to me.
By the way, my apologies for being vague!
Original post:
Here is my code:
#include <stdio.h>
int main(){
int n,sqrs,start=0,h,y,x; scanf("%i",&n);
int p[n][n];
if(n%2==0) sqrs = n/2;
else sqrs = n/2+1;
h = 1;
y = start;
x = start;
for(int t=0; t<sqrs; t++){
for( ; x<n; x++) p[y][x] = h; // THESE LOOPS
for( ; y<n; y++) p[y][x] = h; // here, y changes from 0 to 4
for( ; x>=start; x--) p[y][x] = h;
for( ; y>=start; y--) p[y][x] = h;
y++; x++; h++; start++; n-=2;
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%i",p[i][j]);
} printf("\n");
}
}
As you can see, these loops aren't nested. They would nest though if there weren't any statements in between them, but there are.
So, the first one iterates normally but all of the following ones don't. They just skip.
I did manage to get around this, but curiously only when I added both braces and newlines, i.e. when all loops (except the first one, which doesn't seem to require it) looked like this:
for( ; y<n; y++) {
p[y][x] = h;
}
This remains a mystery to me.
Of course, I'm sorry if this is a duplicate. I just couldn't find anything about this specifically.
EDIT:
As some of you have thankfully noticed, yes, I'm trying to print a "pyramid" matrix. The reason I didn't mention it is because to me the problem was about syntax only, being the not so relevant.
It's not clear from either the OP code or the description what it is you are trying to achieve.
Based on another recent SO question, perhaps this is what you are seeking. This takes advantage of the symmetry of the (I believe) desired output. Four array elements are assigned in each iteration until the centre of the pattern is reached.
int main() {
const uint8_t n = 9;
uint8_t mat[n][n];
for( int r = 0; r < (n+1)/2; r++ )
for( int c = 0; c < (n+1)/2; c++ )
mat[r ][c ] =
mat[n-1-r][c ] =
mat[r ][n-1-c] =
mat[n-1-r][n-1-c] =
1 + (c<r?c:r);
for( int i = 0; i < n; i++ ) {
for( int j = 0; j < n; j++ )
printf("%d ", mat[i][j]);
puts( "" );
}
return 0;
}
1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 1
1 2 3 3 3 3 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1
Perhaps the "not nested" is the criteria?
int main() {
const uint8_t n = 9, h = ((n+1)/2); // 'h'alf way
uint8_t mat[n][n];
for( int x = 0; x < h*h; x++ ) {
int r0 = x / h, r1 = n-1-r0; // top & bottom rows of this square
int c0 = x % h, c1 = n-1-c0; // left & right cols of this square
uint8_t v = 1 + ( c0 < r0 ? c0 : r0 );
mat[r0][c0] = mat[r0][c1] =
mat[r1][c0] = mat[r1][c1] = v;
}
/* same nested output loops */
return 0;
}
There are multiple mistakes in your code:
you should undo the last increment/decrement after each of the inner loops. As coded, you store h beyond the end of the array p.
n should be decremented only by 1
since n is modified, the final loops have no effect.
Here is a modified version, keeping your corny style:
#include <stdio.h>
int main(){
int n,sqrs,start=0,end,h,y,x; scanf("%i",&n);
int p[n][n];
if(n%2==0) sqrs = n/2;
else sqrs = n/2+1;
h = 1;
y = start;
x = start;
end = n;
for(int t=0; t<sqrs; t++){
for( ; x<end; x++) p[y][x] = h; x--;
for( ; y<end; y++) p[y][x] = h; y--;
for( ; x>=start; x--) p[y][x] = h; x++;
for( ; y>=start; y--) p[y][x] = h; y++;
y++; x++; h++; start++; end--;
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%i",p[i][j]);
} printf("\n");
}
}
This code can be simplified as:
#include <stdio.h>
int main(){
int n; scanf("%i",&n);
int p[n][n];
int h=1,start=0,stop=n-1;
for(int t=0; t<n; t+=2,h++,start++,stop--){
int x=start,y=start; p[y][x] = h;
while(x<stop) p[y][x++] = h;
while(y<stop) p[y++][x] = h;
while(x>start) p[y][x--] = h;
while(y>start) p[y--][x] = h;
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++) printf("%i",p[i][j]);
printf("\n");
}
}
And here is a more classic version:
#include <stdio.h>
int main() {
int n;
if (scanf("%i", &n) != 1 || n <= 0)
return 1;
int p[n][n];
int h = 1, start = 0, stop = n - 1;
for (int t = 0; t < n; t += 2, h++, start++, stop--) {
int i = start, j = start;
if (start == stop)
p[i][j] = h;
for (; j < stop; j++)
p[i][j] = h;
for (; i < stop; i++)
p[i][j] = h;
for (; j > start; j--)
p[i][j] = h;
for (; i > start; i--)
p[i][j] = h;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%i", p[i][j]);
printf("\n");
}
return 0;
}
Note that instead of tracing successive squares, you can compute the array values directly:
#include <stdio.h>
int min(int a, int b) { return a < b ? a : b; }
int main() {
int n, i, j;
if (scanf("%i", &n) != 1 || n <= 0)
return 1;
char p[n][n + 1];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
p[i][j] = '1' + min(min(i, j), min(n - i - 1, n - j - 1));
p[i][j] = '\n';
}
printf("%.*s", (int)sizeof(p), &p[0][0]);
return 0;
}
It appears that you are trying to make a pattern of concentric boxes in a matrix. Your logic is wrong. Following is an example using the Ada programming language. Translating that into C should be easy.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Main is
type matrix is array (Positive range <>, Positive range <>) of Positive;
Side : Positive;
begin
Put ("Enter the number of elements on a side of the array: ");
Get (Side);
declare
Box : matrix (1 .. Side, 1 .. Side);
Center : Positive;
Start : Positive := 1;
Stop : Positive := Side;
Value : Positive := 1;
begin
Center := (if Side mod 2 = 0 then side / 2 else side / 2 + 1);
for turn in 1 .. Center loop
-- sides
for I in Start .. Stop loop
Box (Start, I) := Value;
Box (I, Start) := Value;
Box (Stop, I) := Value;
Box (I, Stop) := Value;
end loop;
-- adjust for inner box values
Start := Start + 1;
Stop := Stop - 1;
Value := Value + 1;
end loop;
for I in Box'Range (1) loop
for J in Box'Range (2) loop
Put (Item => Box (I, J), Width => 1);
end loop;
New_Line;
end loop;
end;
end Main;
The output of a sample execution is:
Enter the number of elements on a side of the array: 7
1111111
1222221
1233321
1234321
1233321
1222221
1111111
I have to implement a function which, given a matrix of 0s and 1s, returns a new matrix that contains the coordinates of the 1s. For example: if matrix is 3x3 and the output is:
1 0 1
0 1 1
0 0 1
New matrix will be 5x2 and the output will be:
0 0
0 2
1 1
1 2
2 2
Some advice? My method would be this:
int matrix[3][3];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (matrix[i][i] == 1){
//Code i need
}
}
}
The solution really depends on the requirement:
Is it allowed to allocate result matrix with the maximum size possible (in this case 9 x 2).
If point 1 is not allowed, is it strictly required to use fixed size array (no dynamic allocation). If this is the case then may need to pass the matrix twice to allocate the right size of array.
Other solution is of course by using dynamic allocation (using malloc etc).
The simplified version of option 1 & 2 is shown below:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int matrix[3][3];
matrix[0][0] = 1;
matrix[0][1] = 0;
matrix[0][2] = 1;
matrix[1][0] = 0;
matrix[1][1] = 1;
matrix[1][2] = 1;
matrix[2][0] = 0;
matrix[2][1] = 0;
matrix[2][2] = 1;
//Solution 1 - If allowed to allocate matrix with size more than the result,
//i.e. if input is 3x3 matrix, then the maximum size of result matrix is 9 x 2
int resultMatrix1[9][2];
int usedCount1=0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
resultMatrix1[usedCount1][0] = i;
resultMatrix1[usedCount1][1] = j;
usedCount1++;
} //end if
} //end for
} //end for
//Print the result
printf("\nSolution 1\n");
for (int i = 0; i < usedCount1; i++){
printf("%d %d\n", resultMatrix1[i][0], resultMatrix1[i][1]);
} //end for
//Solution 2 - strictly allocate matrix with size equal to the result.
//Without using dynamic allocation, meaning we need to have two passes.
//1st pass is to count the element which satisfy the criteria
int usedCount2=0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
usedCount2++;
} //end if
} //end for
} //end for
int resultMatrix2[usedCount2][2]; //allocate the right size
int idx=0;
//2nd pass is to fill in the result matrix
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
resultMatrix2[idx][0] = i;
resultMatrix2[idx][1] = j;
idx++;
} //end if
} //end for
} //end for
//Print the result
printf("\nSolution 2\n");
for (int i = 0; i < usedCount2; i++){
printf("%d %d\n", resultMatrix2[i][0], resultMatrix2[i][1]);
} //end for
return 0;
}
Results are the same for both solution:
Solution 1
0 0
0 2
1 1
1 2
2 2
Solution 2
0 0
0 2
1 1
1 2
2 2
If matrix[i][j] == 1 what do you know about the coordinates [i,j] ? That they are the coordinates of a 1 :)
Secondly, will the input matrix always be 3x3? If not, you'll want to store the dimensions of the matrix in variables, and use theses variables for your for loops.
#include<stdio.h>
#define NUM_ROWS 3
#define NUM_COLS 5
int main(void){
int a[NUM_ROWS][NUM_COLS],(*p)[NUM_COLS], i;
for (p = &a[0],i=0; p < &a[NUM_ROWS]; p++,i++){
(*p)[i]=i;
}
printf("The value of a[0][0] is %d\n",a[0][0]); // I want 0
printf("The value of a[0][1] is %d\n",a[0][1]); // 1
printf("The value of a[0][2] is %d\n",a[0][2]); // 2
printf("The value of a[0][3] is %d\n",a[0][3]); // 3
printf("The value of a[0][4] is %d\n",a[0][4]); // 4
return 0;
}
Hi guys I'm a C novice, and I am trying to understand processing the columns of a 2D array.
I wanted output of 0,1,2,3,4 from row 0's columns but I had these results
The value of a[0][0] is 0
The value of a[0][1] is 0
The value of a[0][2] is 1
The value of a[0][3] is 0
The value of a[0][4] is -1
I tried to find what was wrong, but I failed to....
I will be grateful if someone explains what is wrong with my codes..
Your assignment in the loop is initializing the leading diagonal:
(*p)[i] = i;
To illustrate, here's an adaptation of your code that prints the whole matrix (and initializes it):
#include <stdio.h>
#include <string.h>
#define NUM_ROWS 3
#define NUM_COLS 5
int main(void)
{
int a[NUM_ROWS][NUM_COLS], (*p)[NUM_COLS], i;
/* Set all elements to -1 assuming 2's complement */
memset(a, 0xFF, sizeof(a));
for (p = &a[0], i = 0; p < &a[NUM_ROWS]; p++, i++)
{
(*p)[i] = i;
}
for (i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_COLS; j++)
printf("%3d", a[i][j]);
putchar('\n');
}
return 0;
}
The output is:
0 -1 -1 -1 -1
-1 1 -1 -1 -1
-1 -1 2 -1 -1
Notice that the three elements on the leading diagonal are set to 0, 1, 2 and the rest are -1 as set by memset().
If you want to initialize the first row, then you simply use:
int (*p)[NUM_COLS] = &a[0];
for (int i = 0; i < NUM_COLS; i++)
(*p)[i] = i;
Or, more simply still, forget about p and use:
for (int i = 0; i < NUM_COLS; i++)
a[0][i] = i;
If you want to initialize column 0, you need:
(*p)[0] = i;
Or, again, more simply, forget about p and use:
for (int i = 0; i < NUM_ROWS; i++)
a[0][i] = i;
I think you want to write your for loop like this (based on you captions)
for the first row.
for (p = &a[0], i=0; i < NUM_COLS; i++){
(*p)[i] = i;
}
Here increasing the pointer p doesn't help for it will be incremented by a value of NUM_COLS every time due to pointer arithmetic.
Here is a second possible answer if you want to write and read the whole matrix within a single for loop
for (p = &a[0], i = 0; i < NUM_COLS * NUM_ROWS; i++) {
(*p)[i] = i;
}
Or the better solution would be to use two for loops as
for (p = &a[0], i = 0; p < &a[NUM_ROWS] ; p ++) {
int k = 0; //for the column count
for(;k < NUM_COLS; i++, k++) {
(*p)[k] = i;
}
}
Thanks for asking :)
for (p = &a[0]; p < &a[NUM_ROWS]; p++){
for(i=0;i<NUM_COLS;i++){
(*p)[i]=i;
printf("%d\n",(*p)[i]);
// I got 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
}
}
Should I write nested loop to print out all elements of 2D?
Is there a way to print out using 1 loop with processing columns of 2D array?
I have a binary matrix (zeros and ones) D[][] of dimension nxn where n is large (approximately around 1500 - 2000). I want to find the inverse of this matrix in C.
Since I'm new to C, I started with a 3 x 3 matrix and working around to generalize it to N x N. This works for int values, however since I'm working with binary 1's and 0's. In this implementation, I need unsigned int values.
I could find many solutions for int values but I didn't come across any solution for unsigned int. I'd like to find the inverse of a N x N binary matrix without using any external libraries like blas/lapack. It'd be great if anyone could provide a lead on M x N matrix.
Please note that I need inverse of a matrix, not the pseudo-inverse.
/* To find the inverse of a matrix using LU decomposition */
/* standard Headers */
#include<math.h>
#include<stdio.h>
int main() {
/* Variable declarations */
int i,j;
unsigned int n,m;
unsigned int rows,cols;
unsigned int D[3][3], d[3], C[3][3];
unsigned int x, s[3][3];
unsigned int y[3];
void LU();
n = 2;
rows=3;cols=3;
/* the matrix to be inverted */
D[0][0] = 1;
D[0][1] = 1;
D[0][2] = 0;
D[1][0] = 0;
D[1][1] = 1;
D[1][2] = 0;
D[2][0] = 1;
D[2][1] = 1;
D[2][2] = 1;
/* Store the matrix value for camparison later.
this is just to check the results, we don't need this
array for the program to work */
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++) {
C[m][j] = D[m][j];
}
}
/* Call a sub-function to calculate the LU decomposed matrix. Note that
we pass the two dimensional array [D] to the function and get it back */
LU(D, n);
printf(" \n");
printf("The matrix LU decomposed \n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
printf(" %d \t", D[m][j]);
}
printf("\n");
}
/* TO FIND THE INVERSE */
/* to find the inverse we solve [D][y]=[d] with only one element in
the [d] array put equal to one at a time */
for (m = 0; m <= rows-1; m++) {
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[m] = 1;
for (i = 0; i <= n; i++) {
x = 0;
for (j = 0; j <= i - 1; j++){
x = x + D[i][j] * y[j];
}
y[i] = (d[i] - x);
}
for (i = n; i >= 0; i--) {
x = 0;
for (j = i + 1; j <= n; j++) {
x = x + D[i][j] * s[j][m];
}
s[i][m] = (y[i] - x) / D[i][i];
}
}
/* Print the inverse matrix */
printf("The Inverse Matrix\n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
printf(" %d \t", s[m][j]);
}
printf("\n");
}
/* check that the product of the matrix with its iverse results
is indeed a unit matrix */
printf("The product\n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
x = 0;
for (i = 0; i <= 2; i++) {
x = x + C[m][i] * s[i][j];
}
//printf(" %d %d %f \n", m, j, x);
printf("%d \t",x);
}
printf("\n");
}
return 0;
}
/* The function that calcualtes the LU deomposed matrix.
Note that it receives the matrix as a two dimensional array
of pointers. Any change made to [D] here will also change its
value in the main function. So there is no need of an explicit
"return" statement and the function is of type "void". */
void LU(int (*D)[3][3], int n) {
int i, j, k;
int x;
printf("The matrix \n");
for (j = 0; j <= 2; j++) {
printf(" %d %d %d \n", (*D)[j][0], (*D)[j][1], (*D)[j][2]);
}
for (k = 0; k <= n - 1; k++) {
for (j = k + 1; j <= n; j++) {
x = (*D)[j][k] / (*D)[k][k];
for (i = k; i <= n; i++) {
(*D)[j][i] = (*D)[j][i] - x * (*D)[k][i];
}
(*D)[j][k] = x;
}
}
}
This is just a sample example that I tried and I have -1 values in the inverse matrix which is my main concern. I have 1000 x 1000 matrix of binary values and the inverse should also be in binary.
The matrix:
1 1 0
0 1 0
1 1 1
The matrix LU decomposed:
1 1 0
0 1 0
1 0 1
The Inverse Matrix:
1 -1 0
0 1 0
-1 0 1
The product:
1 0 0
0 1 0
0 0 1
I have an array in 1D.
data[27]=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};
I need to convert this into a 3D array of the form using C:
data[3][3][3]
Can someone help me doing this?
I tried the following code. Not seems to be working:
#include <stdio.h>
int main()
{
int x;
int y;
int z;
int res;
int data;
int byte[] data={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}; // Read 4096 bytes
byte[][][] res = new byte[3][3][3];
for (x = 0 ; x != 3 ; x++) {
for (y = 0 ; y != 3 ; y++) {
for (z = 0 ; z != 3 ; z++) {
res[x][y][z] = data[3*3*x + 3*y + z];
}
}
}
printf("Printing the 3D matrix\n");
for (x = 0 ; x != 16 ; x++) {
for (y = 0 ; y != 16 ; y++) {
for (z = 0 ; z != 16 ; z++) {
printf("%d\t",res[x][y][z]);
printf("\n");
}
}
}
return 0;
}
Your logic seems to be okay. The problem is with the declaration of the 1D and 3D arrays.
1) There id no datatype as byte in C
2) new is not a part of C. You cannot allocate memory using new
Try the following changes for your code to work
int main()
{
int x;
int y;
int z;
int data[] ={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}; // Read 4096 bytes
int res[3][3][3];
for (x = 0 ; x < 3 ; x++) {
for (y = 0 ; y < 3 ; y++) {
for (z = 0 ; z < 3 ; z++) {
res[x][y][z] = data[3*3*x + 3*y + z];
}
}
}
printf("Printing the 3D matrix\n");
//run the loop till maximum value of x, y & z
for (x = 0 ; x < 3 ; x++) {
for (y = 0 ; y < 3 ; y++) {
for (z = 0 ; z < 3 ; z++) {
printf("%d\t",res[x][y][z]);
printf("\n");
}
}
}
return 0;
}