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

#include <iostream> using namespace std; int *create_array(int); int enter_data

ID: 3618359 • Letter: #

Question

#include <iostream>
using namespace std;

int *create_array(int);
int enter_data (int*, int);
int get_mode (int*, int);
double average (int*, int);
int showarray (int*, int);

int main ()
{
   int *dyn_array;
   int mode, students;
   float avrg;
  
   do
       {
           cout<< " How many students will you enter? ";
           cin>> students;
          
       }while (students <=0);
      
   dyn_array = create_array (students);
   enter_data (dyn_array, students);
   mode = get_mode (dyn_array, students);
   avrg= average (dyn_array, students);
  
   cout << " The array is: ";
   showarray (dyn_array, students);
  
   cout << endl;
   cout << "The mode is " << mode<< endl;
   cout << "The average is " << avrg<< endl;
  
   delete [] dyn_array;
   return 0;
}

int *create_array (int students)
{
   int *ptr;
  
   ptr= new int[students];
   return ptr;
}

int enter_data (int movie[], int students)
{
   for (int count = 0; count < students;count++)
   {   do
       {   cout <<"How many movies did students " << (count + 1) << "see: ";
         cin>> movie[count];
           if(movie[count] <0 || movie [count] >100)
           cout<<"Invalid number. Please enter a number between 0 and 100. "<< endl;
      
       } while (movie[count] <0|| movie[count] >100);
      
      
   }
  
}

int get_mode (int *student_data, int num_students)
{
   int *frequency;
   frequency[101] =0;
   int mode =0;
   int max;
  
   for ( int count =0; count < num_students ;count ++)
       frequency[count] = 0;
      
   for (int count =0; count < 101; count ++)
   {
       movie[count] =student_data[count];
       frequency[student_data[count]]++;

   for ( int count = 0; count < num_students;count ++ )
   {
       if(frequency[student_data[count]] > max)
       {
           max =frequency[student_data[count]];
           mode =max;
       }
   }
   }
}        

      
double average (int movie[], int students)
{
   int total;
   double avrg;
   total = 0;
  
   for (int count = 0; count < students; count++)
       total += movie[count];
  
   avrg = total / students;
   return avrg;
}


int showarray (int movie[], int stu)
{  
   for (int count = 0; count < stu; count++)
   {
       cout << " Student "<< (count + 1) << " saw " << movie[count]<< " movies ";
   }
}

This is my code. I cannot debug it. It think the problem is inget_mode. I don't know how to fix it. Please fix it for me. Thx

Explanation / Answer

You did have a couple of bugs in that get_mode function. Hereis a correct version of that method: int get_mode(int movie[], int students) {     int frequency[101];     int mode = 0;     int viewed = 0;         for (int count = 0; count < 101; count++){         frequency[count] =0;     }     // Iterate thru the student movie array and     // record the movie count in the frequecyarray.     for (int count = 0; count mode) {             mode =count;         }     }     return mode; }