Link to home
Start Free TrialLog in
Avatar of rmtogether
rmtogether

asked on

print any sequence length

hi, I have a program(using C language) that can print 3 sequence of length from a matrix (like below). It prints all possible combination for sequence length of 3 (which is 3^3=27 possible combinations).

how can I modify the code to let me print any sequence length. I have try to use multiple for loop. it works, but if I want to have sequence of 100, I need write 100 for loops..........terrible!!!

how can I do it more efficiently? could you please help me modify it with using VB. thanks

----------------my code-------------------------
#include <stdio.h>
#include <float.h>

 
int matrix [3][3]={{3,3,4},
                     {8,1,1},
                     {3,3,4}};                    
                   

main (){
    int i,j,k,l;
    long max = 0;

     for (i=0; i<3; i++){
          printf("\n");
         for (j=0; j<3; j++) {
             //printf("\n");
            for (k=0; k<3; k++){
                   printf("Prob(%d%d%d)=%d\n",i,j,k,matrix[i][j]+matrix[j][k]);  
                 
              }//end third for
                       
        }// end second for
    }// end first for

    system("pause");
}
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

Show me 1 example output
Also tell me how you want to pass matrix data to code? from file?
Avatar of rmtogether
rmtogether

ASKER

(1) in my C code, it print 3^3 combinations from 000 to 222 like below.

----sample output-----------------------------
Prob(000)=6
Prob(001)=6
Prob(002)=7
Prob(010)=11
Prob(011)=4
Prob(012)=4
Prob(020)=7
Prob(021)=7
Prob(022)=8

Prob(100)=11
Prob(101)=11
Prob(102)=12
Prob(110)=9
Prob(111)=2
Prob(112)=2
Prob(120)=4
Prob(121)=4
Prob(122)=5

Prob(200)=6
Prob(201)=6
Prob(202)=7
Prob(210)=11
Prob(211)=4
Prob(212)=4
Prob(220)=7
Prob(221)=7
Prob(222)=8
Prob(200)=6
Prob(201)=6
Prob(202)=7
Prob(210)=11
Prob(211)=4
Prob(212)=4
Prob(220)=7
Prob(221)=7
Prob(222)=8

 I would like the program can do any sequence. For example, if sequence of 4, it will 3^4 combinations ( from 0000 to 2222)
for sequence of 5, will print 3^5 combinations (00000 to 22222). I would like the program can print sequence of n (3^n combinations)


(2) at this point, I think matrix just need define inside the code like I declare in C
int matrix [3][3]={{3,3,4},
                     {8,1,1},
                     {3,3,4}};        
You need output to be same?
You need output in a order? Could it be rondom but include all? Do you want to remove duplicates?
what do you mean output to be same?


I think I made some mistake in my sample output, the sample output has 3^3=27 combinations like below
the result should be in order: from 000 001 002.................222
--------------------------------------
Prob(000)=6
Prob(001)=6
Prob(002)=7
Prob(010)=11
Prob(011)=4
Prob(012)=4
Prob(020)=7
Prob(021)=7
Prob(022)=8

Prob(100)=11
Prob(101)=11
Prob(102)=12
Prob(110)=9
Prob(111)=2
Prob(112)=2
Prob(120)=4
Prob(121)=4
Prob(122)=5

Prob(200)=6
Prob(201)=6
Prob(202)=7
Prob(210)=11
Prob(211)=4
Prob(212)=4
Prob(220)=7
Prob(221)=7
Prob(222)=8


 
I want have everything like 3 sequence should have 3^3=27 results
Avatar of Ark
Can you explain what sequnce numbers mean? Row/colum numbers? If so, isn't Prob(000) shoud be 14 (3+8+3)?
hi, Ark

the following C code is exactly what I want. If you can help me write in VB that will be great. thanks in advance.



#include<stdio.h>
#define LEN 4  //change the value of LEN for different size

//int matrix[LEN][LEN];
int INDEX[LEN+1];  //initially 0
int output[LEN];

int matrix [3][3]=  {{3,3,4},
                     {8,1,1},
                     {3,3,4}
                     };  


void print_output()
{
  int i;
  for(i=0;i<=LEN;i++){
        printf("%d",INDEX[i]);
  }
  int sum=0;  
  for( i=0;i<LEN;i++ ){
    sum += matrix[INDEX[i]][INDEX[i+1]];
  }
  printf("=%d\n",sum);
}

/**
  generating combination using backtracking
*/
void generate_combination(int count)
{
  int i;
  if( count > LEN ){
    print_output();
    return;
  }
  for(i=0;i<3;i++ ){         // match n x n matrix, n=3 this case
    INDEX[count]=i;
    generate_combination(count+1);
  }
}

int main()
{
  int i,j;
  /*
  for( i=0;i<LEN; i++ ){
     for(j=0;j<LEN;j++){
       printf("%d ",matrix[i][j] = rand()%LEN);
     }
     printf("\n");
  }
  */
  generate_combination(0);
  system("pause");
}

Hi

Add button and multiline textbox on form

'Const length = 4 '//change the value of LEN for different size
Dim length As Long 'For changing dynamically
'Dim idx(length) As Long '//initially 0
Dim idx() As Long '//initially 0
'Dim output(length - 1) As Long
Dim output() As Long
'//Dim matrix(length-1,length-1)
Dim matrix() As Long

Private Sub print_output()
  Dim i As Long
  Dim s As String
 
  s = Chr(34)
  For i = 1 To length
      s = s & idx(i)
  Next i
  s = s & Chr(34) & Space(5)
  Dim sum As Long
  For i = 1 To length - 1
      sum = sum + matrix(idx(i), idx(i + 1))
  Next i
  s = s & sum
  Text1.Text = Text1.Text & vbCrLf & s
End Sub

'/**
'  generating combination using backtracking
'*/
Private Sub generate_combination(ByVal count As Long)
  Dim i As Long
  If count > length Then
    print_output
    Exit Sub
  End If
  For i = 0 To length - 1    ' // match n x n matrix, n=3 this case
    idx(count) = i
    generate_combination (count + 1)
  Next i
End Sub

Sub DoMain()
  Dim i As Long, j As Long
  Dim s As String
 
  length = 3 'change to any size
  ReDim idx(length)
  ReDim output(length - 1)
  ReDim matrix(length - 1, length - 1)
'================Fixed array like in your sample for testing========
  Dim tmp
  tmp = Array(3, 3, 4, 8, 1, 1, 3, 3, 4)
  For i = 0 To length - 1
     For j = 0 To length - 1
        matrix(i, j) = tmp(i * length + j)
     Next j
  Next i
'====================================================================
  Text1.Text = "Matrix:" & vbCrLf
  For i = 0 To length - 1
     For j = 0 To length - 1
'uncomment for get random array
'        matrix(i, j) = GetRandom(1, 9)
        s = s & matrix(i, j) & " "
     Next j
     s = s & vbCrLf
  Next i
  Text1.Text = Text1.Text & s
  Call generate_combination(0)
End Sub

Function GetRandom(ByVal minValue As Long, ByVal maxValue As Long) As Long
   Randomize
   GetRandom = Int((maxValue - minValue + 1) * Rnd + minValue)
End Function

Private Sub Command1_Click()
   DoMain
End Sub

'======Sample output:
Matrix:
3 3 4
8 1 1
3 3 4

"000"     6
"001"     6
"002"     7
"010"     11
"011"     4
"012"     4
"020"     7
"021"     7
"022"     8
"100"     11
"101"     11
"102"     12
"110"     9
"111"     2
"112"     2
"120"     4
"121"     4
"122"     5
"200"     6
"201"     6
"202"     7
"210"     11
"211"     4
"212"     4
"220"     7
"221"     7
"222"     8
"000"     6
"001"     6
"002"     7
"010"     11
"011"     4
"012"     4
"020"     7
"021"     7
"022"     8
"100"     11
"101"     11
"102"     12
"110"     9
"111"     2
"112"     2
"120"     4
"121"     4
"122"     5
"200"     6
"201"     6
"202"     7
"210"     11
"211"     4
"212"     4
"220"     7
"221"     7
"222"     8
"000"     6
"001"     6
"002"     7
"010"     11
"011"     4
"012"     4
"020"     7
"021"     7
"022"     8
"100"     11
"101"     11
"102"     12
"110"     9
"111"     2
"112"     2
"120"     4
"121"     4
"122"     5
"200"     6
"201"     6
"202"     7
"210"     11
"211"     4
"212"     4
"220"     7
"221"     7
"222"     8
Sorry, correct version:

'Const length = 4 '//change the value of LEN for different size
Dim length As Long 'For changing dynamically
'Dim idx(length) As Long '//initially 0
Dim idx() As Long '//initially 0
'Dim output(length - 1) As Long
Dim output() As Long
'//Dim matrix(length-1,length-1)
Dim matrix() As Long

Private Sub print_output()
  Dim i As Long
  Dim s As String
 
  s = Chr(34)
  For i = 0 To length - 1
      s = s & idx(i)
  Next i
  s = s & Chr(34) & Space(5)
  Dim sum As Long
  For i = 0 To length - 2
      sum = sum + matrix(idx(i), idx(i + 1))
  Next i
  s = s & sum
  Text1.Text = Text1.Text & vbCrLf & s
End Sub

'/**
'  generating combination using backtracking
'*/
Private Sub generate_combination(ByVal count As Long)
  Dim i As Long
  If count >= length Then
    print_output
    Exit Sub
  End If
  For i = 0 To length - 1    ' // match n x n matrix, n=3 this case
    idx(count) = i
    generate_combination (count + 1)
  Next i
End Sub

Sub DoMain()
  Dim i As Long, j As Long
  Dim s As String
 
  length = 3
  ReDim idx(length - 1)
  ReDim output(length - 1)
  ReDim matrix(length - 1, length - 1)
'================Fixed array like in your sample for testing========
  Dim tmp
  tmp = Array(3, 3, 4, 8, 1, 1, 3, 3, 4)
  For i = 0 To length - 1
     For j = 0 To length - 1
        matrix(i, j) = tmp(i * length + j)
     Next j
  Next i
'====================================================================
  Text1.Text = "Matrix:" & vbCrLf
  For i = 0 To length - 1
     For j = 0 To length - 1
'uncomment for get random array
'        matrix(i, j) = GetRandom(1, 9)
        s = s & matrix(i, j) & " "
     Next j
     s = s & vbCrLf
  Next i
  Text1.Text = Text1.Text & s
  Call generate_combination(0)
End Sub

Function GetRandom(ByVal minValue As Long, ByVal maxValue As Long) As Long
   Randomize
   GetRandom = Int((maxValue - minValue + 1) * Rnd + minValue)
End Function

Private Sub Command1_Click()
   DoMain
End Sub

'========sample output:
Matrix:
3 3 4
8 1 1
3 3 4

"000"     6
"001"     6
"002"     7
"010"     11
"011"     4
"012"     4
"020"     7
"021"     7
"022"     8
"100"     11
"101"     11
"102"     12
"110"     9
"111"     2
"112"     2
"120"     4
"121"     4
"122"     5
"200"     6
"201"     6
"202"     7
"210"     11
"211"     4
"212"     4
"220"     7
"221"     7
"222"     8
hi, Ark

the Const length is commented, can I uncomment and change value here?


'Const length = 4 '//change the value of LEN for different size
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial