-
Notifications
You must be signed in to change notification settings - Fork 1
/
repelearray.go
42 lines (34 loc) · 861 Bytes
/
repelearray.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//replacing an element in slice
package main
import(
"fmt"
)
func main(){
//declaring variables
var len,ele,loc int
//defining length of slice
fmt.Printf("Enter the length: ");
fmt.Scanf("%d %d",&len);
//declaring a slice
slc:=make([]int, len);
//entering the elements
fmt.Printf("\nEnter the elements: ")
for i:=0;i<len;i++{
fmt.Scanf("%d",&slc[i])
}
//Input the element and loc
fmt.Printf("\nEnter the location and element(0-%d): ",len-1)
fmt.Scanf("%d %d",&loc,&ele)
//replacing the element at loc
if loc>len || loc<0 {
fmt.Println("\nInvalid Input!")
}else{
fmt.Println(slc[loc]," has been successfully replaced by ",ele,"!")
slc[loc]=ele;
}
//displaying the modified slice
fmt.Printf("\nThe slice after replacement are: ")
for i:=0;i<len;i++{
fmt.Printf("%d, ",slc[i])
}
}