Problem in declaring array.

1
2
3
4
5
6
7
8

int matrix=[][];

clrscr();

printf("Enter size of matrix: ");
scanf("%d",&matrix[][]);



guys i have a problem .. i dont know how to make an array of a matrix that needs a user to input its size. can anyone help?

im making a program find the saddle point ..
Last edited on
Dynamically allocate memory.

etc
1
2
3
4
5
int* matrix;
int size;

cin >> size;
matrix = new int[size];


An array is really a pointer to a block of contiguous memory all having a common yet independent data value of the same type.
Last edited on
The anwser you gave is valid for a 1D array.

A matrix is a 2D array.

Can you explain how to dinamically allocate memory to a 2D and how to set up the pointer?

I'm having the same problem.
You can use a 1D array to mimic a 2D array.

Allocating a 2D arrays is more of a nightmare.

See this for reasons why you should avoid it, and for the ways you can do it if you insist on moving forward with it:

http://cplusplus.com/forum/articles/17108/
Thank's, problem not solved yet but that's just because I haven't had time to read it all, although I get the general picture.
I gave you a solution for both 1D and 2D..

If you want to stick with C arrays, then you can either create an array in for each element in the array. Or you can use divide/times to get the positions in a single array, for multiple dimensions. Otherwise you can use a boost::multi_array if resizing is required.
No resizing required. What upsets me with vector<> is that from what I read I understood that it allocates more memory than necessary in case it needs to grow.

That seems stupid to me, as if I know how big I need the array to be, then theres no need to spend more memory than I need to..
try this code:

int matrix=[10][10];

clrscr();


for(int x=0;x<10;x++)
{
for(int y=0;y<10;x++)
{
cout<<"Enter size of Matrix");
cin>>matrix;

cout<<"The Size of the matrix is :"<<matrix[x][y];
}
}
Last edited on
if you will input an array you should have a repetition statements..

Topic archived. No new replies allowed.