Introdução

Este documento apresenta a resolução dos exercícios propostos na disciplina de Programação em RStudio, com foco em variáveis, estruturas de dados, decisões e repetições.


A) Manipulando Variáveis e Estruturas de Dados

Exercício 1

A <- 5
B <- 10
C <- 15

A + B
## [1] 15
A + C
## [1] 20
B + C
## [1] 25

Exercício 2

A <- 6
B <- 8
temp <- A
A <- B
B <- temp

A
## [1] 8
B
## [1] 6

A B

## Exercício 3


``` r
F <- 100
C <- (F - 32) * (5 / 9)
C
## [1] 37.77778

Exercício 4

X <- 9

# a
X^3 - 4
## [1] 725
# b
X %% 3
## [1] 0
# c
X^(X/3) + 2
## [1] 731
# d
sqrt(X^2)
## [1] 9

Exercício 5

numeros <-1:10
numeros
##  [1]  1  2  3  4  5  6  7  8  9 10

Exercício 6

dias <- c("domingo", "segunda", "terca", "quarta", "quinta", "sexta", "sabado")
dias
## [1] "domingo" "segunda" "terca"   "quarta"  "quinta"  "sexta"   "sabado"

Exercício 7

matriz <- matrix(1:9, nrow = 3, byrow = TRUE)
matriz
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Exercício 8

pessoas <- data.frame(
  nome = c("Ana", "Bruno", "Carlos"),
  idade = c(20, 25, 30),
  cidade = c("RJ", "SP", "MG")
)

pessoas
##     nome idade cidade
## 1    Ana    20     RJ
## 2  Bruno    25     SP
## 3 Carlos    30     MG

Exercício 9

rm(list = ls())

x <- 10
y <- 20
z <- 10

x > y
## [1] FALSE
x < y
## [1] TRUE
x >= z
## [1] TRUE
y <= z
## [1] FALSE
x == z
## [1] TRUE
x != y
## [1] TRUE

Exercício 10

rua <- "Rua das Flores"
numero <- "123"
bairro <- "Centro"

endereco <- paste(rua, numero, bairro, sep = ", ")
endereco
## [1] "Rua das Flores, 123, Centro"

B) Estruturas de Decisão e Repetição

Exercício 11

vetor <- 1:10
soma <- 0

for(i in 1:10){
  soma <- soma + vetor[i]
}

soma
## [1] 55

Exercício 12

media <- soma / length(vetor)
media
## [1] 5.5

Exercício 13

x <- 15
y <- 10

if(x > y){
  print("x é maior que y")
} else {
  print("y é maior que x")
}
## [1] "x é maior que y"

Exercício 14

if(x <= y){
  print("x é menor ou igual a y")
} else {
  print("x é maior que y")
}
## [1] "x é maior que y"

Exercício 15

soma_pares <- 0

for(i in vetor){
  if(i %% 2 == 0){
    soma_pares <- soma_pares + i
  }
}

soma_pares
## [1] 30

Exercício 16

n <- 5
fatorial <- 1

for(i in 1:n){
  fatorial <- fatorial * i
}

fatorial
## [1] 120

Exercício 17

x <- matrix(1, nrow = 4, ncol = 4)
x
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    1    1
## [2,]    1    1    1    1
## [3,]    1    1    1    1
## [4,]    1    1    1    1

Exercício 18

x <- matrix(1:16, nrow = 4, byrow = TRUE)
x
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
## [4,]   13   14   15   16

Exercício 19

df <- as.data.frame(x)
df
##   V1 V2 V3 V4
## 1  1  2  3  4
## 2  5  6  7  8
## 3  9 10 11 12
## 4 13 14 15 16

Exercício 20

# Média coluna 1
mean(x[,1])
## [1] 7
# Média colunas
apply(x, 2, mean)
## [1]  7  8  9 10
# Média linhas
apply(x, 1, mean)
## [1]  2.5  6.5 10.5 14.5
# Média pares por coluna
apply(x, 2, function(col) mean(col[col %% 2 == 0]))
## [1] NaN   8 NaN  10
# Média ímpares por linha
apply(x, 1, function(lin) mean(lin[lin %% 2 != 0]))
## [1]  2  6 10 14
# Soma diagonal principal
sum(diag(x))
## [1] 34
# Soma diagonal secundária
sum(diag(apply(x, 2, rev)))
## [1] 34

Exercício 21

for(i in 1:nrow(df)){
  for(j in 1:ncol(df)){
    if(df[i,j] <= 10){
      print(paste(df[i,j], "<= 10"))
    } else {
      print(paste(df[i,j], "> 10"))
    }
  }
}
## [1] "1 <= 10"
## [1] "2 <= 10"
## [1] "3 <= 10"
## [1] "4 <= 10"
## [1] "5 <= 10"
## [1] "6 <= 10"
## [1] "7 <= 10"
## [1] "8 <= 10"
## [1] "9 <= 10"
## [1] "10 <= 10"
## [1] "11 > 10"
## [1] "12 > 10"
## [1] "13 > 10"
## [1] "14 > 10"
## [1] "15 > 10"
## [1] "16 > 10"

Exercício 22

for(i in 1:10){
  print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10

Exercício 23

vetor <- 1:100
pares <- vetor[vetor %% 2 == 0]
pares
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38
## [20]  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76
## [39]  78  80  82  84  86  88  90  92  94  96  98 100

Exercício 24

maior_elemento <- function(v){
  max(v)
}

maior_elemento(c(10,5,8,2,20,15))
## [1] 20