Reputation: 55
In this program an array has 3 sizes. First size refers to row, second refers to column, but what the third do? I have another question, array refers %s
to print.But in this program array printed by %d
.Since array assign to x
value. Which is int
data type, but we print array. So how it would be %d
?
#include<stdio.h>
int main(void) {
char array[3][3][3];
int i,j,k;
int x = 1;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
for(k=0; k<3; k++){
array[i][j][k] = x;
printf("%d\n", array[i][j][k]);
x++;
}
}
}
return 0; }
Upvotes: 0
Views: 124
Reputation: 15842
char[3]
filled with ones would look like:
+---+---+---+
| 1 | 1 | 1 |
+---+---+---+
char[3][3]
filled with ones would look like:
+---+---+---+
| 1 | 1 | 1 |
+---+---+---+
| 1 | 1 | 1 |
+---+---+---+
| 1 | 1 | 1 |
+---+---+---+
char[3][3][3]
filled with ones would be three above square arrays one over one. There will be a cube.
On the above picture, each small cube will represent single cell so there are 27 cells in total.
I'm glad you didn't ask about char[3][3][3][3]
.
Upvotes: 4
Reputation: 566
Let's think about book. A single line on a page of the book is a 1d array. The whole page is a 2d array. The whole book is a 3d array. Let's assume, the book is in a box, then the box represents a 4d array. A set of boxes is a 5d array. So, you can think more of this. So, We will represent the first element of the first page of the first book of the first box as [0][0][0][0][0].
%s - Take the next argument and print it as a string
%d - Take the next argument and print it as an int
Here, your array elements are integer. Your code prints the array element one by one.
Upvotes: 0
Reputation: 588
%d
is for decimal, %s
is for string.
Here's a representation of a 3D array, the third dimension is the "depth" or in other words, the content of row i and column j (which in a 2d array would be an element but in a 3D array is an array)
int myFirst3DArray [3][4][2] =
{
{{10,11},{12,13},{14,15},{16,17}},
{{18,19},{20,21},{22,23},{24,25}},
{{26,27},{28,29},{30,31},{32,33}}
};
Upvotes: -1
Reputation: 345
Regarding the question of the array[3][3][3], this is a 3 dimension table. You can think of it as three 2-dimension tables. table 0: 3x3 table 1: 3x3 table 2: 3x3
Rows and Columns are descriptions we use to better understand the concept of 2 dimension elements. The array can have more dimensions - if it helps you, you can think of it as list of ( list(of list(..)))
Regarding the char question and the integer representation, chars and ints can be used in the same way in C. chars are essentially ASCII characters represented by the character's index in the ASCII table. ie. 'a' is 97.
Upvotes: 0
Reputation: 409176
To start with the second question first, you have an array of arrays of arrays of (small) integers. How do you print an integer using printf
? You (can) use the "%d"
format. You are confused (I guess) because strings (which are printed with "%s"
) are arrays of characters. But you are not printing an array, you are printing individual int
values.
As for the first question, part of the answer is in the above paragraph. You have an array of three arrays of three arrays of three (small) integers. Rows and columns are just labels, and for a "3d" array they don't make much sense.
Upvotes: 0