Making a matrice with a headline.

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?
A matrix doesn’t have a header.

Output has a header.

Keep them separate and your life will be ever so much easier.


For further help with your code, post some code.
Here's one way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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)
   const int m_rows = 5;
   const int 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 (const auto& row_itr : myMatrix)
   {
      for (const auto& col_itr : row_itr)
      {
         std::cout << col_itr << ' ';
      }
      std::cout << '\n';
   }
}
1 2 3 4
2 2 2 2
4 4 4 4
6 6 6 6
8 8 8 8
you should do it like this.
1
2
3
4
5
6
7
8
9
10
11
12
class matrix
{
   int rows, cols;
   vector<string> headers;
   vector<double> data;
   ...
   somemethod() 
{
   for(i = ...stuff... i< cols) 
    headers.push_back(wordsfromsomewhere());
 }
}


interestingly the same code could also label the rows, if you needed to do that, just add another loop for all the rows after the columns.

it would be possible to use a union/any type thing and jack it all into data, but that is going to waste a great deal of space and time.
Last edited on
Furry guy!

Thank you so much, I got it to work to some capacity, but how would you modify it to make it so the vector is of variable size inputted by the user?
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)
   const int m_rows = 5;
   const int 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.
need to see code to help on that.
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.
Topic archived. No new replies allowed.