I want to use the new operator instead of malloc in this code can someone help?
ID: 3652608 • Letter: I
Question
I want to use the new operator instead of malloc in this code can someone help?#include <iostream>
#include <cstdlib>
int mode(int *arr,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
int **mode;
mode=(int **)malloc(n*sizeof(int *));
for(int i=0;i<n;i++) mode[i]=(int *)malloc(2*sizeof(int));
for(int i=0;i<2;i++)
{
for(int j=0;j<n;j++)
{
mode[j][i]=0;
}
}
mode[0][0]=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(arr[i]==arr[j+1])
{
++mode[i][0];
mode[i][1]=arr[i];
}
}
}
int max;
int k=0;
max=mode[0][0];
for(int j=0;j<n;j++)
{
if(max<mode[j][0])
{
max=mode[j][0];
k=j;
}
}
return mode[k][1];
}
int main()
{
int n;
cout<<"Enter the total integers to be stored in array: ";
cin>>n;
int *arr;
arr = (int *)malloc(n*sizeof(int));
cout<<"Enter integers: ";
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"mode is : ";
cout<<mode(arr,n)<<endl;
}
Explanation / Answer
#include int mode(int *arr,int n) { for(int i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.