#include<stdio.h>
int main() //in c++ main returns an int
{
int i = 1;
int a=0,
b=0,
c=0;
for ( a = 1; a <= 4; a++ )
{
for ( b = 1; b <= 5; b++ )
{
for ( c = 1; c <= 6; c++ )
{
if ( a != b && b != c && c != a )
{ //Missing brackets
if ( c > b && b > a )
{
printf("%d %d %d %d\n", i, a, b, c);
i = i + 1;
}
}
}
}
i = i + 1; //what is the purpose of this?
}
return (0); //missing return statement
}
Well a couple of things, first you're writing in C, to use C++ you #include <iostream> and use std::cout instead of pringf(). Also, the main function returns an int.
Second I have no Idea what you're trying to do with those for loops. About those, why initialise your a, b and c ints and then change their values in your for statement? You could as well have left them at 0 for (a; a <5; a++){}
I don't know if it's the result of you not using code tags, if it is ignore my following remark. Your code could use some spacing, especially between your brackets for ( a = 1; a <= 4; a++ ) reads a lot better than for(a=1;a<=4;a++) for instance :)
is this C or C++?
if its C, you need to add a file*, open it, and write to it. you can use fprintf instead of printf, it has an extra arg for the file pointer.. my memory is rusty you can google it but roughly
FILE * fp;
fp = fopen("filename", options);
…
fprintf(fp, … etc);
you can also redirect this kind of program to a file without the effort:
in your console where you run the program, and it works on dos and unix the same way, just run the program and add >filename to the end of the console command.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
constint NMAX = 6;
int i = 0;
// Choose ONE of the following
// ofstream out( "output.txt" ); // for file
ostream &out = cout; // for screen
for ( int a = 1; a <= NMAX - 2; a++ )
{
for ( int b = a + 1; b <= NMAX - 1; b++ )
{
for ( int c = b + 1; c <= NMAX; c++ )
{
int n = 100 * a + 10 * b + c;
i++; // Largest i should be 6C3 = 20
out << i << ": " << n << '\n';
}
}
}
}
When I const int NMAX = 12 the result is wrong
after 1 2 9 it sould be 1 2 10 but it print 1 3 0
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int NMAX = 6;
int i = 0;
// Choose ONE of the following
// ofstream out( "output.txt" ); // for file
ostream &out = cout; // for screen
for ( int a = 1; a <= NMAX - 2; a++ )
{
for ( int b = a + 1; b <= NMAX - 1; b++ )
{
for ( int c = b + 1; c <= NMAX; c++ )
{
int n = 100 * a + 10 * b + c;
i++; // Largest i should be 6C3 = 20
out << i << ": " << n << '\n';
}
}
}
}
Thank you for your help I want Upper limit but I cant understand what you mean (just out put a,b,c as sepaerate variables rather than combining them into n.)
I am not good in c++
For example
const int NMAX = 6; or 7 or 12 or 20
can you do instead of const int NMAX = 6 to const int NMAX = 12 or a bigger number
thanks