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
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.
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 :...
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 full C++ program to read a series of integer numbers from standard input and...
Write a full C++ program to read a series of integer numbers from standard input and print them out in reverse order, one per line. You may assume that there will be a minimum of 2 numbers to read and a maximum of 200.
In Matlab, write a function that takes a relatively small (less than 100 billion) positive integer...
In Matlab, write a function that takes a relatively small (less than 100 billion) positive integer input and outputs the prime factorization of that integer using as few built-in functions as possible. Explain the processes with comments. NOTE: Do not use the functions factor(n), primes(n), or isprime(n). You should be able to write out what these functions do yourself if you want to be able to do something similar.
Write a program that takes n integer numbers from the user, and then counts the number...
Write a program that takes n integer numbers from the user, and then counts the number of even numbers and odd numbers and print them to the screen. Sample Output: Enter how many numbers you have: 10 Enter the 10 numbers: 1 3 19 50 4 10 75 20 68 100 The number of even numbers is: 6 The number of odd numbers is: 4 (( in java ))
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT