for loop help
Jul 24, 2014 at 2:54pm UTC
I have this for loop that does this:
let say n = 8
and node = 4
n: [1][2][3][4][5][6][7][8]
id: 0 1 2 3 0 1 2 3
1 2 3 4 5 6 7 8 9
id = 0;
while (id < node){
for (i = id + 1; i <= n; i = i + node)
{
//do stuff here
id = i;
}
id+1;
}//end while
and i want it to do this:
n = 8
node= 4
n: [1][2][3][4][5][6][7][8]
id: 0 0 1 1 2 2 3 3
n = 16
node = 4
n: [1][2][3][4][5][6][7][8] ... [13][14][15][16]
id: 0 0 0 0 1 1 1 1 ... 3 3 3 3
n = 8
node= 2
n: [1][2][3][4][5][6][7][8]
id: 0 0 0 0 1 1 1 1
where each id is assign to the top n show in examples
I have this but it only works for the specific scenario n= 8 & node = 4
1 2 3 4
...
b = id + 1
for (i = n-(n-(id+b)); i <= (n-(n-(id+b))+1); i+= 1)
...
Last edited on Jul 24, 2014 at 3:11pm UTC
Jul 25, 2014 at 2:39am UTC
I don't know how to explain it =..=.
So is this what you want?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int n,node,eachnum;
int *id;
cin >> n >> node;
id = new int [n+1];
eachnum = n/node;
for (int i=1;i<=n;i++){
id[i] = (i-1)/eachnum;
}
cout << " n" ;
for (int i=1;i<=n;i++) cout << " " << setw(2) << i;
cout << endl << "id" ;
for (int i=1;i<=n;i++) cout << " " << setw(2) << id[i];
return 0;
}
You just have to know how many of each number you'll assign, after you know it's now easy and have many ways to do. For me I used
id[i] = (i-1)/eachnum;
.(May be you can print 0 for
eachnum and print 1 for
eachnum ,etc.)
Last edited on Jul 25, 2014 at 2:42am UTC
Topic archived. No new replies allowed.