Question: I keep getting this error message ? It doesnt seem like the input file is being read properly. #include #include #include / / For random

I keep getting this error message ? It doesnt seem like the input file is being read properly.
#include
#include
#include // For random numbers
#include // For srand, rand
using namespace std;
// Node struture for 2D linked list grid
struct Node {
int value;
Node* right;
Node* down;
Node(int val =0) : value(val), right(nullptr), down(nullptr){}
};
// LinkedList class that manags the 2D grid
class LinkedList {
private:
Node* head;
int rows, cols;
public:
LinkedList() : head(nullptr), rows(0), cols(0){}
// Destrctor to free memory when the object is destroyd
~LinkedList(){
clearList();
}
// Initilize the list from a file input
void inputList(const string& filename ){
ifstream file("GridNumbers.txt");
if (!file){
cout "Error: Unable to open file." endl;
return;
}
file >> rows >> cols;
Node* prevRowHead = nullptr;
Node* prev = nullptr;
for (int i =0; i rows; ++i){
Node* currentRowHead = nullptr;
for (int j =0; j cols; ++j){
int value;
file >> value;
Node* newNode = new Node(value);
if (j ==0) currentRowHead = newNode;
if (i ==0 && j ==0) head = newNode;
if (prev) prev->right = newNode;
if (prevRowHead){
prevRowHead->down = newNode;
prevRowHead = prevRowHead->right;
}
prev = newNode;
}
prev = nullptr;
prevRowHead = currentRowHead;
}
file.close();
}
// Print the grid neatly
void printList() const {
Node* row = head;
while (row){
Node* col = row;
while (col){
cout col->value "";
col = col->right;
}
cout endl;
row = row->down;
}
}
// Get the value at a speciific row & column
int getItem(int row, int col) const {
Node* current = head;
for (int i =0; i row && current; ++i) current = current->down;
for (int j =0; j col && current; ++j) current = current->right;
if (current) return current->value;
return -1; // Invaliid index
}
// Set the value at a specifiic row & column
void setItem(int row, int col, int value){
Node* current = head;
for (int i =0; i row && current; ++i) current = current->down;
for (int j =0; j col && current; ++j) current = current->right;
if (current) current->value = value;
}
// Clear entire grid & free memory
void clearList(){
Node* row = head;
while (row){
Node* col = row;
while (col){
Node* temp = col;
col = col->right;
delete temp;
}
Node* temp = row;
row = row->down;
delete temp;
}
head = nullptr;
rows =0;
cols =0;
}
// Get no. of rows
int getRows() const { return rows; }
// Get no. of columns
int getCols() const { return cols; }
};
// Driver function to test the LinkedList class
int main(){
srand(time(0)); // Seed for random number generation
LinkedList grid;
cout "Loading the grid:
";
ifstream file("GridNumbers.txt");
grid.printList();
cout "
Testing getItem():
";
for (int i =0; i 5; ++i){
int row = rand()% grid.getRows();
int col = rand()% grid.getCols();
cout "The element at (" row "," col ") is "
grid.getItem(row, col) endl;
}
cout "
Testing setItem():
";
for (int i =0; i 5; ++i){
int row = rand()% grid.getRows();
int col = rand()% grid.getCols();
int newValue = rand()%100;
cout "Changing element at (" row "," col ") to "
newValue endl;
grid.setItem(row, col, newValue);
}
grid.printList();
cout "
Deleting the grid:
";
grid.clearList();
grid.printList();
return 0;
}
//Here is the input file ?
55
3931909038
7630538628
7020396676
7424705150
3488802533
I keep getting this error message ? It doesnt

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!