Question: package main import ( fmt sort ) type Student struct { FirstName string LastName string Marks []int Rank int Status string } func (s Student)
package main
import (
"fmt"
"sort"
)
type Student struct {
FirstName string
LastName string
Marks []int
Rank int
Status string
}
func (s Student) String() string {
return fmt.Sprintf("%s %s %v %s", s.FirstName, s.LastName, s.Marks, s.Status)
}
// if student passed in atleast two subjects then it returns true otherwise its false
func (s Student) Passed() bool {
count := 0
for _, marks := range s.Marks {
if marks >= 33 {
count++
}
if count>1{
return true
}
}
return false
}
// it returns total marks of student
func (s Student) Total() int
{
total := 0
for _, marks := range s.Marks {
total += marks
}
return total
}
// A sortable type for sorting students by their total marks
type ByMarks []Student
func (s ByMarks) Len() int {
return len(s)
}
func (s ByMarks) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByMarks) Less(i, j int) bool {
return s[i].Total() > s[j].Total()
}
// function that takes in a slice of students and assigns them ranks based on their total marks
func assignRanks(students []Student) {
sort.Sort(ByMarks(students))
for i := range students { students[i].Rank = i + 1 }
}
func studetDetails(students []Student){
for i := range students {
if students[i].Passed() {
students[i].Status = "Pass"
} else { students[i].Status = "Fail"
}
}
assignRanks(students)
// Print all student records
for s := range students {
fmt.Println(s+1,students[s])
}
}
func main() {
students := []Student{
{"Ramu", "Ram", []int{45,56,67}, 0, ""},
{"Lakshman", "Lucky", []int{55,46,77}, 0, ""},
{"Sainath", "Sai", []int{35,40,30}, 0, ""},
{"Manohar", "Manu", []int{50,60,70}, 0, ""},
{"Joseph", "Joo", []int{33,45,87}, 0, ""},
{"Madhava","Maddy",[]int{12,37,32},0,""},
{"Sandeep","Sandy",[]int{38,51,37},0,""},
{"Aravind","Indurthi",[]int{56,44,49},0,""},
{"Chaitanya","Chaitu",[]int{55,36,37},0,""},
{"Mani","Kumar",[]int{55,34,56},0,""},
{"Pranay","sebastian",[]int{32,56,48},0,""},
{"Srinivas","Srinu",[]int{37,48,79},0,""},
{"Venkatesh","Venky",[]int{44,34,78},0,""},
{"Ramesh","Babu",[]int{64,36,87},0,""},
{"Charan","Cherry",[]int{45,78,31},0,""},
}
// Assigning status and rank to students
studetDetails(students) }
REQUIREMENT:-1. There is logical error in function passed(), it will give wrong output for some cases
2. Function assignRanks() is sorting the entire records which should never happen, assume records are according to their roll number and it must be displayed in same order.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
