Question

The goal of this homework is to introduce a Ruby environment and to practice basic programming....

The goal of this homework is to introduce a Ruby environment and to practice basic
programming.

1. Function rand(n+1) returns a random integer between 0 and n. Write a function that
creates an array of 100 random numbers between 0 and 10 [slide 143].

3. Make a function hist(v) that plots a histogram of values stored in array v. For example:
0 **********
1 ********
2 *********
3 ***********
4 *******
5 ************
6 ***********
7 *********
8 *************
9 ***********

10 *********

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

In case of any query do comment. Please rate answer as well. Thanks

Code:

#function to generate array of 100 elements between 0 to 10 values

def generateRandomArray(n)

    return Array.new(100) { rand(n+1) }

end

#print the histogram of values present in array

def hist(v)

   #run the for loop from 0 to 10, print the i and it's occurrence in array v

   for i in 0...11 do

        print (i)

        print(" ")

        #print * times i is present in array v

        v.count(i).times do

            print "*"

        end

        puts("") #print new line

        i +=1

    end

end

#main driver function

v = generateRandomArray(10)

hist(v)

===Output and screen shot of the code===

main.rb 1 #function to generate array of 100 elements between e to 10 values 2- def generateRandomArray (n) return Array.new(100) { rand (n+1) } 4 end 6 #print the histogram of values present in array 7- def hist(v) #run the for Loop from 0 to 10, print the i and it's occurence in array v for i in 0...11 do print (i) print(" ") #print * times i is present in array v v.count (i).times do print "*" 9- 10 11 12 13 14 15 end puts("") #print new Line i +=1 16 17 18 end 19 end 20 21 #main driver function v = generateRandomArray(10) 23 hist(v) 22 input 10 ** -..Program finished with exit code 0 Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
The goal of this homework is to introduce a Ruby environment and to practice basic programming....
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
  • Use C Programming DESCRIPTION Write a program that: Creates a Dimensional, integer array that holds 8...

    Use C Programming DESCRIPTION Write a program that: Creates a Dimensional, integer array that holds 8 values Prompts the user to enter 8 integers between 0 and 20 [inclusive) and stores the data in the array Prints the data in array abng with a histogram of the data as shown below. There is no requirement to validate the data. You must use a constant to declare your array and control any loops you need Sample Output: Enter 8 integer values...

  • Goal 2 lo introduce you IV The systematic problem-solving techniques CLO2: Use a basic scientific vocabulary...

    Goal 2 lo introduce you IV The systematic problem-solving techniques CLO2: Use a basic scientific vocabulary that relates to course content: aligns with (Goal 1) (Goal 2) CLO5: Use simple mathematical skills to solve problems which pertain to the physical environment: aligns with (Goal 1)(Goal 2) Unit Objectives: Use Newton's second law to translate a free-body diagram into a mathematical representation. (CLO2)(CLO5) Calculate mass, weight and force (CLO2)(CL05) Calculate the net force acting on objects and their resulting accelerations. (CLO2)(CLO5)...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • please solve it before 11:15 today Subject :C++ programming - Lab_9_Sec 51.pdf 1411113: Programming for Engineers...

    please solve it before 11:15 today Subject :C++ programming - Lab_9_Sec 51.pdf 1411113: Programming for Engineers Fall 2017/2018 LAB #9 Name: ID: Date Section Objectives: After completing this lab, you will be able to .Analyze a problem. .Implement the solution in C++ . Practice arrays and strings. Lab Exc. Mark Score Correct input/output Computational correctness Correct input/output Computational correctness Exercise#1: Number of occurrences in an array Write a program that fills an array with 10 random numbers between 0 and...

  • C programming for the below question.. In this program we are going to test the performance...

    C programming for the below question.. In this program we are going to test the performance of two searching codes. (a) Write a randomarray(n, max) function that returns a pointer to an array of size n integers, filled with random values between 0 and max. (b) Write a median(n, arr) function that returns the median of an integer array, without ordering them. The median value means that half the numbers are smaller or equal and half are bigger or equal....

  • C Programming. Write a prograrm that generates a "random walk" across a 10 × 10 array....

    C Programming. Write a prograrm that generates a "random walk" across a 10 × 10 array. The array will con- tain characters (all ' . ' initially). The program must randomly "walk" from element to ele- ment, always going up, down, left, or right by one element. The elements visited by the program will be labeled with the letters A through Z, in the order visited. Here's an example of the desired output: 9. BCD.. F E. H G KR...

  • Hi, I have programming problem related to array, sorting, and. swap operation Thank you, Best Regards.....

    Hi, I have programming problem related to array, sorting, and. swap operation Thank you, Best Regards.. A non-empty array A consisting of N integers is given. You can perform a single swap operation in array A. This operation takes two indices I and J, such that 0 S13 J<N, and exchanges the values of A[i] and A[J]. The goal is to check whether array A can be sorted into non-decreasing order by performing at most one swap operation. For example,...

  • This problem investigates a two-level randomization problem useful tor applications lke video game programming (Think: Programming a computer on easy" to play against you at tic-tac-loe, where it...

    This problem investigates a two-level randomization problem useful tor applications lke video game programming (Think: Programming a computer on easy" to play against you at tic-tac-loe, where its strategy is to place a mark in a random location.) rite a function called randLocation that takes a vector of any length as its input. The function places a 0 at a random location in the array such that every location is as equally likely The function outputs the new vector You...

  • Microsoft Excel VBA (Visual Basic for Applications) Programming Language Objectives: Create an array and redim it...

    Microsoft Excel VBA (Visual Basic for Applications) Programming Language Objectives: Create an array and redim it to a size equal to an assigned value of a variable, populate the array with a series of random numbers, output the array to a message box and to a worksheet. Instructions: - Review the variables already declared. You won't need others. - See comments in the code that will act as your guide. Add new code directly after each comment. - Assign a...

  • Please help me with this in C# language. Returning an array from a function The goal...

    Please help me with this in C# language. Returning an array from a function The goal for this exercise is to make sure that you return an array from a method (as a return value). Also: to give you more practice creating and using methods/functions. What you need to do for this exercise: In the starter project, add code to the Returning_An_Array class, so that the RunExercise method does the following: Declare an integer array variable, but DO NOT ALLOCATE...

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