Partial preview of the text
Download Data Structures Test 1 Crib Sheet and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!
include } #include bs // Header class // Function to read from a file class DataHandler { - void DataHandler: :readFromFile(const std::string& filename) { private: std::ifstream file(filename) ; int* arr; if (file) { int size; std::cout << "Reading from file: a : public: int val; hile (file >> 1 DataHandler(int s) : size(s) { wae ~ a8. Bove aa — i i 4 it a. > arr = new int[size]; // Allocating memory for array } = a ¥ , for (int i = @; 7 size; i++) { seals, carl esata tend *(arr + i) = i * 10; // Using pointer arithmetic to assign values iTS. c1EE(): : 3 } ~DataHandler() { delete[] arr; } // Destructor to free memory // Function to print array using pointer arithmetic void DataHandler::printArray() { std::cout << “Array contents: "; void writeToFile(const std::string& filename) ; void readFromFile(const std::string& filename) ; void printArray(); for (int* ptr = arr; ptr < arr + size; ptr++) { 33 3 // Pointer dereferencing } // Function to write array to a file std::cout << std::endl; void DataHandler: :writeToFile(const std::string& filename) { } std::ofstream file(filename) ; if (file) { // Main function for (int i = @; i < size; i++) { + int main() { file << *(arr + i) <<" "; // Writing using pointers DataHandler dh(5); // Creating an object with array size 5 } dh.printArray(); file.close(); dh.writeToFile("data.txt"); int a= 42; dh.readFromFile("data.txt"); return @; int* p = &a; // Pointer to a int** pp = &p; // Pointer to pointer |// Output: Array values using pointer arithnetic: std::cout << "Value of a: " << a << "\n"; a) . . | Output: Value of a: 42 for (int 1: Q 1¢ 5 itt) { std::cout << "Value using poititer Br” << sp << "VR"; std::cout << *(ptr + i) <<"; // Output: 18 20 30 48 58 Output: Value using pointer p: 42 std::cout << "Value using pointer to pointer pp: " << **pp << "\n"; } | Output: Val ing pointer to pointer pp: 42 H\ nll = eiue using P er re PP std::cout << "\n )