In: Computer Science
In C, write a program that initializes a 3D array (the exact size doesn't matter) that contain what ever arbitrary integers and print out the array including the elements and use different functions so that the main only contains variables and function calls.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<stdio.h>
#include<stdlib.h>
//number of rows, columns and height of the 3D array we use
//here we are thinking about creating a 3x3x3 array
const int SIZE=3;
//method to fill a 3D array with random values
void fill(int arr[SIZE][SIZE][SIZE]){
//looping through each height value
for(int i=0;i<SIZE;i++){
//looping through each row in current matrix
for(int j=0;j<SIZE;j++){
//looping through each column in current matrix
for(int k=0;k<SIZE;k++){
//generating a value between 0 and 99, assigning to current position
arr[i][j][k]=rand()%100;
}
}
}
}
//method to print the 3d array
void print(int arr[SIZE][SIZE][SIZE]){
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<SIZE;k++){
//printing location and element, so that user will not what is stored in which
//locations
printf("array[%d][%d][%d] = %d\n",i,j,k,arr[i][j][k]);
}
}
}
}
int main(){
//creating a SIZE x SIZE x SIZE array
int array[SIZE][SIZE][SIZE];
//filling with random values
fill(array);
//printing array
print(array);
return 0;
}
/*OUTPUT*/
array[0][0][0] = 41
array[0][0][1] = 67
array[0][0][2] = 34
array[0][1][0] = 0
array[0][1][1] = 69
array[0][1][2] = 24
array[0][2][0] = 78
array[0][2][1] = 58
array[0][2][2] = 62
array[1][0][0] = 64
array[1][0][1] = 5
array[1][0][2] = 45
array[1][1][0] = 81
array[1][1][1] = 27
array[1][1][2] = 61
array[1][2][0] = 91
array[1][2][1] = 95
array[1][2][2] = 42
array[2][0][0] = 27
array[2][0][1] = 36
array[2][0][2] = 91
array[2][1][0] = 4
array[2][1][1] = 2
array[2][1][2] = 53
array[2][2][0] = 92
array[2][2][1] = 82
array[2][2][2] = 21