Question

Write a program that takes a positive integer argument N, and prints out the average, minimum,...

Write a program that takes a positive
integer argument N, and prints out the average, minimum, and maximum (in that order) of N uniform random
numbers that it generates. Note that all three statistics should be over the same sequence of N random numbers.

Homework Answers

Answer #1

Here is the complete code in C++.

rand() function is used to generate the random number and it is stored in an integer array.

To find the average,the sum of all elements in the array is divided by number of array elements.

To find the minimum and maximum, the array is sorted in ascending order using Linear sort technique.After sorting, the minimum value will be present at first index of array(arr[0]) and maximum element will be present at the last index of array(arr[n-1]).

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    long long int sum=0; /* Initialize sum as 0;Sum is declared as long long int as the total of all random numbers might exceed the range of INT */
    cout<<"Enter the number of elements"<<endl; // taking number of integers input from user
    cin>>n;
   srand(time(0));
   int arr[n];
   for(int i=0;i<n;i++){
       int temp=rand()%INT_MAX; // generates a random number upto INT_MAX
       arr[i]=temp; // Stores the random number into array
       sum+=arr[i]; // Store the sum of the elements to find the average
}

int min=INT_MAX,max=INT_MIN,avg;
cout<<"Average = "<<(double)sum/n; // explicitly type casted to double because average might contain a decimal value also
// Sorting the elements of the array in ascending order
for(int i=1;i<n;i++)
{
    if(arr[i-1]>arr[i])
    {
        int temp=arr[i-1];
        arr[i-1]=arr[i];
        arr[i]=temp;
    }
}
cout<<"Minimum = "<<arr[0]<<endl; // First element of array stores the minimum value
cout<<"Maximum = "<<arr[n-1]<<endl; // Last element of array stores the maximum value
}
Know the answer?
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
Similar Questions
write a python program for a function that takes in an integer n and prints the...
write a python program for a function that takes in an integer n and prints the nth taylor approximation for the function f(x)=e^x at the point x=0 to x=1
For C++: a) Write a function is_prime that takes a positive integer X and returns 1...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1 if X is a prime number, or 1 if X is not a prime number. b) write a program that takes a positive integer N and prints all prime numbers from 2 to N by calling your function is_prime from part a.
Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the...
Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the nth string from the first string found on standard input. [MO6.2] Please note that you would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is returned...
Write a program in JAVA that prints the bounds ( maximum and minimum values) for all...
Write a program in JAVA that prints the bounds ( maximum and minimum values) for all integer data types in Java.
problem 1 Write a program that asks the user for an integer and then prints out...
problem 1 Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop. in c plus plus
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
C++ question: Write a recursive function that takes a non-negative integer as an argument and returns...
C++ question: Write a recursive function that takes a non-negative integer as an argument and returns an integer with the same digits of the argument in reverse order (i.e. in order from right to left). All trailing zeros in the argument number are omitted. For example, if the argument is 76038, it would return 83067 and if the argument is 45600 it returns 654.
In C programming language write a function that takes an integer n as input and prints...
In C programming language write a function that takes an integer n as input and prints the following pattern on the screen: 1 (n times) 2 (n-1 times) .n (1 time) For example, if n was 5, the function should print 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
Write a program in C that takes a file name as the argument on the command...
Write a program in C that takes a file name as the argument on the command line, and, if the file is a symbolic link, prints out the contents of that link (i.e. the file name that the link points to). If it is not a symbolic link, the program should print an error int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR :...
python a) Write a function, hailstone(), that takes an integer value n as a parameter, and...
python a) Write a function, hailstone(), that takes an integer value n as a parameter, and displays the hailstone sequence for the given integer. The hailstone sequence is determined as follows: if the value is even, divide by 2 (floor division) or if the value is odd, calculate 3 * n + 1. The function should display each value and continue updating the value until it becomes 1. b) Write a program to display the hailstone sequence of all integers...