indefinite matrix

hi, i am still beginner and don't know how to use but c++ and i was wondering how can i make array that its order is entered by the user but i don't know how to make int x[r][c] work ?

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
  # include <iostream>
using namespace std;
int main()
{
	int r=1, c=1;
	cout << "please enter the number of rows" << endl;
	cin >> r;
	cout << "please enter the number of the colums" << endl;
	cin >> c;
	if (r > 0 && c > 0)
	{
		int x[r][c];
		for (int i = 0; i < r; i++)
		{
			for (int j = 0; j < c; j++)
			{
				cout << "please enter the value of the order (" << i << "," << j << ")" << endl;
				cin >> x[i][j];
			}
		}
	}
	else
		cout << "please enter number of row and colums bigger then 0" << endl;
	system("pause");
	
}
your code seems fine. don't know what you mean by:

but i don't know how to make int x[r][c] work ?
when i write int x[r][c] the programme doesn't want to run, it say that r & c aren't constant
Your code is not fine.

1
2
3
4
5
6
cout << "please enter the number of rows" << endl;
cin >> r;
cout << "please enter the number of the colums" << endl;
cin >> c;

int x[r][c];


This can not be done in c++. The array has to have a const value as your error is saying. If you want to do what you're code is showing. You'd have to allocate memory for the array.

Read here - http://www.cplusplus.com/forum/beginner/63/

And here - http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new

@hellwolf @TarikNeaj don't understand what you guys are talking about. seems and runs fine in codeblocks and cpp.sh. adding cout shows inputs are indeed stored. here is the link:

cpp.sh/4cjf
1
2
3
4
5
6
cout << "please enter the number of rows" << endl;
cin >> r;
cout << "please enter the number of the colums" << endl;
cin >> c;

int x[r][c];


You cant do that in c++.If your compiler is not picking up on that then well, maybe get another one. You can go ask anyone who knows anything about programming.

Read here - http://stackoverflow.com/questions/15013077/arrayn-vs-array10-initializing-array-with-variable-vs-real-number

Or here - http://stackoverflow.com/questions/20601512/c-user-defined-2-dimensional-array-with-geometrical-progression

Edit :Or, Read this from a still active thread - http://www.cplusplus.com/forum/beginner/161065/
Last edited on
the last link was way more helpful. really did learn a new(and strange) thing. thnx for the links.
That's valid in C99, however unfortunately not everything in C is available in C++.

Suggestions:
- Dynamically allocate your arrays
- Get a compiler that supports this non-standard extension.
Last edited on
Topic archived. No new replies allowed.