Golang program to traverse a given input array, with Boolean flag, using arrays and struct.

Go ProgrammingServer Side ProgrammingProgramming

Example

Approach

  • Ask the user to enter the size of array.

  • Make a string array of given size.

  • Ask the user to enter elements.

  • At the end, print the array.

Example

 Live Demo

package main
import "fmt"
func main(){
   arr := []int{10, 20, 30, 60, 40, 50}
   boolArr := []bool{true, false, true, false, true, false}
   fmt.Println("Input Array is: ", arr)
   fmt.Println("Input Boolean Array is: ", boolArr)
   visitedArray := []struct{
      i int
      b bool
   }{
      {10, true},
      {20, false},
      {30, true},
      {60, false},
      {40, true},
      {50, false},
   }
   fmt.Println("Boolean array using struct: ", visitedArray)
}

Output

Input Array is: [10 20 30 60 40 50]
Input Boolean Array is: [true false true false true false]
Boolean array using struct: [{10 true} {20 false} {30 true} {60 false} {40 true}
{50 false}]
raja
Published on 18-Mar-2021 16:54:19
Advertisements