How: User [input] defined number of itterations

S

Stef

Hello,

I have a question that maybe some of you could answer because I don't
know how to do this:

I want the user to be able to define how many itterations or better
loops should spawn... The problem is that they have to be executed at
the same time..

My program looks like:

#include <stdio.h>

int main(int argc, char *argv[]) {
int i, j, k, l;

for(i = 0; i <= 9; i++)
for(j = 0; j <= 9; j++)
for(k = 0; k <= 9; k++)
for(l = 0; l <= 9; l++)
printf("%d%d%d%d\n", i, j, k, l);

return 0;
}

But I want the user to be able to define the digit width...

As you can see I am a beginner who's a bit stuck..

Thank you for the answers....

Stef.
 
T

Tim Rentsch

Stef said:
Hello,

I have a question that maybe some of you could answer because I don't
know how to do this:

I want the user to be able to define how many itterations or better
loops should spawn... The problem is that they have to be executed at
the same time..

My program looks like:

#include <stdio.h>

int main(int argc, char *argv[]) {
int i, j, k, l;

for(i = 0; i <= 9; i++)
for(j = 0; j <= 9; j++)
for(k = 0; k <= 9; k++)
for(l = 0; l <= 9; l++)
printf("%d%d%d%d\n", i, j, k, l);

return 0;
}

But I want the user to be able to define the digit width...

The imporant idea is to put the different loop
variables into an array, and use array techniques
to increment them. The code below illustrates
this. There's a slight trick in that the
characters '0' through '9' are used rather than
numbers, but the idea is the same.

void
loop_10_to_the( unsigned nesting_depth ){
unsigned i;
char *v;

/* v[0] == i_0, v[1] == i_1, v[2] == i_2, ... */

if( nesting_depth == 0 ) return; /* nothing to do */

v = malloc( nesting_depth + 1);
if( !v ) return; /* no memory, give up */

for( i = 0; i < nesting_depth; i++ ){
v = '0';
}
v = 0;

do {
printf( "%s\n", v );

i = nesting_depth;
while( i > 0 && v[i-1]++ == '9' ){
v[i-1] = '0'; /* increment of '9' becomes '0' */
i--; /* and move to next lower counter */
}
} while( i > 0 ); /* when i reaches 0 we're out of loops */

free( v );
}
 
C

Christopher Benson-Manica

Tim Rentsch said:
The imporant idea is to put the different loop
variables into an array, and use array techniques
to increment them. The code below illustrates
this. There's a slight trick in that the
characters '0' through '9' are used rather than
numbers, but the idea is the same.

Your version is probably superior to mine, but I came up with this,
which bears many similarities to what you posted.

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

int main( int argc, char *argv[] )
{
int count, idx;
char *values;
assert( argc >= 2 );
count=strtoul( argv[1], NULL, 10 ); /* Assume valid input */
assert( (values=malloc(count+1)) != NULL );
if( count <= 0 ) {
return EXIT_SUCCESS;
}
for( idx=0; idx < count; idx++ ) {
values[idx]='0';
}
values[count]=0;
while( 1 ) {
idx=count-1;
while( idx >= 0 && (values[idx]='0'+(values[idx]+1-'0')%10) == '0' ) {
idx--;
}
if( idx < 0 ) {
break;
}
printf( "%s\n", values );
}
free( values );
return EXIT_SUCCESS;
}
 
S

Stef

Oww... thank you for sharing the code and the complete answer.
[[[Sorry for my top-quotation, but the message got a bit long the
other way]]]

The code you listed worked fine, .. I will examine your solution and
learn from it..

Again, Thankx a lot !!!

Stf


Stef said:
Hello,

I have a question that maybe some of you could answer because I don't
know how to do this:

I want the user to be able to define how many itterations or better
loops should spawn... The problem is that they have to be executed at
the same time..

My program looks like:

#include <stdio.h>

int main(int argc, char *argv[]) {
int i, j, k, l;

for(i = 0; i <= 9; i++)
for(j = 0; j <= 9; j++)
for(k = 0; k <= 9; k++)
for(l = 0; l <= 9; l++)
printf("%d%d%d%d\n", i, j, k, l);

return 0;
}

But I want the user to be able to define the digit width...

The imporant idea is to put the different loop
variables into an array, and use array techniques
to increment them. The code below illustrates
this. There's a slight trick in that the
characters '0' through '9' are used rather than
numbers, but the idea is the same.

void
loop_10_to_the( unsigned nesting_depth ){
unsigned i;
char *v;

/* v[0] == i_0, v[1] == i_1, v[2] == i_2, ... */

if( nesting_depth == 0 ) return; /* nothing to do */

v = malloc( nesting_depth + 1);
if( !v ) return; /* no memory, give up */

for( i = 0; i < nesting_depth; i++ ){
v = '0';
}
v = 0;

do {
printf( "%s\n", v );

i = nesting_depth;
while( i > 0 && v[i-1]++ == '9' ){
v[i-1] = '0'; /* increment of '9' becomes '0' */
i--; /* and move to next lower counter */
}
} while( i > 0 ); /* when i reaches 0 we're out of loops */

free( v );
}
 
K

Keith Thompson

Stef said:
Oww... thank you for sharing the code and the complete answer.
[[[Sorry for my top-quotation, but the message got a bit long the
other way]]]
[...]

Then you should trim the portions of the previous message that aren't
relevant to your response. It wasn't necessary to quote all the code.
 
T

Tim Rentsch

Stef said:
Oww... thank you for sharing the code and the complete answer.
[[[Sorry for my top-quotation, but the message got a bit long the
other way]]]

The code you listed worked fine, .. I will examine your solution and
learn from it..

Again, Thankx a lot !!!

Stf

You're welcome.

If you want to try an exercise to test your new found understanding,
you might try this. Expand the function to take two additional array
parameters:

void
loop_nestedly( unsigned depth, int start[], int limit[] ){
unsigned i;
int *v;

v = malloc( depth * sizeof *v );

/* now we want the effect of
*
* for( v[0] = start[0]; v[0] < limit[0]; v[0]++ ){
* for( v[1] = start[1]; v[1] < limit[1]; v[1]++ ){
* for( v[2] = start[2]; v[2] < limit[2]; v[2]++ ){
* ...
*
*/

/* YOUR CODE HERE */

free( v );
}

I expect you'll be able to generalize the simpler function to this
more flexible pattern.

After you've coded that up, see what happens if one or more of the
start/limit pairs means no iterations should be done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top