two dimensional matrix array problem
hello, dear all. below is my program code. the problem is an half of my output is zero for entries of matrix for n greater than 4.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MAX 100
int num[MAX][MAX];
int n;
int nrows = 0;
using namespace std;
void print(int i )
{
if (i > 0) {
for (int j = 1; j <=n; j++)
//cout << num[i][j];
cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl;
cout <<"\n";
}
}
void rightRotate ( int n)
{int tmp;
for(int k = 1; k<n; ++k)
for(int l = 1; l<=n; ++l)
{
//cout << "l:"<< l << "\n";
tmp = num[l][k];
num[l][k]= num[l][k+1];
num[l][k+1] = tmp;}
}
void matrixPermute (int n)
{
int i, temp;
if (n ==3 )
{nrows ++;
print(nrows);
return;
}
temp = n-1 ;
cout << "temp:"<< temp<<"\n";
for (i = 1; i <=temp ; ++i)
{
//cout << "i:" << i << "\n";
matrixPermute (temp);
rightRotate (temp);
}
}
void initiate(int n)
{
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
num[i][j] = j;
}
}
int main ()
{
cout << "number must be between 3 and 10 \n";
cin >> n;
cout << endl;
initiate(n);
matrixPermute (n);
cout << "nrows=" <<nrows << "\n";
}
its cant read the following code that i set it up
void matrixPermute (int n)
{
int i, temp;
if (n ==3 )
{nrows ++;
print(nrows);
return;
}
shamila08
Junior Poster in Training
51 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
What are you trying to accomplish?
Can you be more descriptive?
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
let me show the output
number must be between 3 and 10:
5
....
......
num[5][1]=1
num[5][2]=2
num[5][3]=3
num[5][4]=4
num[5][5]=5
num[6][1]=0
num[6][2]=0
num[6][3]=0
num[6][4]=0
num[6][5]=0
temp:3
num[7][1]=0
num[7][2]=0
num[7][3]=0
num[7][4]=0
num[7][5]=0
num[8][1]=0
num[8][2]=0
num[8][3]=0
num[8][4]=0
num[8][5]=0
num[9][1]=0
num[9][2]=0
num[9][3]=0
num[9][4]=0
num[9][5]=0
temp:3
num[10][1]=0
num[10][2]=0
num[10][3]=0
num[10][4]=0
num[10][5]=0
num[11][1]=0
num[11][2]=0
num[11][3]=0
num[11][4]=0
num[11][5]=0
num[12][1]=0
num[12][2]=0
num[12][3]=0
num[12][4]=0
num[12][5]=0
nrows=12
Press any key to continue . . .
the problem is when nrows = 6 until 12, the related elements is zero. something is not corresponds to the nrows.
shamila08
Junior Poster in Training
51 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
when i remove try did not use nrows such as follows:
void print()
{
if (num != 0) {
int i=1;
for (int j = 1; j <=n; j++)
cout << num[i][j];
// cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl;
cout <<"\n";
}
}
and
void matrixPermute (int n)
{
int i, temp;
if (n ==3 )
{ nrows++;
print();
return;
}
this is the output
number must be between 3 and 10
5
temp:4
temp:3
12345
23145
31245
temp:3
23415
34215
42315
temp:3
34125
41325
13425
temp:3
41235
12435
24135
nrows=12
Press any key to continue . . .
however i need to represent the output in matrix form. i couldnt make it. i need the index 'i' will follows nrows index.
shamila08
Junior Poster in Training
51 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
line 50 and 51: array numbers always begin with 0, not 1.
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
num[i][j] = j+1;
}
}
line 44: >>matrixPermute (temp);
This is a recursive function call, yet you always are passing the same value of temp which never changes in that loop. It looks like that function will be an infinite recursion, which will eventually crash and burn the program.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
thanks....
yup that's recursive function. the index 'temp', we can change it where i use code as follows:
if (n ==2 )
{ nrows++;
print();
return;
}
or we can set up
if (n ==1 )
{ nrows++;
print();
return;
}
regarding to my last previous post is i have to represent the output in matrix form correspond to nrows index.
when n = 4, there is no problem. however problem arises when n>4..
is it possible or i have to rewrite the program without 'temp'?
shamila08
Junior Poster in Training
51 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
ok. i start index with '0' not '1'.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MAX 100
int num[MAX][MAX];
int n;
int nrows = 0;
using namespace std;
void print(int i)
{
if (num != 0) {
for (int j = 0; j <n; j++)
// cout << num[0][j];
cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl;
cout <<"\n";
}
}
void rightRotate ( int n)
{int tmp;
for(int k = 0; k<n-1; ++k)
for(int l = 0; l<n; ++l)
{
//cout << "l:"<< l << "\n";
tmp = num[l][k];
num[l][k]= num[l][k+1];
num[l][k+1] = tmp;}
}
void matrixPermute (int n)
{
int i, temp;
if (n ==3 )
{ nrows++;
print(nrows);
return;
}
temp = n-1 ;
cout << "temp:"<< temp<<"\n";
for (i = 0; i <temp ; ++i)
{
//cout << "i:" << i << "\n";
matrixPermute (temp);
rightRotate (temp);
}
}
void initiate(int n)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
num[i][j] = j+1;
}
}
int main ()
{
cout << "number must be between 3 and 10 \n";
cin >> n;
cout << endl;
initiate(n);
matrixPermute (n);
cout << "nrows=" <<nrows << "\n";
}
its also obtained the same output. i understand because it related to 'temp'.
the meaning of the temp is follows:
n = 4
when temp =3,
it will right rotate the first three element such as follows:
1234
2314
3124..........
for n = 5:
when temp 4, it will rotate the first 4 element:
12345 ( the list is not appear).
23415
34125
41235
the continue temp = 3: ( that's the last output)
12345
23145
31245
12345
shamila08
Junior Poster in Training
51 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0