1709673435

C++, Implementing a Dynamic Sized Array


Let's write a small application to manage the student records in a school. The number of students in a class and their details will be given as an input. Write an array-like container to manage the data, which can also support dynamic sizing. We'll also implement some utility functions to merge different classes. ![file](https://i.ytimg.com/vi/jvUEcHC_ZGI/maxresdefault.jpg) Perform the following steps to complete the exercise: 1. first, include the required headers: ```c #include <iostream> #include <sstream> #include <algorithm> ``` 2. now let's write a basic templated structure called **dynamic_array**, as well as primary data members: ```c template<typename T> class dynamic_array { T* data; size_t n; ``` 3. now, let's add a constructor that takes the size of the array and copies it: ```c 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]; } ``` 4. now, let's add **operator[]** and **function()** in the **public** accessor to support the access of data directly, in a similar way to **std::array**: ```c T& operator[](int index) { return data[index]; } const T& operator[](index) const { return data[index]; } T& at(int index) { if(index < n) return data[index]; throw "Index out of range"; } ``` 5. now, let's add a function called **size()** to return the size of the array, as well as a destructor to avoid memory leaks: ```c size_t size() const { return n; } ~dynamic_array() { delete[] data; // a destructor to prevent memory leak } ``` 6. now, let's add iterator functions to support range-based loops to iterate over **dynamic_array**: ```c T* begin() { return data; } cosnt T* begin() const { return data; } T* end() { return data + n; } const T* end() const { return data + n; } ``` 7. now, let's add a function to append one array to another using the + operator. Let's keep it as a **friend** function for better usability: ```c 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; } ``` 8. now, let's add a **to_string** function that takes a separator as a parameter with the default value as ",": ```c 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(); } }; ``` 9. now, let's add a **struct** for students. We'll just keep the name and the standard (that is, the grade/class in which the student is studying) for simplicity, and also add **operator<<** to print it properly: ```c struct student { std::string name; int standard; }; std::ostream& operator<<(std::ostream& os, cosnt student& s) { return(os <<"[Name: " << s.name << ",Standard: " << s.standard <<"]"); } ``` 10. now, let's add a **main** function to use this array: ```c int main() { int nStudents; std::cout << "Enter number of stundents in class 1: "; std::cin >> nStudents; dynamic_array<students> class1(nStudents); for(int i = 0; i < nStudents; ++) { 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 class and creates a bigger one std:count<<"Combined class: "; std::cout<<class3.to_string()<<std::endl; return 0; } ``` 11. execute the preceding code with three students - **Raj(8)**, **Rahul(10)**, and **Viraj(6)** as input.

To comment this publication you need to be logged in.
JavaJuggler
JavaJuggler
0

As for the second comment, by `Mr. Abid876`, I just want to thank you for your clear comment. thanks


JavaJuggler
JavaJuggler
0

very good, `Mr. Beginner895`. very clear definition


Beginner895
Beginner895
0

The provided code implements a dynamic array container named dynamic_array, capable of managing arrays with dynamic sizing. It offers functionalities such as constructors, destructor, accessors (like size(), begin(), end()), and operators (operator[], operator ).Additionally, it defines a student struct to represent student records, including their name and class/standard. The operator<< overload enables proper printing of student objects.In the main function, users can input student records for a class. Exception handling is implemented to manage out-of-range access when setting student records. The code also demonstrates deep copying of one class and merging of two classes using the operator .With the corrections mentioned (fixing typos and adjusting index access in the try block), the code is expected to work correctly for managing student records and showcasing the usage of dynamic array containers.OK.


Abid876
Abid876
0

dynamic_array Templated Class: Manages an array with dynamic sizing. Implements constructors, destructor, accessors (size(), begin(), end()), and utility functions (operator[], operator+). Allows for dynamic resizing as needed. student Struct: Holds student records with attributes such as name and class/standard. Overloads the operator<< to enable proper printing of student objects. main() Function: Accepts user input for student records in a class. Handles out-of-range access using exception handling. Performs a deep copy of one class. Merges two classes using the operator+. Overall, the code facilitates the management of student records in a school setting. It showcases the usage of dynamic arrays for storing and manipulating data, with proper error handling and utility functions for ease of use. Are You understand is comment ? Please write me ok.


JavaJuggler
JavaJuggler
0

for those who understand cplusplus well, help me understand the code above, please