Question

1. Use a for or while loop to assign ID numbers for each student. To make...

1. Use a for or while loop to assign ID numbers for each student. To make it simple, ID numbers should range from 1 to 18.

2. .Write another loop with if-statement(s) nested inside, to place the students from Problem 1 into 6 groups of three (e.g. Students with ID numbers 1,7 and 13 go into one group, ID numbers 2,8 and 14 go into a second group, etc.).

3.

Write a function that outputs the area of a geometric shape. The function should accept three variables. The first variable tells you whether the shape is a triangle or rectangle, the second variable tells you the height of the shape and the third variable tells you the base of the shape.

The output should look something like:

> ZC_Shape_Area("Tri",10,3)
[1] "Area = 15"
>

Can you please provide solution in R programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here are the answers for your questions in R Programming Language.

Kindly upvote if you find the answer helpful.

Question No. 1

NOTE : I have used a vector to assign student id's, each element in the vector represents each student's ID.

CODE :

#Create an empty vector to store student Id's
studentId <- c()

#For loop that loops for 18 times
for( i in seq(1, 18, by = 1)){

#Assigns value of i to vector each time
#Value of i is from 1 to 18 and increases by 1
#each time the loop executes
studentId[[i]] <- i
}

#prints the id's of students
print(studentId)

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

1 #Create an empty vector to store student Ids 2 studentId <- c() 3 4 #For Loop that loops for 18 times for( i in seq(1, 18,

OUTPUT :

$Rscript main.r [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Question No. 2:

NOTE : This code is continuation of question no.1's code

CODE :

#Create an empty vector to store student Id's
studentId <- c()

#For loop that loops for 18 times
for( i in seq(1, 18, by = 1)){

#Assigns value of i to vector each time
#Value of i is from 1 to 18 and increases by 1
#each time the loop executes
studentId[[i]] <- i
}

#prints the id's of students
print(studentId)

#Vector that stores groups of students
groups <- c()

#for loop that loops for 6 times
for(i in seq(1, 6, by = 1)){

#Store value of i in j
j <- i

#Create a new vector i.e., this represents a group
newGroup <- c()

#Assign value of j to new group
newGroup[[1]] <- j

#Increment j value by 6
j = j + 6
newGroup[[2]] <- j
j = j + 6
newGroup[[3]] <- j

#Assign new group to groups
groups[[i]] <- newGroup
}

#print groups
print(groups)

SCREENSHOTS :

1 2 3 4 9 12 #Create an empty vector to store student Ids studentId <- c() #For Loop that loops for 18 times 5 for(i in seq(

newGroup <- c() #Assign value of j to new group newGroup[[1]] <- j 26 27 28 29 30 31 32 33 34 35 36 37 38 39} #Increment j va

OUTPUT :

$Rscript main.r [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [[1]] [1] 1 7 13 [[2]] [1] 2 8 14 [[3]] [1] 3 9 15 [[4]] [1]

Question No. 3

CODE :

#Function with name ZC_Shape_Area
#That takes 3 parameters name and two dimension values
ZC_Shape_Area = function(name, x, y){
#if the name is equal to 'Tri'
if(all(name == "Tri")){
#Prints area of triangle
cat("Area = ",(x*y)/2)
}
#if the name is equal to 'Rect'
else if(all(name == "Rect")){
#Prints area of triangle
cat("Area = ",(x*y))
}
#if the name is neither 'Rect' nor 'Tri'
else{
#Prints the shape is invalid
print("Invalid shape name")
}
}
#Function call
ZC_Shape_Area("Tri",10,3)

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

} 1 #Function with name ZC_Shape_Area 2 #That takes 3 parameters name and two dimension values 3 ZC_Shape_Area function(name,

OUTPUT :

$Rscript main.r Area = 15

Any doubts regarding this can be explained with pleasure :)

Add a comment
Know the answer?
Add Answer to:
1. Use a for or while loop to assign ID numbers for each student. To make...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) Given two positive numbers, write a program using while loop to determine their lowest common...

    1) Given two positive numbers, write a program using while loop to determine their lowest common multiple. You cannot use any automatic LCM, GCD finder function or return command. Program Inputs • Enter the first number: – The user can enter any positive whole number, no error checking required • Enter the second number: – The user can enter any positive whole number, no error checking required Program Outputs • The LCM of X and Y is Z! – Replace...

  • Objectives: Use the while loop in a program Use the do-while loop in a second program...

    Objectives: Use the while loop in a program Use the do-while loop in a second program Use the for loop in a third program Instructions: Part A. Code a for loop to print the Celsius temperatures for Fahrenheit temperatures 25 to 125. C = 5/9(F – 32).Output should be in a table format: Fahrenheit Celsius 25 ? . . . . . . . . Turn in the source and the output from Part A. Part B. Code a while...

  • We should use “Java” language. Java Eclipse :) Fall 2019 1. Assign each line to a...

    We should use “Java” language. Java Eclipse :) Fall 2019 1. Assign each line to a variable and write the passage from Shakespeare's Sonnet #52 to console (Careful about line breaking): Blessed are you whose worthiness gives scope, Being had, to triumph; being lacked, to hope. 2. Create two integer variable, width and height. Write them to console in similar format, i.e. Width of rectangle is 5. 3. Update width and height variable with another value, and write them to...

  • PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one...

    PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one function named multiply. This function should accept one parameter, which you can assume will be a list with one or more integers in it. (If you don’t know what I mean when I say “list of integers”, go do the reading!) The function should loop through all of the numbers in the list, and multiply all of the numbers together. It should return the...

  • *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented...

    *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented programming using classes to create objects. PROBLEM DEFINITION: Write a class named Employee that holds the following data about an employee in attributes: name, IDnumber, department, jobTitle The class should have 8 methods as follows:  For each attribute, there should be a method that takes a parameter to be assigned to the attribute. This is known as a mutator method.  For each...

  • 1- Write a program to read 10 numbers and find the average of these numbers. Use...

    1- Write a program to read 10 numbers and find the average of these numbers. Use a while loop to read these numbers and keep track of input numbers read. Terminate the while loop with a sentinel value. 2- Now modify the program to find the maximum of these numbers as well. The program should print the number of elements read as input and run until -1 is entered. (-1 is the sentinel that terminates the loop and the program)...

  • Using C++ 1. Create a while loop that counts even numbers from 2 to 10 2....

    Using C++ 1. Create a while loop that counts even numbers from 2 to 10 2. Create a for loop that counts by five (i.e. 0, 5, 10, ...) from 0 to 100 3. Ask for a person's age and give them three tries to give you a correct age (between 0 and 100) 4. Use a for loop to list Celsius and Fahrenheit temperatures. The "C" should be from -20 to 20 and the F should be shown correspondingly...

  • Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You...

    Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You should implement the following functions to print a rectangle shape. • print_rectangle(length, width, fillChar, hollow). This function will use draw_shape_line() function to print a rectangle shape with the given length and width, If the hollow is true, the shape will be hollow rectangle, otherwise a filled rectangle. This function must not interact with the user. For example, the call print_rectangle(5, 10, '*', False) should...

  • Piggy back on the programming project one you completed in module 3. You will use some...

    Piggy back on the programming project one you completed in module 3. You will use some of those concepts here again. We will be building this project inside out, start individual quiz and use a for loop to repeat it three times for three students. In this program you will be required to write a python program that generates math quizzes for students in second grade. Your program should do the following Ask the student for their name Provide 3...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT