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.
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
-
sookieHi 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
Output:
3
Please 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
Thanks ! -
spp
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
Output:
Please add exactly what kind of stuff you are looking for? Also, feel free to correct if anything wrong done.
Thanks !
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_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 -
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
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 ?
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
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
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...