128k views
5 votes
C++

Write a program that dynamically allocates an array, on the Heap, large enough to hold 100 test scores between 55 and 99 -- using a Random Number generator to populate the array. Then do the following:

1) Sort scores in ascending order.
2) List your scores in rows of ten(10) values.
3) Calculate the Mean for the distribution.
4) Calculate the Variance for the distribution.
5) Calculate the Median for the distribution.
6) Calculate the Mode for the distribution.
7) Determine the number of scores in each of
the discrete intervals specified below:

55-59|**
60-64|***
65-69|*****
70-74|******
75-79|***********
80-84|****
85-89|***
90-94|**
95-99|*
|----|----|----|----|----|----|----|----|
0 5 10 15 20 25 30

7a) List the number of scores in each interval.

7b) Display your Histogram Chart distribution to the Console screen.

1 Answer

2 votes

Answer:

Check the explanation

Step-by-step explanation:

The Code

#include <fstream>

#include <iostream>

#include <cmath>

#include <cstring>

#include <cstdlib>

#include <ctime>

using namespace std;

//Function Declarations

void fillArrayWithRandNos(int nos[],int size);

void displayArray(int nos[],int size);

double mean(int nos[],int size);

double variance(int nos[],int size);

double median(int nos[],int size);

int mode(int nos[],int size);

void histogram(int nos[],int size);

int main() {

//Declaring variables

const int size=100;

srand(time(NULL));

// Creating array dynamically

int* nos = new int[size];

//Calling the functions

fillArrayWithRandNos(nos,size);

displayArray(nos,size);

cout<<"Mean :"<<mean(nos,size)<<endl;

cout<<"variance :"<<variance(nos,size)<<endl;

cout<<"Median :"<<median(nos,size)<<endl;

cout<<"Mode :"<<mode(nos,size)<<endl;

histogram(nos,size);

return 0;

}

void fillArrayWithRandNos(int nos[],int size)

{

for(int i=0;i<size;i++)

{

nos[i]=rand()%(45) + 55;

}

}

void displayArray(int nos[],int size)

{

cout<<"Displaying the array elements :"<<endl;

for(int i=0;i<size;i++)

{

cout<<nos[i]<<" ";

if((i+1)%10==0)

cout<<endl;

}

}

double mean(int nos[],int size)

{

double sum=0;

for(int i=0;i<size;i++)

{

sum+=nos[i];

}

return sum/size;

}

double variance(int nos[],int size)

{

double avg=mean(nos,size);

double variance,sum=0;

for(int i=0;i<size;i++)

{

sum+=pow(nos[i]-avg,2);

}

//calculating the standard deviation of nos[] array

variance=(double)sum/(size);

return variance;

}

double median(int nos[],int size)

{

//This Logic will Sort the Array of elements in Ascending order

int temp;

for (int i = 0; i < size; i++)

{

for (int j = i + 1; j < size; j++)

{

if (nos[i] > nos[j])

{

temp = nos[i];

nos[i] = nos[j];

nos[j] = temp;

}

}

}

int middle;

float med;

middle = (size / 2.0);

if (size % 2 == 0)

med = ((nos[middle - 1]) + (nos[middle])) / 2.0;

else

med = (nos[middle]);

return med;

}

int mode(int nos[],int size)

{

int counter1 = 0, counter2, modevalue;

for (int i = 0; i < size; i++) {

counter2 = 0;

for (int j = i; j < size; j++) {

if (nos[i] == nos[j]) {

counter2++;

}

if (counter2 > counter1) {

counter1 = counter2;

modevalue = nos[i];

}

}

}

if (counter1 > 1)

return modevalue;

else

return 0;

}

void histogram(int nos[],int size)

{

int hist[9]={0};

for(int i=0;i<size;i++)

{

if(nos[i]>=55 && nos[i]<=59)

{

hist[0]++;

}

else if(nos[i]>=60 && nos[i]<=64)

{

hist[1]++;

}

else if(nos[i]>=65 && nos[i]<=69)

{

hist[2]++;

}

else if(nos[i]>=70 && nos[i]<=74)

{

hist[3]++;

}

else if(nos[i]>=75 && nos[i]<=79)

{

hist[4]++;

}

else if(nos[i]>=80 && nos[i]<=84)

{

hist[5]++;

}

else if(nos[i]>=85 && nos[i]<=89)

{

hist[6]++;

}

else if(nos[i]>=90 && nos[i]<=94)

{

hist[7]++;

}

else if(nos[i]>=95 && nos[i]<=99)

{

hist[8]++;

}

}

cout<<"Displaying the count of numbers in each interval:"<<endl;

int cnt=0;

for(int i=55;i<=99;i+=5)

cout<<i<<"-"<<i+4<<"

cout<<"Displaying the histogram :"<<endl;

cnt=0;

for(int i=55;i<=99;i+=5)

{

cout<<i<<"-"<<i+4<<"|";

for(int j=0;j<hist[cnt];j++)

{

cout<<"*";

}

cout<<endl;

cnt++;

}

}

#########

___________________________

The output can be seen in the attached image below.

C++ Write a program that dynamically allocates an array, on the Heap, large enough-example-1
User Cassio
by
5.7k points