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

Define a class which has a constructor, a member variable, two member functions,

ID: 3688735 • Letter: D

Question

Define a class which has a constructor, a member variable, two member functions, and a destructor. The member variable is an interger pointer. In the constructor, use this pointer to dynamically allocate an interge array, the array size is decided by the constructor parameter (e.g. int n). The two member functions are: prime_count() and prime_check(), the first returns how many prime numbers are in the array and the second is to check if a number is prime or not. In the destructor, deallocate the dynamic array memories through the pointer. In main(), declare and assign n using cin or rand(), then declare an object using the class defined above and pass n as its constructor parameter, then call its member function prime_count() to output how many numbers in the array are prime.

Explanation / Answer

#include<iostream.h>
#include<stdlib.h>

class prim
{
public:
   int *a;

   prim(int n)
   {
       a=new int[n];
      

   }

   void prime_count(int n)
   {
       int i,j;

       for(i=0;i<n;i++)
       {
           for(j=2;j<i;j++)
           {
               if(a[i]%j==0)
               {
                   break;
               }
              
           }
      
           if(j==i)
               c++;
       }
       cout<<"Count is "<<c;
       getch();
   }

   void prime_check(int n)
   {
       int j,n1;
       n1=a[n];
       for(j=2;j<n1;j++)
       {
           if(n1%j==0)
           {
               cout<<"Not Prome";
               break;
           }
              
       }
       if(j==n)
           cout<<"Prime";
  
       getch();
   }

};

void main()
{
   prim o(5);

   cout<<"Enter 5 numbers";
   for(i=0;i<5;i++)
   {
       cin>>o.a[i];
   }

   o.prime_count(5);
   o.prime_check(3);
   getch();

}
  

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