Question

Count Below and Above Average Numbers

Use the following main( ) function:

const int maxSize = 50;

int main()

{

int l, u, n, A[maxSize], le, mo;

input(l, u);

cout << "How many numbers you want to generate < 50: ";

cin >> n;

generateNumbers(n, l, u, A);

le = countLessMore(n, A, mo);

cout << "There are " << le << " numbers less than the average and "

   << mo << " numbers more than the average" << endl;

return 0;

}

Define the following functions:

void input(int &, int &) // reads the lower L and upper U limits of an interval and

// validates that the limits satisfy 0 < L < U < 100.

void generateNumbers(int, int, int, int []) // generates n < 50 random numbers between L and U // inclusive. The function saves the random numbers

// in an array.

int countLessMore(int, int [], int &) // calculates and prints the average of the n random // numbers, it also counts how many numbers are less // than the average and how many are greater than or // equal to the average.

Format the output as in the sample input/output below and make sure that the program generates different numbers in each run:

Enter the value of lower, o< lower < 100: -6 Enter the value of lower, o< lower < 100: 27 Enter the value of upper, 27 < uppe

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

const int maxSize = 50;

void input(int &lower, int &upper);
void generateNumbers(int cnt, int lower, int upper, int nos[]);
int countLessMore(int cnt, int nos[], int &cntAbove);
int main()

{

int l, u, n, A[maxSize], le, mo;

input(l, u);

cout << "How many numbers you want to generate < 50: ";

cin >> n;

generateNumbers(n, l, u, A);

le = countLessMore(n, A, mo);

cout << "There are " << le << " numbers less than the average and "

<< mo << " numbers more than the average" << endl;

return 0;

}


// reads the lower L and upper U limits of an interval and
// validates that the limits satisfy 0 < L < U < 100.
void input(int &lower, int &upper)
{
   while(true)
   {
   cout<<"Enter the value of lower , 0 < lower < 100:";
   cin>>lower;
   if(lower>0 && lower<100)
   {
       break;
   }      
   }
  
   while(true)
   {
   cout<<"Enter the value of upper , "<<lower<<" < upper < 100:";
   cin>>upper;
   if(upper>lower && upper<100)
   {
       break;
   }      
   }
}

// generates n < 50 random numbers between L and U
// inclusive. The function saves the random numbers
// in an array.
void generateNumbers(int cnt, int lower, int upper, int nos[])
{
   srand(time(NULL));
   for(int i=0;i<cnt;i++)
   {
       nos[i]=rand()%( upper - lower + 1 ) + lower;
   }
  
   cout<<"The generated numbers are :";
   for(int i=0;i<cnt;i++)
   {
       cout<<nos[i]<<" ";
   }
   cout<<endl;
}


// calculates and prints the average of the n random
// numbers, it also counts how many numbers are less
// than the average and how many are greater than or
// equal to the average.
int countLessMore(int cnt, int nos[], int &cntAbove)
{
  
   double avg=0,sum=0;
   int le=0;
   for(int i=0;i<cnt;i++)
   {
       sum+=nos[i];
   }
   avg=sum/cnt;
  
   cout<<"The average is "<<avg<<endl;
   for(int i=0;i<cnt;i++)
   {
       if(nos[i]<avg)
       {
           le++;
       }
       else if(nos[i]>avg)
       {
           cntAbove++;
       }
   }
  
   return le;
}

_________________________

Output:

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\ReadUpperLowerLimitsCalculatesAverageCountUpperLowerThanAvg.exe Enter the value of

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Count Below and Above Average Numbers Use the following main( ) function: const int maxSize =...
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
  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • 13. Write a recursive function with the declaration: int count Equal (int* numbers, int n, int...

    13. Write a recursive function with the declaration: int count Equal (int* numbers, int n, int x) that has as parameters an array numbers with n > 0 elements, and an integer x, and returns how many times I appears in the array. 14. Write a recursive function with the declaration: double dist(double* u, double *v, int n) that gets two double precision arrays with n > 1 elements and returns the value: Vu[0] - v[0])2 + (u[1] – v[1])2...

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • First create the two text file given below. Then complete the main that is given. There...

    First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...

  • use c++ language, keep it simple i am using code block Exercise#2: Arrays with Random Numbers...

    use c++ language, keep it simple i am using code block Exercise#2: Arrays with Random Numbers Write a program that generates n random numbers as follows: Asks the user to input a positive integer n Asks the user to input the maximum value (say m) for the integers to be generated. Write a program that generates the n numbers in the range 0 to m. The program stores the generated numbers in an array A[ Asks the user to enter...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • 1. The following program calls the function countLarger that accepts three arguments: an integer array, an...

    1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

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