0% found this document useful (0 votes)
8 views3 pages

Slip 7

Uploaded by

photosaccdivya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Slip 7

Uploaded by

photosaccdivya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1.

A) Write a program in GO language to accept one matrix and display


its transpose.
[20 Marks]
package main

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)

// Q1.A) Matrix Transpose


func matrixTranspose() {
reader := bufio.NewReader(os.Stdin)

fmt.Println("Enter number of rows:")


rowsStr, _ := reader.ReadString('\n')
rows, _ := strconv.Atoi(strings.TrimSpace(rowsStr))

fmt.Println("Enter number of columns:")


colsStr, _ := reader.ReadString('\n')
cols, _ := strconv.Atoi(strings.TrimSpace(colsStr))

matrix := make([][]int, rows)


for i := range matrix {
matrix[i] = make([]int, cols)
fmt.Printf("Enter elements for row %d (space-separated):\n", i+1)
rowStr, _ := reader.ReadString('\n')
rowElements := strings.Split(strings.TrimSpace(rowStr), " ")
for j, el := range rowElements {
num, err := strconv.Atoi(el)
if err != nil {
fmt.Println("Invalid input. Please enter only
numbers.")
return
}
matrix[i][j] = num
}
}

transpose := make([][]int, cols)


for i := range transpose {
transpose[i] = make([]int, rows)
for j := 0; j < rows; j++ {
transpose[i][j] = matrix[j][i]
}
}

fmt.Println("Original Matrix:")
for _, row := range matrix {
fmt.Println(row)
}

fmt.Println("Transpose Matrix:")
for _, row := range transpose {
fmt.Println(row)
}
}

OR
B) Write a program in GO language to create structure student. Write a
method show() whose receiver is a pointer of struct student.
// Q1.B) Student Structure and show() method
type Student struct {
rollNo int
name string
grade string
}

func (s *Student) show() {


fmt.Println("Roll No:", s.rollNo)
fmt.Println("Name:", s.name)
fmt.Println("Grade:", s.grade)
}

func studentShow() {
reader := bufio.NewReader(os.Stdin)

fmt.Println("Enter student roll number:")


rollStr, _ := reader.ReadString('\n')
roll, _ := strconv.Atoi(strings.TrimSpace(rollStr))

fmt.Println("Enter student name:")


name, _ := reader.ReadString('\n')
name = strings.TrimSpace(name)

fmt.Println("Enter student grade:")


grade, _ := reader.ReadString('\n')
grade = strings.TrimSpace(grade)

student := Student{
rollNo: roll,
name: name,
grade: grade,
}

student.show() // Call the method


}

func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Choose which program to run:")
fmt.Println("1. Matrix Transpose")
fmt.Println("2. Student Show Method")
choiceStr, _ := reader.ReadString('\n')
choice, _ := strconv.Atoi(strings.TrimSpace(choiceStr))

switch choice {
case 1:
matrixTranspose()
case 2:
studentShow()
default:
fmt.Println("Invalid Choice")
}
}

You might also like