Q:

Golang program to demonstrate the multidimensional array

0

In this program, we will read elements for the 3D array from the user and then elements of the 3D array on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to demonstrate the multidimensional array is given below. The given program is compiled and executed successfully.

// Golang program to demonstrate the multidimensional array

package main

import "fmt"

func main() {
	var ThreeD [2][2][2]int

	fmt.Printf("Enter elements for 3D array: \n")
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			for k := 0; k < 2; k++ {
				fmt.Printf("Elements: ThreeD[%d][%d][%d]: ", i, j, k)
				fmt.Scanf("%d", &ThreeD[i][j][k])
			}
		}
	}

	fmt.Printf("Elements of multidimentional array: \n")
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			for k := 0; k < 2; k++ {
				fmt.Printf("\nThreeD[%d][%d][%d]: %d ", i, j, k, ThreeD[i][j][k])
			}
		}
	}
}

Output:

Enter elements for 3D array:
Elements: ThreeD[0][0][0]: 1
Elements: ThreeD[0][0][1]: 2
Elements: ThreeD[0][1][0]: 3
Elements: ThreeD[0][1][1]: 4
Elements: ThreeD[1][0][0]: 5
Elements: ThreeD[1][0][1]: 6
Elements: ThreeD[1][1][0]: 7
Elements: ThreeD[1][1][1]: 8
Elements of multidimentional array:

ThreeD[0][0][0]: 1
ThreeD[0][0][1]: 2
ThreeD[0][1][0]: 3
ThreeD[0][1][1]: 4
ThreeD[1][0][0]: 5
ThreeD[1][0][1]: 6
ThreeD[1][1][0]: 7
ThreeD[1][1][1]: 8

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created a 3D array.

fmt.Printf("Enter elements for 3D array: \n")
for i:=0;i<2;i++{
    for j:=0;j<2;j++{
        for k:=0;k<2;k++{
            fmt.Printf("Elements: ThreeD[%d][%d][%d]: ",i,j,k)
            fmt.Scanf("%d",&ThreeD[i][j][k])
        }    
    }
}

In the above code, we read elements for a 3D array from the user.

fmt.Printf("Elements of multidimentional array: \n")
for i:=0;i<2;i++{
    for j:=0;j<2;j++{
        for k:=0;k<2;k++{
            fmt.Printf("\nThreeD[%d][%d][%d]: %d ",i,j,k,ThreeD[i][j][k])
        }    
    }
}

In the above code, we printed the elements of the 3D array on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now