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?
Run the following code and explain what the code does Run this code at least twice...
Run the following code and explain what the code does Run this code at least twice and take screenshots Explain what the code does What is the running result supposed to be What caused this issue? #include <thread> #include <iostream> using namespace std; const unsigned int NTHREADS = 20; const int ITERS = 10000000; int counter; void increment() {        for (int i = 0; i < ITERS; i++)                      counter++;               } void decrement() {        for (int i...
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?
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?
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> ]>
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 explain how does the following C code work. a) int function1(int num1, int num2) {...
please explain how does the following C code work. a) int function1(int num1, int num2) { int num = num1 ^ num2; int result = 0; while(num > 0) { result += (num & 1); num >>= 1; } return result; } b) int function2(unsigned int num) { return num & ~(num - 1); } c) int f1(unsigned int x) { int count = 0; while(x) { count++; x = x&(x-1); } return count; } d) double ldexp(double x, int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT