Rayne
Rayne

Reputation: 14977

Varying number of for-loops

I'm trying to write a code that could print something like this

-xv
-xvv
-xvvv
-xvvvv
-xvvvvv

-xxv
-xxvv
-xxvvv
-xxvvvv
-xxvvvvv

-xxxv
-xxxvv
-xxxvvv
-xxxvvvv
-xxxvvvvv

The extra spacing between the 3 "groups" are just for clarity's sake. The maximum number of '-' is 1, 'x' is 3, 'v' is 5, and the number of each symbol increments.

To draw this diagram, I have the following code

for (k = 1 ; k <= num_dash ; k++)
{
   for (i = 1 ; i <= num_x ; i++)
   {
      for (j = 1 ; j <= num_v ; j+++)
      {
         for (k1 = 0 ; k1 < k ; k++)
            printf("-");
         for (i1 = 0 ; i1 < i ; i++)
            printf("x");
         for (j1 = 0 ; j1 < j ; j++)
            printf("v");
         printf("\n");
      }
   }
}

This is when I know there are 3 different kinds of symbols. Is it possible to do the same if the number of symbols are only known at runtime? For example, what if I want the same program to also be able to print

xv
xvv
xvvv
xvvvv
xvvvvv

xxv
xxvv
xxvvv
xxvvvv
xxvvvvv

xxxv
xxxvv
xxxvvv
xxxvvvv
xxxvvvvv

In this case, my code would only be

   for (i = 1 ; i <= num_x ; i++)
   {
      for (j = 1 ; j <= num_v ; j+++)
      {
         for (i1 = 0 ; i1 < i ; i++)
            printf("x");
         for (j1 = 0 ; j1 < j ; j++)
            printf("v");
         printf("\n");
      }
   }

And there are only 2 for-loops instead of 3. Can I write my code such that the number of for-loops varies?

Upvotes: 0

Views: 649

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

E.g.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print_aux(char **symbols, int *times, int len, int pos, char *acc){
    int i;
    char *p;

    if(pos == len){
        printf("%s\n", acc);
        return;
    }
    p = malloc(sizeof(char)*strlen(symbols[pos])*times[pos]+strlen(acc)+1);
    for(i=0;i<times[pos];++i){
        int j;
        *p='\0';
        strcpy(p, acc);
        for(j=0;j<=i;++j){
            strcat(p, symbols[pos]);
        }
        print_aux(symbols, times, len, pos + 1, p);
    }
    if(pos + 1 == len)// when last symbol
        printf("\n",pos);
    free(p);
}

//wrap function
void print(char **symbols, int *times, int len){
    print_aux(symbols, times, len, 0, "");
}

int main() {
    int i,n;
    char **symbols;
    int  *times;

    fprintf(stderr,"number of symbols :");
    scanf("%d", &n);
    symbols=(char**)malloc(sizeof(char*)*n);
    times = (int*)malloc(sizeof(int)*n);
    for(i=0;i<n;++i){
        char wk[128];
        fprintf(stderr,"input symbol [%d]:", i+1);
        scanf(" %s", wk);
        symbols[i] = strdup(wk);
        fprintf(stderr,"maximum number of \"%s\":",wk);
        scanf(" %d", &times[i]);
    }

    print(symbols, times, n);

    {   //release the allocated area
        for(i=0;i<n;++i){
            free(symbols[i]);
        }
        free(symbols);
        free(times);
    }
    return 0;
}

Upvotes: 1

hcg
hcg

Reputation: 652

Use functions instead of innermost for loops and pass the characters and number of iterations as parameters to that function.

Upvotes: 2

Related Questions