Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Structures Test 1 Crib Sheet, Study Guides, Projects, Research of Data Structures and Algorithms

Professor was Xidong for the Spring Semester

Typology: Study Guides, Projects, Research

2024/2025

Uploaded on 04/28/2025

sebastian-brochu
sebastian-brochu 🇺🇸

1 document

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1

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 )