Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that can be used to gather statistical data about the number of

ID: 3714102 • Letter: W

Question

Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should perform the following steps: Ask the user how many students were surveyed (not less than zero). An array of integers the size of the number of students should be dynamically allocated. Allow the user to enter the number of movies each student saw into the array (between 0 and 100, inclusive). Calculate the average of the values entered. Display the result Notes: Whenever accessing dyn_array, use pointer notation in place of regular square array brackets. Use the following skeleton for the main program: For copy and paste

int main()

{ int *dyn_array;

int students;

float avrg;

do {

cout << "How many students will you enter? ";

cin >> students; }while ( students <= 0 );

dyn_array = create_array( students ); //this function creates a dynamic array //and returns its pointer

enter_data( dyn_array, students );

//use 'pointer' notation in this function to

//access array elements

//accept only numbers 0-100 for movie seen

avrg = find_average( dyn_array, students );

//use 'pointer' notation in this function to

//access array elements

cout << "The array is:" << endl << endl;

show_array( dyn_array, students);

cout << endl;

cout << "The average is " << avrg << ". ";

delete [] dyn_array; return 0;

}

Don't forget to block-comment every function definition.

Explanation / Answer

#include <iostream>

using namespace std;
int * create_array(int);
void enter_data(int *,int);
float find_average(int *,int);
void show_array(int *,int);
int main()

{
int *dyn_array;

int students;

float avrg;

do {

cout << "How many students will you enter? ";

cin >> students;

}while ( students <= 0 );

dyn_array = create_array(students); //this function creates a dynamic array
//and returns its pointer
enter_data(dyn_array,students);

//use 'pointer' notation in this function to

//access array elements

//accept only numbers 0-100 for movie seen
avrg = find_average( dyn_array, students );

//use 'pointer' notation in this function to

//access array elements
cout << "The array is:" << endl << endl;

show_array(dyn_array,students);

cout << endl;

cout << "The average is " << avrg << ". ";

delete [] dyn_array;
return 0;

}

int * create_array(int n)
{
int * f;
// allocates memory for n number of students if fails nullptr returns
f = new (nothrow) int [n];
return f;
}

void enter_data(int *f,int n)
{
int i,k;
cout << "enter number of movies by each student";
cout << "enter the value between 0 and 100";
cout << endl;
for(i=0;i<n;i++)
{
cin >> k;
cout << endl;
// if the data doesn't exist between 0 and 100 then prompts for re-entering data
if(k>=0 && k<=100)
*(f+i)=k;
else
{
cout << "Re-enter the data between 0 and 100";
cout << endl;
i=i-1;
}
}

}
float find_average(int *f,int n)
{
int i,total=0;
float avgg;
//calculates the sum
for(i=0;i<n;i++)
{  
total+=*(f+i);
}
//calculates average
avgg=(float)total/n;
return avgg;
}

void show_array(int *f,int n)
{
int i;
for(i=0;i<n;i++)
{  
//displays the number of movies watched by the student
cout << *(f+i);
cout << endl;
}
}

How many students will you enter? 5                                                                                              

enter number of movies by each studententer the value between 0 and 100                                                          

4                                                                                                                                

                                                                                                                                 

3                                                                                                                                

                                                                                                                                 

-1                                                                                                                               

                                                                                                                                 

Re-enter the data between 0 and 100                                                                                              

3                                                                                                                                

                                                                                                                                 

4                                                                                                                                

                                                                                                                                 

5

The array is:                                                                                                                    

                                                                                                                                 

4                                                                                                                                

3                                                                                                                                

3                                                                                                                                

4                                                                                                                                

5                                                                                                                                

                                                                                                                                 

The average is 3.8.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote