Está en la página 1de 5

#tablero de ajedrez

n <- 40 # numero de filas y/o columnas

M <- matrix(0, nrow =n, ncol = m)

fil <- rep(rep(c(0,1), each=5),4)

id <- 1:n

x <- id[fil== 1]

y <- id[fil== 0]

z <- expand.grid(x=x, y=y)

M[as.matrix(z)] <- 1

z <- expand.grid(y=y, x=x)

M[as.matrix(z)] <- 1

view(M)

#funciones

nombre_funcion <- function(){

#cuerpo

return()

#ejemplo1

func1 <- function(x){

if(x < 0)}

y <- x^2

}else{ # x>=0

y <- x

}
#ejemplo2

func2 <- function(x, k=0){

if(x > k){

y <- 1

}else{

if(x == k){

y <- 0

}else{

y <- -1

return(y)

func2(3, k=5)

func2(-2, k=5)
# sentencias de control

#for

for(i in 1:n){

#cuerpo

#ejemplo1

n <- 20

for(i in 1:0){

print(i)

#ejemplo

n <- 100

s <- 0

for(i in 1:n){

s <- s + i

#while

while () {

#ejemplo

x <- 0

while(x<10){

print(x)

x <- x+1

}
#ejemplo 3 con while en vez del for

i<- 1

s<- 0

while(i<=100){

s <- s + i

i <- i+1

# repetir

repeat{

#cuerpo

break

#ejemplo1

i <- 0

repeat{

if(i<10){

print(i)

i <- i + 1

}else{

break

#ejemplo2
i <- 1

s <- 0

repat{

if(i<= n){

s <- s + i

i <- i + 1

}else{

break

También podría gustarte