### 2d array

hello i want to make a 2d array using variables and to generate random values in it which is the probability of programme.as user enter three integers m,n,p.m and n make a 2d array and the random values are generated according to probability p entered by user.but i am getting some trouble,kindly tell me what is the right way to do it,
when size of array is given by user mean a[m][n];then my code is here
#include<iostream.h>
#include<conio.h>
int rand ();
void main ()
{
clrscr();
int m,n=0;
cout<<"enter two numbers";
cin>>m>>n;
int a[m][n];
int p=0;
cout<<"enter probability";
cin>>p;
for(int i=0;i<m;i++)
{
for(int j=o;j<n;j++)
int [i][j]=rand();
}
for(int i=0;i<m;i++)
{
for(int j=o;j<n;j++)
cout<<a[i][j];
}
getch();
}

Last edited on
you can't set up the array's size like this. either use pointers or use a fixed size.
Use `std::vector<>`
http://www.mochima.com/tutorials/vectors.html

 ``12345678910111213141516171819`` ``````#include #include int main () { std::size_t nrows, ncols ; std::cout << "enter number of rows: " ; std::cin >> nrows ; std::cout << "enter number of cols: " ; std::cin >> ncols ; // invariant: nrows, ncols are greater than zero typedef std::vector row_t ; std::vector matrix( nrows, row_t(ncols) ) ; // use matrix. eg. matrix[0][0] = 1234 ; }``````
Topic archived. No new replies allowed.