C++ Program ReadData manipulates a two-dimensional table. #include <iostream> #i
ID: 3722059 • Letter: C
Question
C++
Program ReadData manipulates a two-dimensional table.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int ROW_MAX = 8;
const int COL_MAX = 10;
typedef int ItemType;
typedef ItemType Table[ROW_MAX][COL_MAX];
int main ()
{
Table table;
int rowsUsed;
int colsUsed;
ifstream dataIn;
ofstream dataOut;
int col;
int row;
dataIn.open("debug.dat");
dataOut.open("debug.out");
dataIn >> colsUsed >> rowsUsed;
// Input table values.
for (col = 0; col < colsUsed; col++)
for (row = 0; row < rowsUsed; row++)
dataIn >> table[row][col];
// Output table values.
for (row = 0; row < rowsUsed; row++)
{
for (col = 0; col < colsUsed; col++)
dataOut << setw(3) << table[row] [col];
dataOut << endl;
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
using namespace std;
const int ROW_MAX = 8;
const int COL_MAX = 10;
typedef int ItemType;
typedef ItemType Table[ROW_MAX][COL_MAX];
int main ()
{
Table table;
int rowsUsed;
int colsUsed;
ifstream dataIn;
ofstream dataOut;
int col;
int row;
// Warning: : table should be memset. it may have some garbage value which will cause inproper output
memset(table,'',sizeof(table));
dataIn.open("debug.dat");
// Error: if file not availabe then also should print error:
if (!dataIn)
{
cerr << "Unable to open file debug.dat";
return 0; // call system to stop
}
dataOut.open("debug.out");
if (!dataOut)
{
cerr << "Unable to open file debug.out";
return 0; // call system to stop
}
dataIn >> colsUsed >> rowsUsed;
/*
Error: ROW_MAX and COL_MAX are define. but if colsUSed and rowsUsed is more than it then it will throw segmentation fault
Example idebug.data:
--------------------
25 25
1 2 3
1 2 3
-------------------
cpu will try to read 25 * 25 data. but table size is not sufficient. so it will show segmentation fault.
*/
if(colsUsed > COL_MAX || rowsUsed > ROW_MAX)
{
cout<<"MAX size for col: "<<COL_MAX<<"Row: "<< ROW_MAX<<endl;
cout<<"Invalid size ";
return 0;
}
// Input table values.
for (col = 0; col < colsUsed; col++)
for (row = 0; row < rowsUsed; row++)
if (dataIn >> table[row][col]) // Error: needs to check read is success or not
{
continue; // should contineue if read success
}
else
break; // if read fails then no need to read further. if ignore then will give junk value
/*
error:
suppose debug.data is
----------------------
5 5
1 2 3
1 2 3
------------------------
then it will read upto 5 * 5 but actally data is not available. so here needs to print error and return.
*/
if(row != rowsUsed || col != colsUsed)
{
cout<< "Please check input file. it should have sufficient data ";
return 0;
}
// Output table values.
for (row = 0; row < rowsUsed; row++)
{
for (col = 0; col < colsUsed; col++)
dataOut << setw(3) << table[row] [col];
dataOut << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.