Solved

print any sequence length

Posted on 2006-11-05
11
227 Views
Last Modified: 2010-04-30
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
Comment
Question by:rmtogether
  • 4
  • 4
  • 3
11 Comments
 
LVL 17

Expert Comment

by:CSecurity
ID: 17877364
Show me 1 example output
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 17877366
Also tell me how you want to pass matrix data to code? from file?
0
 

Author Comment

by:rmtogether
ID: 17877400
(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
 
LVL 17

Expert Comment

by:CSecurity
ID: 17877508
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
 

Author Comment

by:rmtogether
ID: 17877557
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 27

Expert Comment

by:Ark
ID: 17879203
Can you explain what sequnce numbers mean? Row/colum numbers? If so, isn't Prob(000) shoud be 14 (3+8+3)?
0
 

Author Comment

by:rmtogether
ID: 17884089
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
 
LVL 27

Expert Comment

by:Ark
ID: 17886609
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
 
LVL 27

Expert Comment

by:Ark
ID: 17887011
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
 

Author Comment

by:rmtogether
ID: 17888478
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
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
ID: 17894006
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now