0

Golang as u can see in this code(.go) am trying to input 2d arrays and storing even integers in even array and for odd in odd array. But we can see in its output as i mentioned below after code in comments it skips indexes during input? and even array can't get the even values. What am doing wrong?

 package main
    import (
    "fmt"
    )
    func main() {
    numbers := [4][4]int{}
    even := [4]int{}
    odd := [4]int{}
    row := 4
    col := 4

    fmt.Println("Enter data in your 2-D Array")
    for row = 0; row < 4; row++ {
        for col = 0; col < 4; col++ {
            fmt.Scanf("%d", &numbers[row][col])
            if numbers[row][col]%2 == 0 {
                even[row] = numbers[row][col]
            } else {
                odd[row] = numbers[row][col]
            }
        }
    }

    fmt.Println("Printing 2D Arrays", numbers)
    fmt.Println("Print Even Array", even)
    fmt.Println("Print Odd Array", odd)

    }
    //My output is as follow
    //Printing 2D Arrays [[1 0 2 0] [3 0 4 0] [5 0 6 0] [7 0 8 0]]
    //Print Even Array [0 0 0 0]
    //Print Odd Array [1 3 5 7]
1
  • I think you are doing this wrong. Correct me if it is not. Your numbers has 16 elements and even and odd have 4 elements for each. so you need 2D array to match event and odd elements? Commented Jun 3, 2021 at 8:51

1 Answer 1

2

You have 16 elements in your input and you try to store those elements in length 4 even array and length 4 odd array. then it is obvious to skip 8 elements there. You can use append to even and odd arrays.

Note :- when you declare 2D array, [rows][columns]int{}. Be aware when iterating through this, otherwise it can be a Out of bound panic.

package main
import (
    "fmt"
)
func main() {
    numbers := [4][4]int{}
    var even []int
    var odd []int
    //row := 2
    //col := 4

    fmt.Println("Enter data in your 2-D Array")
    for row := 0; row < 4; row++ {
        for col := 0; col < 4; col++ {
            fmt.Scanf("%d", &numbers[row][col])
            if numbers[row][col]%2 == 0 {
                even = append(even, numbers[row][col])
            } else {
                odd = append(odd, numbers[row][col])
            }
        }
    }

    fmt.Println("Printing 2D Arrays", numbers)
    fmt.Println("Print Even Array", even)
    fmt.Println("Print Odd Array", odd)

    //Printing 2D Arrays [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16]]
    //Print Even Array [2 4 6 8 10 12 14 16]
    //Print Odd Array [1 3 5 7 9 11 13 15]

}
Sign up to request clarification or add additional context in comments.

2 Comments

Sir thnks for ur kind reply. Num array must store 16 elements but it is just storing 8 elemets and skiping 8 elements problem lies here in this statement fmt.Scanf("%d", &numbers[row][col]). See the output (Printing 2D Arrays [[1 0 2 0] [3 0 4 0] [5 0 6 0] [7 0 8 0]]) i only enter 8 elemets others are by default zeroo.
Can you run this code i have posted above and see the results? I have edited it with my outputs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.