You are here:

C++/help me!!!!

Advertisement


Question
A public utilities company in in a city wants to track the status of its power service throughout the city for 14 sectors.
with a 3x4 grid in which each cell represents power service in one sector. When power is available
everywhere, all grid values are 1. A grid value of 0 indicates an outage somewhere in the sector.
Write a program that inputs a grid from a file and displays the grid. If all grid values are 1, display a
message that power is on throughout the grid. Otherwise, display a message that list out the sector that
have the outages. Your program should also include functions get_grid, display_grid, power_ok, and
where_off.

Answer
Sir, I'll help you with the get_grid and display_grid, and you can then finish your assignment using my example.

The get_grid is the most difficult function. When you read from a file, you should be prepared to check for error conditions, such as the file not being found, or there not being enough data in the file.

I will present you code that reads through the file character by character and stores the grid data in a global grid array. I suggest you start with a global grid array. If your instructor does not like global variables, then I suggest you get the program working with a global grid array, and then change the functions so that the grid array can be passed to the functions.

When reading a file, you must be prepared to handle newline characters. These occur whenever there is a line break. You must ignore them.

In the code below, you notice that I ignore all but the 0 and 1 characters. This means that your grid file can look like this:
1111
1010
1111

or like this:
1,1,1,1
1,0,1,0
1,1,1,1

or like this:
111110101111

The same code handles them all.

Also notice that I use the definitions GRID_ROWS and GRID_COLS to declare the grid array. Then I use the GRID_ROWS and GRID_COLS in all the loops that traverse the array. In this way, the size of the grid can be changed by changing only the definitions.

If you have questions about the code, please feel free to ask.

Here is the program:

#include <iostream>
#include <fstream>
#include <string>

#define GRID_ROWS 3
#define GRID_COLS 4
char grid[GRID_ROWS][GRID_COLS];


void display_grid(void)
{
   std::cout << "Here is the grid\n";
   for(int row = 0; row < GRID_ROWS; ++row)
   {
       for (int col = 0; col < GRID_COLS; ++col)
       {
         std::cout << grid[row][col];
       }
       std::cout << std::endl;
   }
}

void get_grid(const char* filename)
{
   std::ifstream fs;
   fs.open(filename, std::ios_base::in);
   if (!fs.is_open())
   {
       std::string error("Cannot open the grid file: ");
       error.append(filename);
       throw error;
   }

   for(int row = 0; row < GRID_ROWS; ++row)
   {
       for (int col = 0; col < GRID_COLS; ++col)
       {
         bool ignore = false;
         do
         {
         ignore = false;
         int character = fs.get();
         if (character < 0)
         {
         fs.close();
         std::string error("There is not enough data in the grid file: ");
         error.append(filename);
         throw error;
         }
         if (character == '1' || character == '0') grid[row][col] = character;
         else ignore = true;
         } while(ignore);
       }
   }

   fs.close();
}

int main(void)
{
   try
   {
       get_grid("c:\\temp\\grid.txt");
       display_grid();
   }
   catch(std::string& error)
   {
       std::cerr << "Error: " << error << std::endl;
   }

   return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.