I have a nested loop which generates ordered pairs, triplets, ... of integers from 0 to a maximum N. -
for ( a[0]=0;a[0]<N;a[0]++)
-
for( a[1]=0;a[1]<a[0];a[1]++)
-
for( a[2]=0;a[2]<a[1];a[2]++)
-
I would like to generalize this for a[n] for given n, or find some more creative way of generating these sets.
Any help is much appreciated.
7 1862
I have a nested loop which generates ordered pairs, triplets, ... of integers from 0 to a maximum N. -
for ( a[0]=0;a[0]<N;a[0]++)
-
for( a[1]=0;a[1]<a[0];a[1]++)
-
for( a[2]=0;a[2]<a[1];a[2]++)
-
I would like to generalize this for a[n] for given n, or find some more creative way of generating these sets.
Any help is much appreciated.
Hi are n and N different values?
Yes they are different sorry for the confusion. N is just the maximum value allowed and n is the number of elements taken at a time 2,3,4... this is what I would like to make general.
Yes they are different sorry for the confusion. N is just the maximum value allowed and n is the number of elements taken at a time 2,3,4... this is what I would like to make general.
OK. here you go -
const int n = 3;
-
int a[n];
-
int N = 100;
-
-
for (int i = 0; i < n; i++)
-
a[i] = N - (i + 1);
Thank you for your reply but that is not quite what I am looking for. I need all possiblities, as produced by this code for n=3 and N=5. If I want n=4 then I need to add an additional loop
for ( a[3]=0;a[3]<a[2];a[3]++)
and having to change this for different n or n>10 i would like to avoid. -
#include<iostream>
-
int main()
-
{
-
const int n=3;
-
int N=5,a[n];
-
-
for ( a[0]=0;a[0]<N;a[0]++)
-
for( a[1]=0;a[1]<a[0];a[1]++)
-
for( a[2]=0;a[2]<a[1];a[2]++)
-
std::cout<<a[0]<<','<<a[1]<<','<<a[2]<<'\n';
-
-
return 0;
-
}
-
Thank you for your reply but that is not quite what I am looking for. I need all possiblities, as produced by this code for n=3 and N=5. If I want n=4 then I need to add an additional loop
for ( a[3]=0;a[3]<a[2];a[3]++)
and having to change this for different n or n>10 i would like to avoid. -
#include<iostream>
-
int main()
-
{
-
const int n=3;
-
int N=5,a[n];
-
-
for ( a[0]=0;a[0]<N;a[0]++)
-
for( a[1]=0;a[1]<a[0];a[1]++)
-
for( a[2]=0;a[2]<a[1];a[2]++)
-
std::cout<<a[0]<<','<<a[1]<<','<<a[2]<<'\n';
-
-
return 0;
-
}
-
You have just added a line of code that completely changes your question
I should have posted this code to begin, I need to make use of the values in each loop, not just the final loop.
Well, I found a solution in a recursive function, the only thing I am not thrilled about is that instructions are now in the recursive function 'loop' after the else. Thanks willakawill for replying. -
#include<iostream>
-
-
void loop(int a[], int n,int i){
-
for (a[i]=0;a[i]<a[i-1];a[i]++){
-
if ((i+1)<n) loop( a, n, i+1);
-
else std::cout<<a[0]<<','<<a[1]<<','<<a[2]<<'\n';
-
}
-
}
-
-
int main()
-
{
-
const int n=3;
-
int N=5,a[n];
-
-
for ( a[0]=0;a[0]<N;a[0]++)
-
loop(a,n,1);
-
-
return 0;
-
}
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: chad |
last post by:
I am writing a program to do some reliability calculations that
require several nested for-loops. However, I believe that as the
models become more complex, the number of required for-loops will...
|
by: mark |
last post by:
My problem is I need to have a "nested" repeater. I have an array which I
load into a hashtable - that part works great. I can setup the second
repeater to work just fine, as long as it's not...
|
by: Martin Schou |
last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C,
which explains why I'm doing an exercise like this.
In the following tripple nested loop:
int digit1 = 1;
int digit2 = 0;...
|
by: Neptune |
last post by:
Hello.
I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"
(Sam's series), and for nested loops, he writes (p116) "It's often
necessary to create a loop even when you are...
|
by: Peter Olcott |
last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html
Why is C# 500% slower than C++ on Nested Loops ???
Will this problem be solved in...
|
by: Javaman59 |
last post by:
Using local declarations within a block often makes code more readable, but
is it less efficient? eg...
void P() {
while (...) {
int i = ...;
bool b = ...;
....
}
}
|
by: Peter Olcott |
last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html
The above link shows that C# is 450% slower on something as simple as a nested
loop....
|
by: =?Utf-8?B?QUEyZTcyRQ==?= |
last post by:
Could someone give me a simple example of nested scope in C#, please?
I've searched Google for this but have not come up with anything that makes
it clear. I am looking at the ECMA guide and...
|
by: Nathan Sokalski |
last post by:
I have several nested For loops, as follows:
For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |