Print various conformation of string in 2-D array
I am presently trying to code a protein folding project in c. Where I will be given a 3-4 length string lets say BWB or BBWW… I have to store this string in 2-D array and print all possible combinations with this string.
if the length of the string is n the length开发者_开发技巧 of the matrix is 2n. and I am storing the 1st element at the center of the array.
What I have tried so far is as follows- I am able to print the number of conformation of a particular input-let's say for 3 letter string it ll generate 12 combinations..for 4 it ll generate 36 combinations..like this..
so my 1st letter ll be at center of the matrix, then second word can be at any position-top,left,right,down of this one..and depending on this second one third one can be at top,right,left or any 3 combinations…
in total i ll have 12 combinations..
I have tried many things till now …and whatever I tried is
#include<string.h>
#include<math.h>
#include<stdio.h>
main()
{
int n=3;
//printf("enter the number of inputs:\t");
//scanf("%d",&n);
int i=4;
int temp=pow((i-1),(n-2));
int comb=i*temp;
//printf("total number of combination is : %d",comb);
char str[3]="ABC";
int size=2*n;
int p;
char mat[size][size];
int j,k;
int a=size/2;
int b=size/2;
for(j=0;j<size;j++)
{
for(k=0;k<size;k++)
{
mat[j][k]='*';
}
}
mat[a][b]=str[0];
int q;
int r;
for(r=1;r<3;r++)
{
for(q=1;q<=4;q++)
{
switch(q)
{
case 1:a=a+1;
break;
case 2:a=a-1;
break;
case 3:b=b+1;
break;
case 4:b=b-1;
break;
}
mat[a][b]=str[r];
}
}
for(p=0;p<comb;p++)
{
for(j=0;j<size;j++)
{
for(k=0;k<size;k++)
{
printf("%c",mat[j][k]);
}
printf("\n");
}
printf("\n");
}
printf("total number of combination is : %d",comb);
}
Output I am getting is
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
******
******
******
***CC*
***C**
******
total number of combination is : 12
Any Help will be appreciated!
You are filling in the array once, and then printing it 12 times. Presumably you want to fill it in 12 different ways and print each one.
Often, this type of problem is done with recursion:
- Place the current character of the string in a possible position, skipping the position if it is already filled.
- If this was the last character in the string, print out the array. Otherwise, recur on the next character.
- Remove the character that was just placed.
- go to 1
For the recurring function, you will probably want to pass in the current position in the array, and the next index in the string. Something like
void do_next(int i, int j, int str_index)
{
if(str_index >= strlen(str))
{
print_array();
return;
}
if(arr[i+1][j] == '*')
{
arr[i+1][j] = str[str_index];
do_next(i+1, j, str_index+1);
arr[i+1][j] = '*';
}
// three similar blocks for the other positions
// don't use "else". Each block should be executed once
}
精彩评论