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: 673134 • 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:

1. Ask the user how many students were surveyed. An array of integers with this many elements should then be dynamically allocated.

2. Allow the user to enter the number of movies each student saw into the array.

3. Calculate and display the average, median, and mode of the values entered. Input validation: Do not accept negative numbers for input:

Note: You must use pointer /offset notation in this assignment instead of array notation. For example:

If you have an array named a, to access it content under subscript 3, you must write:

*(a + 3) instead of a[3]. Please note that points will be deducted if this requirements is not followed.

Could anyone change the array notation to pointer/offset notation of the following code?

#include <iostream>
using namespace std;
void getinfo(int*, int);
void sort(int [], int);
double average(int*, int);
double median(int*, int);
int   get_mode(int*, int);
int main()
{ int*array;  
   int num;      
   int mode,i,range;
    float avg;
    cout<<"enter number of students? ";
    cin>>num;
   while(num<=0)
    {cout<<"Invalid Entry ";
    cout<<"enter number of students? ";
    cin>>num;
    }
array = new int[num];
      getinfo(array, num);
   cout<<" The array is: ";
   for(i=0;i<num;i++)
       cout<<"student "<<i+1<<" saw "<<*(array+i)<<" movies. ";
   sort(array, num);
   cout << " the median is "<<median(array, num) << endl;
   cout << "the average is "<<average(array, num) << endl;
   mode = get_mode(array, num);
   if (mode == -1)
         cout << "no mode. ";
   else
         cout << "The mode is " << mode << endl;
delete [] 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 num)
{ int tot = 0,i;
   double avg;
   for (i= 0;i<num; i++)
       tot += a[i];
   avg=(double)tot/num;
   return avg;
}

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= new int[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);
}

Explanation / Answer

// I have changed the array notation to pointer notation. Please look at getInfo(), Average and Sort function where you have used array notation So I convert them in to pointer notation and Output is coming correct. Always note You just need to do these small things to convert array notation to pointer notation. Like a[] to *a because a denotes base address of the array. a[i] to *(a+i) because suppose you want to find a[2[ then *(a+2) equals to pointer moves two location forward and then gives the value stored in that address because *(a+2) gives the value located at address currently pointer hold.

#include <iostream>
using namespace std;
void getinfo(int*, int);
void sort(int *, int);
double average(int*, int);
double median(int*, int);
int   get_mode(int*, int);

int main()
{ int*array;
   int num;    
   int mode,i,range;
    float avg;
    cout<<"enter number of students? ";
    cin>>num;
   while(num<=0)
    {cout<<"Invalid Entry ";
    cout<<"enter number of students? ";
    cin>>num;
    }
  
array = new int[num];
    
getinfo(array, num);

   cout<<" The array is: ";
   for(i=0;i<num;i++)
       cout<<"student "<<i+1<<" saw "<<*(array+i)<<" movies. ";
   sort(array, num);
   cout << " the median is "<<median(array, num) << endl;
   cout << "the average is "<<average(array, num) << endl;
   mode = get_mode(array, num);
   if (mode == -1)
         cout << "no mode. ";
   else
         cout << "The mode is " << mode << endl;
delete [] array;
system("pause");
   return 0;
}

void getinfo(int *a, int n)      // Here I use the Signature. I change the argument from a[] to *a
{int i;
   for (i= 0;i<n;i++)
      {do
         {cout<<"How many movies did student "<<(i+1)<< " see? ";
          cin >> *(a+i);                        // a[i] to *(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 num)                // In this function also I changed the Syntax .
{ int tot = 0,i;
   double avg;
   for (i= 0;i<num; i++)
       tot += *(a+i);
   avg=(double)tot/num;
   return avg;
}

void sort(int *a, int n)                              // Here Also I changed the syntax.
{ 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);                                            // In these three lines I changed the syntax
           *(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= new int[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);
}

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