### plz help me.

how to initilize (N*N) matrix with random number in the range 1-9.plz help me.
is this true?
const int n=20;
using namespace std;
void initilization (int arr[],int n)
{for (int i=0;i<n;i++){

arr[i]=(rand()%100);
1) You've declared that initialization takes only a single dimensioned array. That's fine, if you're passing n*n as the second argument. Not clear since you didn't post the array declaration or the call to initializaion.

2) Your const n of 20 and the use of n as the argument to initialization is confusing because they're the same name.

3) Your rand calculation is off. If you want a number from 1-9, it should be:
 12 arr[i] = (rand()%9) + 1;

4) You're missing two }

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
this program is giving wrong input.plz see it

#include<iostream>
#include<conio.h>
#include <stdlib.h>
#include <time.h>
const int n=9;
using namespace std;
void initilization (int arr[3][5],int n)
{
for (int i=0;i<n;i++){
for(int j=0;j<n;j++)

arr[i][j] = (rand()%9) + 1;
{
cout<<arr[i]<<" ";}
cout<<endl;
}}
int main(){
int arr[3][5];
cout<<"the random no are"<<endl;
initilization(arr,n);
getch();

}
At line 18, you're declaring the array as 3x5 (15 elements).
At line 20, you're passing 9 as the second argument.
At lines 9,10, you're indexing i from 0-8 and j from 0-8 (81 elements).

It's a wonder your program isn't crashing from writing unallocated memory.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Topic archived. No new replies allowed.