CrazyEngineers
  • How to write programme for 'M' for loops dynamically

    spp

    spp

    @spp-8ADGRN
    Updated: Oct 26, 2024
    Views: 953
    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<n;i++)
    {
    for(j=0;j<n;j++)
    {
    process();
    }
    }
    can anyone help me out to solve this problem.
    Its very urgent & necessery for me.
    so please help me out.
    Thank u.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sookie

    MemberSep 6, 2009

    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;i<M;i++){
                            System.out.println(i+" loop out of "+M+" specified loops");
                            //put anything that you want to make to execute 'M' times
     
                    }
        }
    }
    
    Output:
    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 !
    Are you sure? This action cannot be undone.
    Cancel
  • spp

    MemberSep 6, 2009

    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;i<M;i++){
                            System.out.println(i+" loop out of "+M+" specified loops");
                            //put anything that you want to make to execute 'M' times
     
                    }
        }
    }
    
    Output:


    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<m;i++)
    {
    for(j=0;j<m;j++)
    {
    for(k=0;k<m;k++)
    {
    printf("Hello world");
    }
    }
    }
    for M=4
    total 4 for loops will be used ie
    for(i=0;i<m;i++)
    {
    for(j=0;j<m;j++)
    {
    for(k=0;k<m;k++)
    {
    for(l=0;l<m;l++)
    {
    printf("Hello world");
    }
    }
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberSep 6, 2009

    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
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberSep 7, 2009

    @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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • spp

    MemberSep 7, 2009

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • spp

    MemberSep 10, 2009

    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...
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register