How to write programme for 'M' for loops dynamically
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.
Member • Sep 6, 2009
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:
3Please add exactly what kind of stuff you are looking for? Also, feel free to correct if anything wrong done.
0 loop out of 3 specified loops
1 loop out of 3 specified loops
2 loop out of 3 specified loops
Member • Sep 6, 2009
HI sookiesookieHi 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 !
Member • Sep 6, 2009
#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.
Member • Sep 7, 2009
Member • Sep 7, 2009
yah dude.m writing a programme to find the basic feasible solutionof a given system of equation.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 ?
Member • Sep 10, 2009
thanks man...pradeep_agrawalBelow 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