“HAPPINESS SHOULD BE A FUNCTION WITHOUT ANY PARAMETERS”

WEEK:10 PROGRAMMING ASSIGNMENT 3 SOLUTION (Jan 2020-Apr2020) An Introduction to Programming through C++ NPTEL 2020

  

               An Introduction To Programming Through C++


IIT Bombay.



Week 10- Programming Assignment 3:


Define a class to store an extensible array.  It should be possible to index into the array, and it should be possible to extend the array using a push_back operation.  A skeleton definition of the class is given below.  You are to fill in the ... portions, and then type the entire struct into the textbox of the assignment.  You should implement the protocol that at any time there must be exactly one pointer to space allocated on the heap, and your implementation must prevent memory leaks and dangling pointers.


struct eArray{
private:
  ...  // put data members here
public:
  eArray(){...}  // construct an array with 0 elements
  int &operator[](int i){...}
    // return a reference to the ith element of the array
  void push_back(int v){...}
    // Append v to the current array
    // Use a simple implementation: allocate a new array to
    // accommodate the extra element v.  Then copy the current
    // array into it.  Copy v, and delete the current array.
  int size() const {...}
    // return the current size of the array
    // "const" says this function will not change the receiver
  eArray(const eArray &rhs){...}
    // copy constructor
  ~eArray(){...}
    // destructor
  eArray& operator=(const eArray &rhs){...}
    // assignment operator
};

Using this it should be possible to write programs of the following
kind.  The following programs has already been typed in and will be
used to test your code.

void f(eArray A){A[5] = 5000;}   // changes only local copy
void g(eArray &A){A[6] = 6000;}  // changes original

int main(){
  eArray A,B;
  int n; cin >> n;
  for(int i=0; i<10; i++){
    int v; cin >> v;
    A.push_back(v);
  }
  // at this point A should contain all values read.
  f(A);
  g(A);
  B = A;
  B[8] = 800;
  A[8] = 8000;
  for(int i=0; i<B.size(); i++) cout << B[i] <<' ';
  cout << endl;
}
On input 10 0 10 20 30 40 50 60 70 80 90 this should print 0, 10, 20, 30, 40, 50, 6000, 70, 800, 90.

Note that the eArray as defined above is similar to the vector class in the standard library.  Your implementation can be very simple; whenever an element is appended, you can just allocate a new array of a larger size, copy the elements, and delete the array.  In the vector class, a large memory is allocated every time the current allocation is found inadequate; this way the number of allocation operations is reduced.
Public Test Cases:

Compilation : Passed
Public Test Cases: 3 / 3 Passed

Public Test Cases Input Expected Output Actual Output Status
Test Case 1
0 10 20 30 40 50 60 70 80 90
0 10 20 30 40 50 6000 70 800 90 \n
0 10 20 30 40 50 6000 70 800 90 \n
Passed

Test Case 2
1 2 3 4 5 6 -10 7 -80 9
1 2 3 4 5 6 6000 7 800 9 \n
1 2 3 4 5 6 6000 7 800 9 \n
Passed

Test Case 3
1 2 3 4 5 6 0 7 0 9
1 2 3 4 5 6 6000 7 800 9 \n
1 2 3 4 5 6 6000 7 800 9 \n
Passed



WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM: COPY THE CODE AS PROGRAM IS TOO BIG.

struct eArray{
private:
  int* elements;
  int n;
public:

   eArray(){n=0; elements = NULL;}  // construct an array with 0 elements
  int &operator[](int i){ return elements[i];}
    // return a reference to the ith element of the array
  void push_back(int v){
    int* newE = new int[n+1];
    for(int i=0; i<n; i++) newE[i] = elements[i];
    delete[] elements;
    elements = newE;
    elements[n] = v;
    n++;
  }
  int size() const {return n;}
  eArray(const eArray &rhs){
    n = rhs.n;
    elements = new int[n];
    for(int i=0; i<n; i++) elements[i] = rhs.elements[i];
  }
  ~eArray(){
    delete[] elements;
  }
  eArray& operator=(const eArray &rhs){
    if(this == &rhs) return *this;
    
    delete[] elements; 

    n = rhs.size();
    elements = new int[n];
    for(int i=0; i<n; i++) elements[i] = rhs.elements[i];
    return *this;
  }
};

 
WEEK-10 :PROGRAMMING ASSIGNMENT 1 AND 2 
CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 10:PROGRAMMING -01>>
<<CLICK HERE FOR  WEEK 10:PROGRAMMING-02>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a comment

If you have any problems related to solutions or any concept please let me know.