Question

In: Computer Science

Explain what this code does. Will this work? Let us discuss cents = new *Ent[num_cents]; for...

Explain what this code does. Will this work? Let us discuss


cents = new *Ent[num_cents];
for (i=1;i<num_cents+1;i++)
{
cents[i-1] = new Ent();
}

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

This program will not run as new *Ent[num_cents]; is not a valid syntax.

The correct syntax should be :

Ent** cents;
   cents = new Ent*[num_cents];
  
   for (i=1;i<num_cents+1;i++)
   {
       cents[i-1] = new Ent();
   }

Here cents will be pointer to pointer to Ent then we will initialize cents as Array of pointers to Ent (Both array and pointer are same). Now in loop we are creating pointer to Ent and saving it array of cents.

Sample Run(With some modifications):

#include<iostream>
using namespace std;

class Ent{
public:
   int a;
   Ent(int a){
       this->a=a;
   }
};
void main(){
   const int num_cents=5;
   int i;
   Ent** cents;
   cents = new Ent*[num_cents];
  
   for (i=1;i<num_cents+1;i++)
   {
       cents[i-1] = new Ent(i);
   }

   for (i=1;i<num_cents+1;i++)
   {
       cout<<cents[i-1]->a<<endl;
   }
}


Related Solutions

What is MALDI-TOF? How does it work? What can it tell us?
What is MALDI-TOF? How does it work? What can it tell us?
Explain why the PATH-GOAL theory DOES NOT work in the new millennium. In other words, what...
Explain why the PATH-GOAL theory DOES NOT work in the new millennium. In other words, what current technological, social/cultural, or political factors challenge the concept and make this theory difficult to adopt in the workplace.
Discuss the new move for the internet to be controlled outside of the US? What are...
Discuss the new move for the internet to be controlled outside of the US? What are the implications of that?
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE...
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE teo[                                 <ELEMENT teo((stations| databases)+)>                                 <ELEMENT stations(stationName stationLocation sensors*)> <ELEMENT databases(databaseName databaseType )> <ELEMENT stationName(#PCDATA)> <ELEMENT stationLocation(#PCDATA)> <ELEMENT sensors (sensorName phenomenon)+> <ELEMENT sensorName(#PCDATA)> <ELEMENT phenomenon(#PCDATA)> <ELEMENT databaseName(#PCDATA)> <ELEMENT databaseType(#PCDATA)> <!ATTLIST stations boundingBox CDATA #required> ]>
Discuss the case of the woman with akinetopsia. What does this case tell us about the...
Discuss the case of the woman with akinetopsia. What does this case tell us about the importance of motion perception?
Why does this code not work even when typing in the correct product code? It always...
Why does this code not work even when typing in the correct product code? It always gives me the error message even though im typing one of the 3 correct product codes. String[] product_code= new String[3]; String[] product_name= new String[3]; // String[] product_description = new String[3]; int[] quantity = new int[3]; double[] price = new double[3]; double[] itemTotal = new double[3]; double subTotal = 0, salesTax, total;    //getting all the needed inputs for(int i = 0; i < 3;...
1. Please explain what accrued expenses are and let us know why these adjustments are necessary....
1. Please explain what accrued expenses are and let us know why these adjustments are necessary. Please provide an example of an adjusting entry for an accrued expense. 2. Please explain what accrued revenues are and let us know why these adjustments are necessary. Please provide an example of an adjusting entry for accrued revenues. 3. Please explain what an Unearned Revenue account is and why an adjustment may be necessary for Unearned Revenue. Please provide an example of an...
Discuss the topic of exergy what doesit reprsent ? what does the exergy balance tell us...
Discuss the topic of exergy what doesit reprsent ? what does the exergy balance tell us ? how can we use exergy analysis ? please type the answer NO HAND WRITING
please let me know reference of this MATLAB code. please explain this code line by line....
please let me know reference of this MATLAB code. please explain this code line by line. . . N=256; %% sampling number n=linspace(0,1,N); fs=1/(n(2)-n(1)); x=5*(sin((2*pi*10*n))); %% create signal N=length(x); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(211) plot(f,fftshift(abs(fft(x)))); title('|G(f)|');grid; xlabel('frequency'); ylabel('|G(f)|'); %Zero padding xx=[zeros(1,128) x zeros(1,128)]; N=length(xx); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(212) plot(f,fftshift(abs(fft(xx)))); title('|Gz(f)|');grid; xlabel('frequency'); ylabel('|Gz(f)|');
What is the role of the labor market in the Classical Model? What does it tell us? What does it not tell us?
What is the role of the labor market in the Classical Model? What does it tell us? What does it not tell us?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT