ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Golang program to demonstrate the multidimensional array

Here, we are going to demonstrate the multidimensional array in Golang (Go Language)?
Submitted by Nidhi, on March 09, 2021

Problem Solution:

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.

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.

Golang Array Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.