fix the getIndex function

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Not sure how to fix the getIndex function please help

---------------------------

#include <iostream>

#include <cassert>

using namespace std;

 

class SomeObj{

  public:

    SomeObj(int d ): id(d){}

    int getId() const;

    void output();

  private:

    int id;

};

 

int SomeObj::getId() const{

  return id;

}

 

void SomeObj::output(){

  cout<<id<<endl;

}

 

template<typename T>

class MyArray {

public:

    MyArray();

    MyArray(int c);

    T& operator[](int index);

    void push_back(T e);

    int getSize() const;

    int getCapacity() const;

    int getIndex(T a);

    void erase();

private:

    void grow();

    T *data;

    int capacity;

    int size;

};

 

template <typename T>

MyArray<T>::MyArray(): MyArray(1) {

}

 

template <typename T>

MyArray<T>::MyArray(int c) {

    assert(c>0);

    size = 0;

    capacity = c;

    data = new T[capacity];

}

 

template <typename T>

T& MyArray<T>::operator[](int index) {

    assert(index >=0 && index < size);

    return data[index];

}

 

template <typename T>

int MyArray<T>::getSize() const {

    return size;

}

 

template <typename T>

int MyArray<T>::getCapacity() const {

    return capacity;

}

 

template <typename T>

void MyArray<T>::grow(){

int* new_a = new int[(capacity*2)-1];

 

  for (int i = 0; i < size; i++){

    new_a[i] = *data[i];

}

capacity = (capacity*2)-1;

data = &new_a;

}

 

template <typename T>

int MyArray<T>::getIndex(T a){

  for(int i = 0; i < size; i++){

    if(data[i] == a){

      return i;

    }

  else return -1;

  }

  return 0;

}

 

template <typename T>

void MyArray<T>::push_back(T e) {

  if(size>=capacity){

    grow();

  }

  else

    assert(size < capacity);

    data[size++] = e;

}

 

template <typename T>

void MyArray<T>::erase(){

  for (int i = size; i < capacity; i++){

  data[size] = data[size + 1];

  data[capacity - 1] = data[size];

  }

}

 

template <typename T>

void printHeap(MyArray<T> &m) {

    for(int i=0; i<m.getSize();i++) {

        cout << *(m[i]) << " ";

    }

    cout << endl;

}




int main() {

    cout << endl;

 

    MyArray<int*> Mnums(10);

 

    for(int i=0; i<Mnums.getCapacity(); i++) {

        int *nums = new int(i+1);

        Mnums.push_back(nums);

    }

 

    printHeap(Mnums);

 

    cout << endl;

    return 0;

}



Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Introduction to Template
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education