Matrix problem windows, c++, codeblocks, gnu gcc compiler

closed account (9G3MizwU)
I haven't used C ++ for a while, today I started making a program with the matrix, but I have a problem:
when I assign values to each row, the last row changes the values of the previous rows.

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
#include <iostream>

using namespace std;

int main()
{
    int I=0,J=0,i,j,A[I][J];
    cout<<"Matrix"<<endl<<"Rows: ";
    cin>>I;
    cout<<"Columns: ";
    cin>>J;
    for(i=0;i<I;i++)
    {
        cout<<"Enter values "<<i+1<<"* row:"<<endl;
        for(j=0;j<J;j++)
        {
            cin>>A[i][j];
        }
    }
    for(i=0;i<I;i++)
    {
        cout<<endl;
        for(j=0;j<J;j++)
        {
            cout<<A[i][j]<<" ";
        }
    }
    return 0;
}
Last edited on
1
2
3
4
5
6
    int I=0,J=0,i,j,A[I][J];
    cout<<"Dati Matrice"<<endl<<"Righe: ";
    cin>>I;
    cout<<"Colonne: ";
    cin>>J;
 

Your array has zero size, and updating I,J does NOT cause your array to resize.

Variable sized arrays are also illegal in C++ code.
closed account (9G3MizwU)
I'm stupid. OK, so i just create the matrix after entering the row and column values, thanks.
Last edited on
Except that, as @salem c said, variable sized arrays are illegal in C++. The size of the array must be known at compile-time.

You should use the std::vector type instead of a C-style array. See, for example:

https://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

Using a 2D vector:
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
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>

int main()
{
   std::cout << "Creating the matrix...\nRows: ";
   size_t row_size { };
   std::cin >> row_size;

   std::cout << "Columns: ";
   size_t col_size { };
   std::cin >> col_size;
   std::cout << '\n';

   // create a 2D vector with known sizes
   std::vector<std::vector<int>> matrix(row_size, std::vector<int>(col_size));

   for (size_t row { }; row < row_size; row++)
   {
      for (size_t col { }; col < col_size; col++)
      {
         std::cout << "value for matrix[" << row << "][" << col << "]: ";
         std::cin >> matrix[row][col];
      }
   }
   std::cout << '\n';

   for (size_t row { }; row < row_size; row++)
   {
      for (size_t col { }; col < col_size; col++)
      {
         std::cout << matrix[row][col] << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   std::cout << "Display a 2D vector using iterators....\n";
   for (auto row_itr { matrix.cbegin() }; row_itr != matrix.cend(); row_itr++)
   {
      for (auto col_itr { row_itr->cbegin() }; col_itr != row_itr->cend(); col_itr++)
      {
         std::cout << *col_itr << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   std::cout << "Display a 2D vector using range-based loops....\n";
   for (const auto& row_itr : matrix)
   {
      for (const auto& col_itr : row_itr)
      {
         std::cout << col_itr << ' ';
      }
      std::cout << '\n';
   }
}
If you want to get really fancy you can overload std::ostream's operator<<. Works for 1D and 2D vectors:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <vector>
#include <numeric>

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v);

int main()
{
   std::vector<int> vec { 1, 2, 3, 4, 5, 6, 7 };

   std::cout << "Display the 1D vector...\n";
   std::cout << vec << '\n';

   std::cout << "\nCreating the 2D vector...\nRows: ";
   size_t row_size { };
   std::cin >> row_size;

   std::cout << "Columns: ";
   size_t col_size { };
   std::cin >> col_size;
   std::cout << '\n';

   // create a 2D vector with known sizes, all values initialized to zero
   std::vector<std::vector<int>> matrix(row_size, std::vector<int>(col_size));

   // initialize the vector with some values other than zero
   int start  { 101 };
   int offset { 100 };

   // step through each row and fill the row vector with some values
   for (auto& itr : matrix)
   {
      std::iota(itr.begin(), itr.end(), start);
      start += offset;
   }

   std::cout << "Display the 2D vector...\n";
   std::cout << matrix;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      os << x << ' ';
   }

   return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v)
{
   for (auto const& x : v)
   {
      os << x << '\n';
   }

   return os;
}
Topic archived. No new replies allowed.