Something is not right...

I wrote this program and ran it. It didn't print out all the rows, just 3. While I was debugging, I saw that the debug process was stopped. I still have no idea why this happened.

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
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <iostream>
#include <vector>
#include <time.h>

//---------------------------------------------------------------------------

#pragma argsused
using namespace std;
int main(int argc, char* argv[])
{
        int i=0,j=0,b=0,q=0,w=0,c=0,k=0;
        srand(time(NULL));
        cout<<"The number of rows:"<<endl;
        cin>>i;
        cout<<"The number of columns:"<<endl;
        cin>>j;
        vector< vector<int> > a;
        a.resize(i);
        for(b=0;b<=j;b++)
        a[b].resize(j);
        cout<<"The normal matrix:"<<endl;
        for(c=0;c<i*j;c++)
        {
                a[q][w]=1+rand()%9;
                cout<<a[q][w];
                k=j-1;
                if(c%j==k)
                {
                        cout<<endl;
                        q++;
                        w=0;
                }
                else
                {
                        cout<<" ";
                        w++;
                }
        }
        vector< vector<int> > aT;
        aT.resize(j);
        for(b=0;b<=i;b++)
        aT[b].resize(j);
        cin.get();
        cin.get();
        return 0;
}
//---------------------------------------------------------------------------
Welp, AccessViolation is coming. But why?????
I had a quick look.
Check line 23. U sized at i, but U loop until j.
That may help, I hope

same line 45
Last edited on
Nope, that didn't help.
closed account (E0p9LyTq)
Creating 2-dimensional vectors can be tricky:

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

#include <chrono> // for system_clock
#include <random> // for random number engines and distributions

int main()
{
   // obtain a seed from the system clock
   unsigned seed = static_cast<int> (std::chrono::system_clock::now().time_since_epoch().count());

   // seeds the random number engine, the default random engine
   std::default_random_engine gen(seed);

   // set a distribution range (1 - 100)
   std::uniform_int_distribution<int> dist(1, 100);

   // get the number of rows from the user
   std::cout << "The number of rows: ";
   int rows;
   std::cin >> rows;

   // get the number of columns from the user
   std::cout << "The number of columns: ";
   int columns;
   std::cin >> columns;
   
   std::cout << "\n";

   // create the 2-dimensional vector (int) from the supplied values
   std::vector< std::vector<int> > vec(rows, std::vector<int>(columns));

   // fill the vector with some random values
   for (int row_loop = 0; row_loop < rows; row_loop++)
   {
      for (int col_loop = 0; col_loop < columns; col_loop++)
      {
         vec[row_loop][col_loop] = dist(gen);
      }
   }

   // display the contents of the vector
   for (int row_loop = 0; row_loop < rows; row_loop++)
   {
      for (int col_loop = 0; col_loop < columns; col_loop++)
      {
         std::cout << vec[row_loop][col_loop] << "\t";
      }
      std::cout << "\n";
   }
}


The number of rows: 3
The number of columns: 5

83      72      18      86      34
73      63      37      81      52
97      39      22      12      4
closed account (E0p9LyTq)
You can also dynamically create the 2D vector:
http://www.cplusplus.com/forum/general/833/#msg2999

Creating a vector dynamically can lead to memory fragmentation, better if you create the vector with the needed size.
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
#include <iostream>
#include <vector>

//---------------------------------------------------------------------------

using namespace std;
int main(int argc, char* argv[])
{
        int i=0,j=0,b=0,q=0,w=0,c=0,k=0;
        srand(time(NULL));
        cout<<"The number of rows:"<<endl;
        cin>>i;
        cout<<"The number of columns:"<<endl;
        cin>>j;
        vector< vector<int> > a;
        a.resize(i);
        for(b=0;b<=i;b++)
           a[b].resize(j);
        cout<<"The normal matrix:"<<endl;
        
        for(c=0;c<i*j;c++)
        {
                a[q][w]=1+rand()%9;
                cout<<a[q][w];
                k=j-1;
                if(c%j==k)
                {
                        cout<<endl;
                        q++;
                        w=0;
                }
                else
                {
                        cout<<" ";
                        w++;
                }
        }

        return 0;
}
//--------------------------------------------------------------------------- 


back to your code, using C++Shell, it works fine.
I just checked the indexes, and supress the include needed by your compiler.

Don't worry, I just doubled the number of rows and the number of columns to avoid AccessViolation
Topic archived. No new replies allowed.