#include <iostream>
#include <sstream>
usingnamespace std;
int main() {
int numRows = 0;
int numCols = 0;
int index = 0;
int j = 0;
int matrix[1000][1000];
stringstream ss;
string str = "2 8 4";
cout << "enter row " << numRows << " for the matrix: ";
getline(cin, str);
ss.str(str);
while(ss >> matrix[numRows][numCols])++numCols; ++numRows;
cout << "The program has determined the matrix will have " << numCols;
cout << " columns.";
cout << endl << endl;
while(1)
{
cout << "enter row " << numRows << " for the matrix: ";
ss.str("");
ss.clear();
getline(cin, str);
ss.str(str);
index = 0;
while((ss >> matrix[numRows][index])) ++index;
++numRows;
if(index == 0) break;
}
cout << "The program has determined the matrix will have " << numRows << " rows. ";
cout << "The size : " << numCols << " x " << numRows << endl << endl;
for (index = 0; index < numRows; index++)
{
for (int j = 0; j < numCols; j++)
{
cout << matrix[index][j];
}
}
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
enter row 0 for the matrix: 9 9 9
The program has determined the matrix will have 3 columns.
enter row 1 for the matrix: 7 7 7
enter row 2 for the matrix: 2 2 2
enter row 3 for the matrix:
The program has determined the matrix will have 4 rows. The size : 3 x 4
999777222000
Process finished with exit code 0
FIXED input error but
Still adding unnecessary zeros any tips to get rid of them
Thank you for putting in the time and care to help me solve my problem.
I am going to learn from the way you helped me. You have done an outstanding job.
Thank you again! You are amazing!
#include "Header.h"
/**************************************************************
* First Matrix - These function blocks will prompt the user to
* input the values of the first matrix and
* output the entire matrix.
**************************************************************/
void clMatrix::promptForFirstMatrix()
{
int numRows = 0;
int numCols = 0;
int index = 0;
int j = 0;
stringstream ss;
string str = "2 8 4";
cout << "enter row " << numRows << " for the matrix: ";
getline(cin, str);
ss.str(str);
while(ss >> firstMatrix[numRows][numCols])++numCols; ++numRows;
cout << "The program has determined the matrix will have " << numCols;
cout << " columns.";
cout << endl << endl;
while(1)
{
cout << "enter row " << numRows << " for the matrix: ";
ss.str("");
ss.clear();
getline(cin, str);
ss.str(str);
index = 0;
while((ss >> firstMatrix[numRows][index])) ++index;
++numRows;
if(index == 0) break;
}
cout << "The matrix will have the size : " ;
cout << numCols << " x " << numRows -1 << endl << endl;
}
void clMatrix::outputFirstMatrix()
{
cout << endl;
int numRows = 0;
int numCols = 0;
for (int index = 0; index < numRows -1; index++)
{
for (int j = 0; j < numCols; j++)
{
cout << firstMatrix[index][j];
}
}
}
You didn't really fix the logic error I have pointed out. So that you don't have to manually write numRows - 1 everywhere - it would be a bad practice if you repeat using numRows - 1 in your code.
> This is the logic error I fixed some minutes ago.