Write a C++ program main function which will prompt the user for the number of e
ID: 3625378 • Letter: W
Question
Write a C++ program main function which will prompt the user for the number of elements called N desired for an array which has the capacity to store 50 elements and performs the following:
1.calls a void function, createArray which creates an array of N random numbers from 1 to 100 using the system functions srand() and rand() and returns the newly created array.
2. calls a void function, printArray which prints an array of N numbers.
3. calls a value-returning function, avgArray which finds the average of an array of
N numbers.
4. calls a value-returning function, greaterElements which finds the percentage of
array elements greater than the average of the array
5. calls a value-returning function, lesserElements which finds the percentage of
array elements less than the average of the array
The main function will print the result of the percentages. All output should be labeled.
Your program should (better) contain comments as appropriate for each call/function.
Note:
You may select to place your function before or after main.
You should consider defining your array as defined below for greater flexibility:
const int limit = 50;
int myarray[limit];
Explanation / Answer
please rate - thanks
#include <iostream>
#include <time.h>
using namespace std;
void createArray(int[],int);
double avgArray(int[],int);
double greaterElements(int[],double,int);
double lesserElements(int[],double,int);
int main()
{const int limit = 50;
int i,n;
int myArray[limit];
double avg;
cout<<"How many elements do you want? ";
cin>>n;
createArray(myArray,n); //generate the numbers
cout<<"The numbers are: ";
for(i=0;i<n;i++) //print them
{cout<<myArray[i]<<" ";
if((i+1)%10==0) //print 10 elements per row
cout<<endl;
}
cout<<endl;
avg=avgArray(myArray,n); //get the average
cout<<"The average of the array is: "<<avg<<endl;
cout<<"The percentage of the numbers greater than the average is: "<<
greaterElements(myArray,avg,n)<<"% ";
cout<<"The percentage of the numbers less than the average is: "<<
lesserElements(myArray,avg,n)<<"% ";
system("pause");
return 0;
}
void createArray(int a[] ,int n)
{int i;
srand(time(0)); //see random number generator
for(i=0;i<n;i++)
a[i]=rand(); //fill the array with random numbers
}
double avgArray(int a[],int n)
{int i,sum=0;
for(i=0;i<n;i++)
sum+=a[i]; //add all the numbers
return (double)sum/n; //do real division to find the average
}
double greaterElements(int a[],double avg,int n)
{int i,count=0;
for(i=0;i<n;i++)
if(a[i]>avg) //compare each number to the average
count++; // and count it if it's greater;
return (double)count/n*100.; //do real division to find the percent
}
double lesserElements(int a[],double avg,int n)
{int i,count=0;
for(i=0;i<n;i++)
if(a[i]<avg) //compare each number to the average
count++; // and count it if it's less
return (double)count/n*100.; //do real division to find the percent
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.