51.8k views
2 votes
Write a loop that will compute the cubes of all integers 5 through 33, but store the results in a vector, named cubes. Display the contents of cubes after you create it. Answer in R Code

1 Answer

6 votes

Final answer:

To create a vector of cubes for integers 5 through 33 in R, use a loop to calculate each cube and store it in the vector, then display the vector.

Step-by-step explanation:

To compute the cubes of all integers from 5 through 33 and store the results in a vector named cubes in R, you can use the following loop:


cubes <- vector()
for (i in 5:33) {
cubes <- c(cubes, i^3)
}
print(cubes)

After the loop execution, the cubes vector will contain the cubes of numbers starting at 5 and ending at 33. The print function is then used to display the contents of the cubes vector.

To compute the cubes of all integers from 5 to 33, you can use a for loop in R. Here's the code:

cubes <- vector()

for (i in 5:33) {

cubes <- c(cubes, i^3)

}

print(cubes)

When you run this code, it will create a vector called cubes and store the cubes of the integers from 5 to 33. The contents of the vector will then be displayed using the print() function.

User Marc Grue
by
8.5k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.