• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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");
}
0
rmtogether
Asked:
rmtogether
  • 4
  • 4
  • 3
1 Solution
 
CSecurityCommented:
Show me 1 example output
0
 
CSecurityCommented:
Also tell me how you want to pass matrix data to code? from file?
0
 
rmtogetherAuthor Commented:
(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}};        
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CSecurityCommented:
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?
0
 
rmtogetherAuthor Commented:
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
0
 
ArkCommented:
Can you explain what sequnce numbers mean? Row/colum numbers? If so, isn't Prob(000) shoud be 14 (3+8+3)?
0
 
rmtogetherAuthor Commented:
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");
}

0
 
ArkCommented:
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
0
 
ArkCommented:
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
0
 
rmtogetherAuthor Commented:
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
0
 
ArkCommented:
Yes, you can, but I specialy dimmed it as variable to allow you change it dynamicaly, by entering value, say, in textbox.
For example, you can change Sub DoMain as following:

Sub DoMain (ByVal matrix_length As Long)
'...............
   length=matrix_length
'...............
End Sub

and call it:
Private Sub Command1_Click()
   DoMain Text1.Text
End Sub

If you want to use it as constant, then uncomment
'Const length = 3 '//change the value of LEN for different size
Comment:
Dim length As Long 'For changing dynamically
'.....
length=3

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now