C++ Please, I need help on this. In all problems use dynamically allocated array
ID: 3685264 • Letter: C
Question
C++
Please, I need help on this.
In all problems use dynamically allocated arrays. Use pointer notation whenever possible.
Note in problem 7 you will use the functions (mode and median) you developed in problems 5 and 6.
5. Pie a la Mode In statistics the mode of a set of values is the value that occurs most often. Write a program that determines how many pieces of pie most people eat in a year. Set up an integer array that can hold responses from 30 people. For each person, enter the number of pieces they say they eat in a year. Then write a function that finds the mode of these 30 values. This will be the number of pie slices eaten by the most people. The function that finds and returns the mode should accept two arguments, an array of integers, and a value indicating how many elements are in the array. 6. Median Function In statistics the median of a set of values is the value that lies in the middle when the values are arranged in sorted order. If the set has an even number of values, then the median is taken to be the average of the two middle values. Write a function that determines the median of a sorted array. The function should take an array of numbers and an integer indicating the size of the array and return the median of the values in the array. You may assume the array is already sorted. Use pointer notation whenever possible. 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, median, and mode of the values enteredExplanation / Answer
5.
#include <iostream>
using namespace std;
void sort(int *p, const int n)
{
for(int i = 0; i < n-1; i++)
for(int j = 0; j < n-i-1; j++)
if(*(p+j) > *(p+j+1))
{
int temp = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = temp;
}
}
int pieMode(int *p, const int &SIZE)
{
int mode = 0, currentCount = 1;
sort(p, SIZE);
for (int count = 0; count < SIZE - 1; count ++)
{
if(*(p+count) == *(p+count+1))
currentCount++;
else
{
if(currentCount > mode)
mode = currentCount;
}
}
return mode;
}
int main()
{
int mode;
const int SIZE = 30;
int *Responses = new int[SIZE]; //An integer array of size 30.
cout<<"Enter the number of pieces eaten in a year..."<<endl;
for (int count = 1; count < SIZE; count++)
{
cout << "Person #" << count << ": ";
cin >> *(Responses + count);
}
mode = pieMode(Responses, SIZE);
delete [] Responses;
cout << "The mode of the read responses is: " << mode << endl;
return 0;
}
6.
double Median(int *p, const int &SIZE)
{
if(SIZE % 2 == 1)
return *(p+SIZE/2);
else
return (*(p+(SIZE/2)) + *(p+(SIZE/2-1))) / 2.0;
}
7.
#include <iostream>
using namespace std;
void sort(int *p, const int n)
{
for(int i = 0; i < n-1; i++)
for(int j = 0; j < n-i-1; j++)
if(*(p+j) > *(p+j+1))
{
int temp = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = temp;
}
}
int Mode(int *p, const int &SIZE)
{
int mode = 0, currentCount = 1;
sort(p, SIZE);
for (int count = 0; count < SIZE - 1; count ++)
{
if(*(p+count) == *(p+count+1))
currentCount++;
else
{
if(currentCount > mode)
mode = currentCount;
}
}
return mode;
}
double Median(int *p, const int &SIZE)
{
if(SIZE % 2 == 1)
return *(p+SIZE/2);
else
return (*(p+(SIZE/2)) + *(p+(SIZE/2-1))) / 2.0;
}
double Average(int *p, const int N)
{
double sum = 0;
for(int i = 0; i < N; i++)
sum += *(p+i);
return sum / N;
}
int main()
{
int num;
cout<<"Enter the number of students surveyed: ";
cin>>num;
int *Array = new int[num];
cout<<"Enter the number of movies student have seen..."<<endl;
for (int count = 0; count < num; count++)
{
cout << "Student #" << count+1 << ": ";
cin >> *(Array + count);
}
cout<<"Average number of movies seen by the students is: "<<Average(Array, num)<<endl;
cout<<"Mode value of movies seen is: "<<Mode(Array, num)<<endl;
cout<<"Median value of movies seen is: "<<Median(Array, num)<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.