Hey!
I've been struggling to get this done, I can make a normal matrice work, but I can't figure out how to make it so the first line is a headline, that I would fill with char entries so I could name the columns, then fill the first column with 1, 2, 3, 4, ... n, where n is the number of columns, and then fill the rest of the matrice with numerical values inputed by the user, could someone help me out?
#include <iostream>
#include <vector>
int main()
{
// set the dimensions of the matrix
// the number of rows need to be one bigger
// than the expected number of rows (header)
constint m_rows = 5;
constint m_cols = 4;
// create a blank 2D vector (matrix)
std::vector<std::vector<int>> myMatrix;
// create a temp "row" vector to hold the data for each column
std::vector<int> temp(m_cols);
// fill the temp vector with the header numbers
for (int i = 0; i < m_cols; i++)
{
temp[i] = i + 1;
}
myMatrix.push_back(temp);
for (int i = 1; i < m_rows; i++)
{
// code to fill the temp vector from user input
// let's just fill the temp vector with some unique values as a test
for (auto& itr : temp)
{
itr = i * 2;
}
// push the temp filled vector into the matrix
myMatrix.push_back(temp);
}
// let's display the matrix
for (constauto& row_itr : myMatrix)
{
for (constauto& col_itr : row_itr)
{
std::cout << col_itr << ' ';
}
std::cout << '\n';
}
}
Instead of creating const row and column sizes, you simply ask the user for m_row and m_col.
IOW, change:
6 7 8 9 10
// set the dimensions of the matrix
// the number of rows need to be one bigger
// than the expected number of rows (header)
constint m_rows = 5;
constint m_cols = 4;
to:
6 7 8 9 10 11 12 13 14 15 16
// get the row and column sizes from the user
std::cout << "How many rows do you want? ";
int m_rows;
std::cin >> m_rows;
// add an extra row for the header
m_rows++;
std::cout << "How many columns do you want? ";
int m_cols;
std::cin >> m_cols;
Do note, there is ZERO error checking for bad input.
Thanks! But I've tried changing the row and column sizes from constants to variables, but only the number of rows varies with my input, no matter what I answer in regard to columns it always defaults to 32.
I've tried changing the row and column sizes from constants to variables, but only the number of rows varies with my input, no matter what I answer in regard to columns it always defaults to 32.
Did you change the consts in the code I posted to inputs, or you modified your own code?
Hard to say what is wrong without being able to see your code.