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 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.
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.
In C++ Using recursion write a program that takes a positive integer number and returns whether...
In C++ Using recursion write a program that takes a positive integer number and returns whether there is 6. For example, if the input number is 7068, the function returns true
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between 1 and 100. 2- Moves all multiple of 3-numbers in the array that created in part-a into a new array. 3- Moves all multiple of 5-numbers in the array that created in part-a into a new array. 4- Find the maximum and the minimum multiple of 3-numbers, if exist. 5- Find the maximum and the minimum multiple of 5-numbers, if exist. 6- Prints the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT