CA2 - Assignment Mod1 - 2 - AY22-23
CA2 - Assignment Mod1 - 2 - AY22-23
(Established under the Presidency University Act, 2013 of the Karnataka Act 41 of 2013)
SCHOOL OF ENGINEERING
CSE399-PROGRAMMING IN GO
Assignment-1(Module2)
Instructions:
Each Module assignments are part of Continuous assessment (10 marks).
Completion of assignment is mandatory to attend Midterm and Final exams.
The assignment submission format is attached with questions, Strictly Follow the same format.
You can upload assignment only in MS teams.
Late submissions will invite mark deduction
Screen shot of customized output is mandatory.
1. Ask the user to input a sentence which contains at-least 4 words. Capitalize first letter of each
word and and display complete sentence.
Code ( typing):
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
fmt.Print("Enter a sentence with 4 at-least words:")
inputReader := bufio.NewReader(os.Stdin)
input, _ := inputReader.ReadString('\n')
words := strings.Fields(input)
if len(words) < 4 {
fmt.Println("Please input at-least for words.")
return
}
for i := 0; i < len(words); i++ {
words[i] = strings.Title(words[i])
}
fmt.Println(strings.Join(words, " "))
}
import "fmt"
func main() {
var x = []float64{1.2, 2.2, 3.2, 4.2, 2.3}
var y = []float64{2.1, 3.1, 5.1, 2.3, 2.4}
sumx, sumy := 0.0, 0.0
if len(x) == len(y) {
fmt.Println("Lists are of same length.")
} else {
fmt.Println("Lists are not of same length.")
}
for _, s := range x {
sumx += s
}
for _, s := range y {
sumy += s
}
if sumx == sumy {
fmt.Println("Sum of the lists are equal to each other.")
} else {
fmt.Println("Sum of the lists are not equal to each other.")
}
for _, s := range x {
for _, sy := range y {
if s == sy {
fmt.Println("There are common values present in lists.")
}
}
}
}
3. Read a list of integers from the user, name it as input, utill the user enters 0. Then Create 2 new
lists namely
1). Square, where each element is a square of the corresponding element in the input list
2). Positive, with only positive numbers from the input list.
Code ( typing):
package main
import "fmt"
func main() {
var input []int
var Square []int
var Positive []int
var i int
fmt.Println("Enter input:\n(Note:Enter 0 to stop giving input to list)")
for {
fmt.Scan(&i)
if i != 0 {
input = append(input, i)
} else {
break
}
}
fmt.Println("Input list:", input)
for _, s := range input {
Square = append(Square, (s * s))
}
fmt.Println("Square list:", Square)
for _, s := range input {
if s > 0 {
Positive = append(Positive, s)
}
}
fmt.Println("Positive list:", Positive)
}
Code ( typing):
package main
import "fmt"
func main() {
max, sum, n := 0, 0, 7
var mkey string
var avg float32
temp := map[string]int{"sun": 32, "mon": 30, "tue": 29, "wed": 25, "thur": 25, "fri": 27, "sat":
24}
for key, value := range temp {
if max <= value {
max = value
mkey = key
}
sum = sum + value
}
avg = float32(sum) / float32(n)
fmt.Println("Maximum temperature:", max, mkey, "\nAverage temperature of week:", avg)
}
Output (screen shot):
5. Initilaize a map having key as section and value is another map consists of roll number
and cgpa. Read roll number from the user and find section, and cgpa of the student
map{"cse1":map[string]float32{"cse0001":8.9, "cse003":7.8 }
"cse2": map[string]float32{"cse0010":8.0, "cse0011":7.0 }
Code ( typing):
package main
import "fmt"
func main() {
data := map[string]map[string]float64{"cse1": {"cse0001": 8.9, "cse003": 7.8},
"cse2": {"cse0010": 8.0, "cse0011": 7.0}}
var section string
var rollno string
cgpa := -0.1
fmt.Print("Enter the Roll number:")
fmt.Scanln(&rollno)
for k, v := range data {
for k2, v2 := range v {
if k2 == rollno {
cgpa = v2
section = k
break
}
}
}
if cgpa == -0.1 {
fmt.Println("Roll number not found")
} else {
fmt.Println("Roll number", rollno, "is in section", section, "and has CGPA", cgpa)
}
}
Output (screen shot):
6. Given a map with covid patient detials from a hospital with patient id and age.
data{1001:21, 1002:35, 1003:12, 1004:64, 1005:17, 1006:59, 1007:43........}
Make 2 list, one contains id of young patients less thant 18 years old and second one consists of id of
senior citizens aged above 60
Code ( typing):
package main
import "fmt"
func main() {
var data = map[int]int{1001: 21, 1002: 35, 1003: 12, 1004: 64, 1005: 17, 1006: 59, 1007: 43}
var young = []int{}
var senior = []int{}
for i, val := range data {
if val > 60 {
senior = append(senior, i)
continue
} else if val > 18 {
young = append(young, i)
}
}
fmt.Println("Young patients ID:", young)
fmt.Println("Senior patients ID:", senior)
}
Output (screen shot):
7. Read account no., name,balance amount and amount to be deposited to create and print account
details of 2 persons using struct "bank account".
Code ( typing):
package main
import "fmt"
func main() {
var p [2]bank_account
for i := 0; i < len(p); i++ {
fmt.Printf("Enter the details of user %d:\n", i+1)
fmt.Print("Name:")
fmt.Scanln(&p[i].name)
fmt.Print("Account number:")
fmt.Scanln(&p[i].acc_no)
fmt.Print("Balance amount:")
fmt.Scanln(&p[i].bal_amt)
fmt.Print("Amount to be deposited:")
fmt.Scanln(&p[i].dep_amt)
}
fmt.Println()
for i := 0; i < len(p); i++ {
fmt.Println("Details of user ", i+1, ":")
fmt.Println("Name:", p[i].name, " Account number:", p[i].acc_no, " Balance amount:",
p[i].bal_amt, " Amount to be deposited:", p[i].dep_amt)
}
}
Output (screen shot):
8. Create account details of 2 persons using struct "bank account". Read account no., name, balance
amount and amount to be deposited and print the updated balance using go functions.
Code ( typing):
package main
import "fmt"
import "fmt"
import "fmt"
11. The Fibonacci sequence is defined as: fib(0) = 0, fib(1) = 1, fib(n) = fib(n-1) + fib(n-2). Write a
recursive function in go which can find fib(n).
Code ( typing):
package main
import "fmt"