473,382 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How: User [input] defined number of itterations

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.
Nov 15 '05 #1
5 2046
Stef <St*****@sirtaflaie.gr> writes:
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[i] = '0';
}
v[i] = 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 );
}
Nov 15 '05 #2
Tim Rentsch <tx*@alumnus.caltech.edu> wrote:
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;
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3
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
On 13 Sep 2005 13:03:20 -0700, Tim Rentsch <tx*@alumnus.caltech.edu>
wrote:
Stef <St*****@sirtaflaie.gr> writes:
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[i] = '0';
}
v[i] = 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 );
}


Nov 15 '05 #4
Stef <St*****@sirtaflie.gr> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5
Stef <St*****@sirtaflie.gr> writes:
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[i]/limit[i] pairs means no iterations should be done.
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
4
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
10
by: Kathy Burke | last post by:
HI. in asp.net app, I have an xmlDocument that I transform to the client html. Using xsl I create a few textboxes to capture user input. Each of these are related to <data> elements in the xmlDoc....
51
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
4
by: c676228 | last post by:
Hi everyone, I need to write a insruance program which needs to collect multiple people information, The information for each person includes name, email, address, phone, dob etc. The DOB data...
14
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num;...
39
by: emre esirik(hacettepe computer science and enginee | last post by:
int n_mines; printf("How many mines do you want in the minefield?"); scanf("%d", &n_mines); while(n_mines>100 || n_mines<0) { printf("Please only enter between 1 and 100"); scanf("%d",...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.