The function rand() generates a random integer between 1 and 32767. To generate
ID: 3624259 • Letter: T
Question
The function rand() generates a random integer between 1 and 32767. To generate a random value in a given range, we can take the modulo (%) of a random value to the range that we want. For example, rand()%100 generates a random value between 0 and 99. You are given the task of evaluating the statistics of this built-in random value generator. First, ask the user how many values to generate. Then, using a loop, generate that many random values between 1 and 100 and put them into an array. You may need to indicate to the user the maximum number of random values your program will allow.Write a function consistent with the following prototype:
GetMean(* in * int rArray[], \ the array to analyze
* in * int n, \ the number of values
*out * float& mean \ the result
);
\ Calculates the mean value of an array
\ Preconditions: rArray[] contains int values, and
\ n contains the number of values in rArray[]
\ Postconditions: mean will contain the average of rArray[]
\ as calculated by dividing the sum of the
\ values in rArray[] by n
Also, in a second array, make a set of counters which will indicate number of values in each “10s” range bin (1-10, 11-20 etc). This will be a histogram. Write another function called MakeHistogram(int array[], int n, int histogram[]) which will receive your original random number array as input and count the number of values for each bin and put the result in the histogram array. Write a third function called DisplayHistogram(int histogram[]) which will display the histogram.
Once you have generated the values, found the mean and calculated the histogram, present the results to the user. An example exchange might look like this:
How many random numbers to generate?
>> 30
The average was 50.3
The histogram:
1-10: 3
11-20: 4
21-30: 3
31-40: 2
41-50: 3
51-60: 2
61-70: 3
71-80: 4
81-90: 4
91-100: 3
Explanation / Answer
Module Module1
Sub Main()
Dim rand As New Random
Dim numbers As Integer
Dim randNumber(50) As Integer
Dim mean As Double
Console.WriteLine("How many random numbers to generate?")
numbers = Console.ReadLine()
Dim i As Integer
For i = 0 To numbers - 1
randNumber(i) = rand.Next(1, 100)
Next i
For i = 0 To numbers - 1
Console.Write(" " & randNumber(i) & " ")
Next i
mean = GetMean(randNumber, numbers)
Console.WriteLine("Mean" & mean & " ")
DisplayHistogram(randNumber, numbers)
Console.Read()
End Sub
Function GetMean(ByVal values As Array, ByVal size As Integer) As Double
Dim sum As Double
Dim i As Integer
sum = 0
For i = 0 To size - 1
sum = sum + values(i)
Next i
Return (sum / size)
End Function
Function DisplayHistogram(ByVal values As Array, ByVal size As Integer)
Dim i As Integer
Dim hist(10) As Integer
For i = 0 To size - 1
hist(i) = 0
Next i
For i = 0 To size - 1
If (values(i) <= 10) Then
hist(0) = hist(0) + 1
ElseIf (values(i) >= 11 And values(i) <= 20) Then
hist(1) = hist(1) + 1
ElseIf (values(i) >= 21 And values(i) <= 30) Then
hist(2) = hist(2) + 1
ElseIf (values(i) >= 31 And values(i) <= 40) Then
hist(3) = hist(3) + 1
ElseIf (values(i) >= 41 And values(i) <= 50) Then
hist(4) = hist(4) + 1
ElseIf (values(i) >= 51 And values(i) <= 60) Then
hist(5) = hist(5) + 1
ElseIf (values(i) >= 61 And values(i) <= 70) Then
hist(1) = hist(6) + 1
ElseIf (values(i) >= 71 And values(i) <= 80) Then
hist(1) = hist(7) + 1
ElseIf (values(i) >= 81 And values(i) <= 90) Then
hist(1) = hist(8) + 1
ElseIf (values(i) >= 91 And values(i) <= 100) Then
hist(9) = hist(9) + 1
End If
Next i
Dim j As Integer
j = 1
For i = 0 To 9
Console.WriteLine(j & "-" & (j + 9) & ":" & hist(i))
j = j + 10
Next i
End Function
End Module
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.