The purpose of this assignment is to provide you with experience in working with
ID: 3716775 • Letter: T
Question
The purpose of this assignment is to provide you with experience in working with pointers and dynamic memory allocation. The problem description is as follows. Additional specifications are shown in blue text.
This assignment is not implemented using classes. It is written in a single program
7. Movie Statistics
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 ask the user how many students were surveyed and dynamically allocate an array of that size. The program should then allow the user to enter the number of movies each student has seen. The program should then calculate the average of the values entered.
Create the following functions:
printArray. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should print out the contents of the array.
getAverage. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should return the average as a double value.
sortArray. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It sorts the array sent into it and returns nothing.
getMaximum. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should return the maximum value within the array as an int value.
Make sure that user enters positive numbers for # of students and number of movies seen by each student
Note that you only have to calculate average, not median and mode of the values entered.
This assignment does not require you to create classes. Code the assignment in a single cpp file.
Approach 1 - Use "regular" pointers
In this approach, an integer pointer variable is declared. An array (sized at the number entered by the user) is created and the memory address of this array is placed in the pointer variable. With this approach, remember to deallocate the dynamically created array.
Approach 2 - use Smart Pointers
You may use unique_ptr to declare a smart pointer to hold the address of the dynamically created array. The syntax to declare such a pointer and to set it to point to an array is as follows:
1
2
unique_ptr<int[]> dynamic_array;
dynamic_array = make_unique<int[]>(5);
The statements shown above create a 5 element array dynamically and place the address of this array in the dynamic pointer. In this assignment, a variable is used instead of the numeric literal "5".
When using smart pointer, remember the following 2, very important, issues:
Smart points MUST be passed into function by reference
Dynamic arrays managed by unique pointer do not have to be "deleted".
Sample output from my version of Assignment 5
User input is shown in bold, blue, highlighted text.
The main function's pseudocode is as follows:
In int main
ask user for number of student to be surveyed
ensure that the number is > 0 (Do not check for non-numeric input, test with numbers)
Setup dynamic array
ask user to enter movies seen by each of the students
ensure that number of movies seen by each student is > 0 (Do not check for non-numeric input, test with numbers)
Invoke these functions:
printArray function, passing in the appropriate arguments
sortArray function, passing in the appropriate arguments
printArray function after the sortArray function has been invoked
getAverage function, passing in the appropriate arguments, and print out the value returned by this function
getMaximum function, passing in the appropriate arguments, and print out the value returned by this function
7. Movie Statistics
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 ask the user how many students were surveyed and dynamically allocate an array of that size. The program should then allow the user to enter the number of movies each student has seen. The program should then calculate the average of the values entered.
Create the following functions:
printArray. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should print out the contents of the array.
getAverage. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should return the average as a double value.
sortArray. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It sorts the array sent into it and returns nothing.
getMaximum. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should return the maximum value within the array as an int value.
Make sure that user enters positive numbers for # of students and number of movies seen by each student
Explanation / Answer
SOurceCode:
#include <iostream>
using namespace std;
double median(int *, int);
int get_mode(int *, int);
int *create_array(int);
void getinfo(int *, int);
void sort(int [], int);
double average(int *, int);
int getrange(int *,int);
int main()
{ int *dyn_array;
int students;
int mode,i,range;
float avrg;
do
{cout << "How many students will you enter? ";
cin >> students;
}while ( students <= 0 );
dyn_array = create_array( students );
getinfo(dyn_array, students);
cout<<" The array is: ";
for(i=0;i<students;i++)
cout<<"student "<<i+1<<" saw "<<*(dyn_array+i)<<" movies. ";
sort(dyn_array, students);
cout << " the median is "<<median(dyn_array, students) << endl;
cout << "the average is "<<average(dyn_array, students) << endl;
mode = get_mode(dyn_array, students);
if (mode == -1)
cout << "no mode. ";
else
cout << "The mode is " << mode << endl;
cout<<"The range of movies seen is "<<getrange(dyn_array,students)<<endl;
delete [] dyn_array;
system("pause");
return 0;
}
void getinfo(int a[], int n)
{int i;
for (i= 0;i<n;i++)
{do
{cout<<"How many movies did student "<<(i+1)<< " see? ";
cin >> a[i];
if(a[i]<0||a[i]>100)
cout<<"Invalid entry, Please enteer a value between 0 and 100 ";
}while(a[i]<0||a[i]>100);
}
}
double average(int a[], int students)
{ int tot = 0,i;
double avg;
for (i= 0;i<students; i++)
tot += a[i];
avg=(double)tot/students;
return avg;
}
int *create_array(int n)
{ int *ptr;
ptr = new int[n];
return ptr;
}
void sort(int a[], int n)
{ int i,j,t;
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
if(a[i]>a[j])
{t=a[i];
a[i]=a[j];
a[j]=t;
}
}
double median(int *a, int n)
{ int m1,m2;
if (n%2==0)
{m1=n/2;
m2=(n/2)-1;
return((*(a+m1)+*(a+m2))/2.);
}
else
return *(a+(n/2));
}
int get_mode(int *a, int n)
{
int *count,most,index,i,j;
count= create_array(n);
for (i= 0;i< n;i++)
count[i] = 0;
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{if (*(a+j)==*(a +i))
(*(count+i))++;
}
}
most=*count;
index=0;
for (i=1;i<n;i++)
{if (*(count+i) >most)
{most=*(count+i);
index=i;
}
}
if (most == 1)
return -1;
else
return *(a+index);
}
int getrange(int* a,int n)
{return a[n-1]-a[0];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.