How to write programme for 'M' for loops dynamically

dynamically i want to write a programme for 'M' for loops i.e. 'M' will be provided at the run time and depending on that 'M' for loops will executes.
Example:
let M=2;
then for(i=0;i {
for(j=0;j {
process();
}
}
can anyone help me out to solve this problem.
Its very urgent & necessery for me.
so please help me out.
Thank u.

Replies

  • sookie
    sookie
    Hi spp,

    Sorry but your problem is not clear to me. Below is my trial of what I understood from the problem specified in above post.

    package myjava;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class MTimesForLoop {
        public static void main(String[] args) throws IOException{
                    BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
                    int M= Integer.parseInt(bin.readLine());
                    for(int i=0;iOutput:
    3
    0 loop out of 3 specified loops
    1 loop out of 3 specified loops
    2 loop out of 3 specified loops
    Please add exactly what kind of stuff you are looking for? Also, feel free to correct if anything wrong done.

    Thanks !
  • spp
    spp
    sookie
    Hi spp,

    Sorry but your problem is not clear to me. Below is my trial of what I understood from the problem specified in above post.

    package myjava;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class MTimesForLoop {
        public static void main(String[] args) throws IOException{
                    BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
                    int M= Integer.parseInt(bin.readLine());
                    for(int i=0;iOutput:


    Please add exactly what kind of stuff you are looking for? Also, feel free to correct if anything wrong done.

    Thanks !
    HI sookie
    I think u dont understand my problem.what problem I am facing is for any value of 'M' total 'M' for loops will execute.
    example:
    let i want to print "Hello world" and M=3
    then the code will be
    for(i=0;i {
    for(j=0;j {
    for(k=0;k {
    printf("Hello world");
    }
    }
    }
    for M=4
    total 4 for loops will be used ie
    for(i=0;i {
    for(j=0;j {
    for(k=0;k {
    for(l=0;l {
    printf("Hello world");
    }
    }
    }
    }
  • pradeep_agrawal
    pradeep_agrawal
    Below is my code for the given problem statement solved using recursion.

    #include "stdio.h"
    
    void function(int m, int n, int* loopIndexes) {
      printf("Hello\n");
    }
    
    
    void mNumberOfLoops(int m, int n, int currentLoop, int* loopIndexes) {
      int i = 0;
    
      if(currentLoop != m) {
        for(i = 0; i < n; i++) {
          loopIndexes[currentLoop] = i;
          mNumberOfLoops(m, n, currentLoop + 1, loopIndexes);
        }
      } else {
        function(m, n, loopIndexes);
      }
    }
    
    
    int main() {
      int m = 0, n = 0;
      int *loopIndexes = NULL;
      int i = 0;
    
      scanf("%d%d", &m, &n);
      if((m < 1) || (n < 1)) {
        printf("Invalid input\n");
        return 0;
      }
    
      loopIndexes = (int*)malloc(sizeof(int)*m);
      for(i = 0; i < m; i++) {
        loopIndexes[i] = 0;
      }
    
      mNumberOfLoops(m, n, 0, loopIndexes);
    
      return 0;
    }
    
    Let me know if you have some query.

    -Pradeep
  • sookie
    sookie
    @spp Oh so you were talking about "nested for loops -m times". Dude if you simply want to print("Hello") M[sup]M[/sup] times then why so much complications with for loops? Are you planning to do something else also in each of your "nested for loop" everytime ?
  • spp
    spp
    sookie
    @spp Oh so you were talking about "nested for loops -m times". Dude if you simply want to print("Hello") M[sup]M[/sup] times then why so much complications with for loops? Are you planning to do something else also in each of your "nested for loop" everytime ?
    yah dude.m writing a programme to find the basic feasible solutionof a given system of equation.
    for that i have to select a m*m matrix from a given m*n matrix and for that i need the use of 'nested for loops'.
    thanks dude for showing interest on this problem.
  • spp
    spp
    pradeep_agrawal
    Below is my code for the given problem statement solved using recursion.

    #include "stdio.h"
    
    void function(int m, int n, int* loopIndexes) {
      printf("Hello\n");
    }
    
    
    void mNumberOfLoops(int m, int n, int currentLoop, int* loopIndexes) {
      int i = 0;
    
      if(currentLoop != m) {
        for(i = 0; i < n; i++) {
          loopIndexes[currentLoop] = i;
          mNumberOfLoops(m, n, currentLoop + 1, loopIndexes);
        }
      } else {
        function(m, n, loopIndexes);
      }
    }
    
    
    int main() {
      int m = 0, n = 0;
      int *loopIndexes = NULL;
      int i = 0;
    
      scanf("%d%d", &m, &n);
      if((m < 1) || (n < 1)) {
        printf("Invalid input\n");
        return 0;
      }
    
      loopIndexes = (int*)malloc(sizeof(int)*m);
      for(i = 0; i < m; i++) {
        loopIndexes[i] = 0;
      }
    
      mNumberOfLoops(m, n, 0, loopIndexes);
    
      return 0;
    }
    
    Let me know if you have some query.

    -Pradeep
    thanks man...

You are reading an archived discussion.

Related Posts

In a programme i created a .c/.cpp file dynamically ie at run time and i want to run that file simultanously. how should i proceed? Its urgent & very important....
Hi guys Can anyone please tell me How to know the VGA(AGP) graphic card or display drivers suppourted or given for my laptop(i.e.,Compaq Presario 2500)
find the limit of the following function using epsilon and delta approach f(x,y) = [2(x)^4 + 3(y)^4]/[(x)^2 + (y)^2] as (x,y) tends to (0,0)
I am final year EEE students,looking for a good project related to electrical engineering.so give me some projects name and ideas
i have been given the responsibility of organising junkyard ๐Ÿ˜...i got a month time to prepare for it...need your ideas as to what all one has to do(i m an...