0% found this document useful (0 votes)
38 views12 pages

CA2 - Assignment Mod1 - 2 - AY22-23

The document contains instructions for an assignment in the Programming in Go course (CSE399) at Presidency University. It lists 10 programming questions and asks the student to write code to solve each question, provide output screenshots, and follow a specified submission format. The questions cover topics like string manipulation, slices, maps, structs, and functions. The assignment is part of the continuous assessment for the course and completion is mandatory to attend exams. Late submissions will be penalized.

Uploaded by

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

CA2 - Assignment Mod1 - 2 - AY22-23

The document contains instructions for an assignment in the Programming in Go course (CSE399) at Presidency University. It lists 10 programming questions and asks the student to write code to solve each question, provide output screenshots, and follow a specified submission format. The questions cover topics like string manipulation, slices, maps, structs, and functions. The assignment is part of the continuous assessment for the course and completion is mandatory to attend exams. Late submissions will be penalized.

Uploaded by

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

AYYAPPA B K 7CSE-1 20191CSE0047

(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, " "))
}

Output (screen shot):

2. Read two lists of floating point numbers. Display whether


1) both lists are of same length or not
2)the elements in each list sum up to the same value
3)there any values that occur in both the lists.
Code ( typing):
package main

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.")
      }
    }
  }
}

Output (screen shot):

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)
}

Output (screen shot):

4. Given a map of day wise temperature in a week.


temp={"sun":32, "mon":30, "tue":29,"wed":25, "thur":25, "fri":27, "sat":24 }
Find which day recorded maximum temperature and also find average temperature of the week.

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"

type bank_account struct {


acc_no string
name string
bal_amt float64
dep_amt float64
}

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"

type bank_account struct {


acc_no string
name string
bal_amt float64
dep_amt float64
}

func getData() bank_account {


var user bank_account
fmt.Print("Name:")
fmt.Scanln(&user.name)
fmt.Print("Account number:")
fmt.Scanln(&user.acc_no)
fmt.Print("Balance amount:")
fmt.Scanln(&user.bal_amt)
fmt.Print("Amount to be deposited:")
fmt.Scanln(&user.dep_amt)
return user
}
func printData(user bank_account) {
fmt.Println("Name:", user.name, " Account number:", user.acc_no, " Balance amount:",
user.bal_amt, " Amount to be deposited:", user.dep_amt)
}
func updateBalance(user bank_account) {
user.bal_amt = user.bal_amt + user.dep_amt
fmt.Println("Updated balance:", user.bal_amt)
}
func main() {
fmt.Println("Enter the details of user 1:")
user1 := getData()
fmt.Println("Enter the details of user 2:")
user2 := getData()
fmt.Println("Details of user1:")
printData(user1)
updateBalance(user1)
fmt.Println("Details of user2:")
printData(user2)
updateBalance(user2)
}

Output (screen shot):


9. Write a function which takes an integer and halves it and returns true if it is even or false if it is odd.
For example half(1) should return (0, false) and half(2) should return (1, true).
Code ( typing):
package main

import "fmt"

func half(x int) bool {


y := x / 2
if y%2 == 0 {
fmt.Println("Half of", x, "is", y)
return true
} else {
fmt.Println(y)
return false
}
}
func main() {
var num int
fmt.Print("Enter number: ")
fmt.Scanln(&num)
fmt.Print(half(num))
}

Output (screen shot):


10.Write a function with one variadic parameter accepts a slice and then it finds the greatest number in
a list of numbers.
Code ( typing):
package main

import "fmt"

func greatest(values ...int) {


max := 0
for _, v := range values {
if max < v {
max = v
}
}
fmt.Println("Greatest number:", max)
}
func main() {
var a = []int{55, 23, 14, 99, 1111}
fmt.Println("List of number:", a)
greatest(a...)
}

Output (screen shot):

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"

func fibo(n int) int {


if n <= 1 {
return n
} else {
return fibo(n-1) + fibo(n-2)
}
}
func main() {
var num int
fmt.Print("Length of fibonacci series to be printed:")
fmt.Scanln(&num)
var i int
for i = 0; i < num; i++ {
fmt.Print(fibo(i), " ")
}
}

Output (screen shot):

You might also like