Question

In: Computer Science

In C, write a program that initializes a 3D array (the exact size doesn't matter) that...

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.

Solutions

Expert Solution

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


Related Solutions

In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT