Just started programming (generally and in C++) and wanted to make a program that would calculate every permutation of numbers 1 and 9 (i.e. 1,2 1,3 1,4 ....). Ive come up with the following code :
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int x [2]={0,0};
int g=9;
while (g == 9){
for (int z=0;z <=8;z++){ // 2nd element increases by 1 everytime
x[1]= x[1]+1;
if (x[1]==9){ //when 2nd element reaches 9:
// first element increases by 1
x[0] = x[0]+1; // counter in z reset to 0
z=0;
x[1] = 0; // second element of array restarts at 0
}
if (x[0] > 9){ // at the point when the first element becomes greater than 9:
g=10; // g = 10 and so the loop stops
}
}
cout << x[0] << x[1] << endl;
}
}
All I get is:
18
28
38
48
58
68
78
88
98
108
I feel my code is quite close to working (but then I'm a complete beginner at this). Any help most appreciated!


Sign In
Create Account

Back to top









