Home > other >  Self-study of the language problem sets
Self-study of the language problem sets

Time:10-07

Package the main

Import (
"FMT"
"Math/rand"
"Time"
)
//save the randomly generated 10 integers array, and the reverse order to print and averaging, maximum, minimum subscript, and find whether there are 55

Func main () {

Var intArr [10] int
Var sum int=0

//10 random integer array and store again declared intArr in
Rand. Seed (time) Now ()) UnixNano ())
For I:=0; I & lt; 10; I++ {
IntArr [I]=rand. Intn (100) + 1
Sum=sum + intArr [I]
}

FMT. Println (intArr)
FMT. Println (" the sum to array ", sum)
Avg:=float64 (sum)/float64 (len (intArr))//while averaging possible decimal, so type conversion into float64
FMT. Println (" the average of array ", avg)

//reverse print
For I:=9; I & gt;=0; I - {
FMT. Printf (" intArr [v] %=% v \ n ", I, intArr [I])
}

//for maximum subscript
Var maxIndex int=0
For I:=0; I & lt; 10; I++ {
If intArr [I] & gt; IntArr [maxIndex] {
MaxIndex=I
}
}
FMT. Printf (" maximum value for: % v subscript v for % \ n ", intArr [maxIndex], maxIndex)

//for the minimum subscript
Var minIndex int=0
For I:=0; I & lt; 10; I++ {
If intArr [I] MinIndex=I
}
}
FMT. Printf (" minimum value is: % v subscript for % v \ n ", intArr [minIndex], minIndex)

//o in the array are 55
For I:=0; I & lt; 10; I++ {
If intArr [I]==55 {
FMT. Printf (" intArr [v] %=% v ", I, intArr [I])
Break
} else if I==(len (intArr) - 1) {
FMT. Println (" there is no 55 this number in the array ")
}
}
}

CodePudding user response:

What is the problem?
For a maximum of the minimum value of the index of the index, so the minimum value is wrong
  • Related