0: a bcbef|- 1:- - - - - - - chi 2:- - - - - - - - - - - - |d k 1 |

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Help please The picture is how the output should look

Needed Files:

---LINES.TXT---

0 a   a b c

0 b   b e f

1 c   c h i

2 d   d k l

 

// the following mat.cpp program is not reading the input from the file >>  lines.txt !

Please help me find the bug that will make it work

The code  works to show the table but does not read the file and input to the table

 

---MAT.CPP---

int const R = 3;  // number of rows

int const C = 4;  // number of columns

int const VL = 3; // table entry vector length

 

vector<char> T[R][C]; // 3 rows and 4 columns table for now

 

// ------------------ Functions --------------------------------

 

int convert(char x) {

  // TODO: use a formula to convert a to 0, b to 1, c to 2 etc.

  return x -'a'; //ascii conversion

}

 

// this reads the input file into the matrix table

void readTable() {

  int row, col;

  char col_c;

  ifstream fin("lines.txt", ios::in);

  fin.open("lines.txt");

  // Read in the file into T

  while (fin >> row) // per row

  {    cout << "collect";

    vector<char> V;

    fin >> col_c;

    col = convert(col_c); // to a slot number

    char c;               // one char from the file

    //cout << "123";

    // TODO: Fill vector V with chars from the file (a for loop)

      while (fin >> c) {

            if (c == '\n') {

                break; // Stop reading if a newline is encountered or end of file

            }

            V.push_back(c); // Fill vector V with characters

        }

        cout << "hello world";

// TODO: Put the vector in T[row][col]

        int vectorIndex = 0;

        for (int i = 0; i < 4; ++i) {

          //&& vectorIndex < V.size()

            if (i == col) { // Place characters in the specified column

              col = convert(i);

              T[row][i] = V[vectorIndex++][];

            }

        }

    }

 

    fin.close();

  } // end of while

 

// display the vector nicely to the outstream

void showVector(vector<char> v, ostream &ost) {

  if (v.size() == 0) // empty entry will be dashes {

    for (int i = 1; i <= VL; i++) {

      ost << "- "; //ost~cout

    }

    else {

    // TODO show the content of v separated by blanks

    for (int i = 0; i < v.size(); i++) {

      ost << v[0] << ' ';

    }

    }

}

 

// display the matrix

void displayTable() {

  // display T nicely labeled with row numbers

  //    by calling showVector with cout

  //    for each column

  for (int r = 0; r < R; r++) {

    cout << r << ":";

    for (int c = 0; c < C; c++) {

      showVector(T[r][c], cout);

      cout << "|";

    }

    cout << endl;

  }

}

 

// save the matrix to the output file

void saveTable() {

  ofstream fout("table.txt", ios::out);

 

  //    output T nicely labeled with row numbers

  //    by calling showVector with fout

  //    for each column

  // TODO

      if (!fout) {

      cerr << "Error opening file." << endl;

        return;

    }

    // Iterate over each column

    for (size_t col = 0; col < C; col++) {

        // Extract the column

     

        vector<char> column;    

        for (size_t row = 0; row < R; row++) {

            column.push_back(T[row][col][0]);

        }

 

        // Output the column nicely labeled with row numbers

        showVector(T[R][C],fout);

    }

    fout.close();

}

 

int main() {

  cout << "reading from lines.txt" << endl;

  readTable();

  displayTable();

 

  cout << "saving the table in table.txt" << endl;

  saveTable();

} // the end

0: a bcbef|-
1:-
-
-
-
-
-
-
chi
2:-
-
-
-
-
-
-
-
-
-
-
-
|d k 1 |
Transcribed Image Text:0: a bcbef|- 1:- - - - - - - chi 2:- - - - - - - - - - - - |d k 1 |
AI-Generated Solution
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
steps

Unlock instant AI solutions

Tap the button
to generate a solution

Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education