ase debug this code and the correct output is shown in the image below #include #include #include // primary data members: template class dynamic_array { T* data; size_t n;

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

Please debug this code and the correct output is shown in the image below

#include <iostream>
#include <sstream>
#include <algorithm>

// primary data members:
template <typename T>
class dynamic_array
{
T* data;
size_t n;



public:
dynamic_array(int n)
{
this->n = n;
data = new T[n];
}
dynamic_array(const dynamic_array<T>& other)
{
n = other.n;
data = new T[n];
for(int i = 0; i < n; i++)
data[i] = other[i];
}



T& operator[](int index)
{
return data[index];
}
const T& operator[](int index) const
{
return data[index];
}
T& at(int index)
{
if(index < n)
return data[index];
throw "Index out of range";
}



size_t size() const
{
return n;
}


//
// dynamic_array:
T* begin()
{
return data;
}
const T* begin() const
{
return data;

}
T* end()
{
return data + n;
}
const T* end() const
{
return data + n;
}



friend dynamic_array<T> operator+(const dynamic_array<T>& arr1, dynamic_array<T>& arr2){
dynamic_array<T> result(arr1.size() + arr2.size());
std::copy(arr1.begin(), arr1.end(), result.begin());
std::copy(arr2.begin(), arr2.end(), result.begin() + arr1.size());
return result;
}

std::string to_string(const std::string& sep = ", ")
{
if(n == 0)
return "";
std::ostringstream os;
os << data[0];
for(int i = 1; i < n; i++)
os << sep << data[i];
return os.str();
}
};



// add operator<< to print it properly:
struct student
{
std::string name;
int standard;
};
std::ostream& operator<<(std::ostream& os, const student& s)
{
return (os << "[Name: " << s.name << ", Standard: " << s.standard <<
"]");
}



int main()
{
int nStudents;
std::cout << "Enter number of students in class 1: ";
std::cin >> nStudents;
dynamic_array<student> class1(nStudents);
for(int i = 0; i < nStudents; i++)
{
std::cout << "Enter name and class of student " << i + 1 << ": ";
std::string name;
int standard;
std::cin >> name >> standard;
class1[i] = student{name, standard};
}
// Now, let's try to access the student out of range in the array
try
{
class1[nStudents] = student{"John", 8}; // No exception, undefined
// behavior
std::cout << "class1 student set out of range without exception" <<
std::endl;
class1.at(nStudents) = student{"John", 8}; // Will throw exception
}
catch(...)
{
std::cout << "Exception caught" << std::endl;
}
auto class2 = class1; // Deep copy
std::cout << "Second class after initialized using first array: " <<
class2.to_string() << std::endl;
auto class3 = class1 + class2;
// Combines both classes and creates a bigger one
std::cout << "Combined class: ";
std::cout << class3.to_string() << std::endl;
return 0;
}

Enter number of students in class 1 : 3
Enter name and class of student 1: Raj 8
Enter name and class of student 2: Rahul 10
Enter name and class of student 3: Viraj 6
classi student set out of range without exception
Exception caught
Second class after initialized using first array : [Name: Raj, standard: 8], [Name: Rahul, standard: 10], [Name: Viraj, standa
rd: 6]
Combined class : [Name: Raj, standard: 8], [Name: Rahul, standard: 10],
[Name: Viraj, standard: 6], [Name: Raj, Standard: 8], [Name: Rahul,
Standard: 10], [Name: Viraj, standard: 6]
Transcribed Image Text:Enter number of students in class 1 : 3 Enter name and class of student 1: Raj 8 Enter name and class of student 2: Rahul 10 Enter name and class of student 3: Viraj 6 classi student set out of range without exception Exception caught Second class after initialized using first array : [Name: Raj, standard: 8], [Name: Rahul, standard: 10], [Name: Viraj, standa rd: 6] Combined class : [Name: Raj, standard: 8], [Name: Rahul, standard: 10], [Name: Viraj, standard: 6], [Name: Raj, Standard: 8], [Name: Rahul, Standard: 10], [Name: Viraj, standard: 6]
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
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