ok i tried for 2 variables but i am getting another problem of printing alternate 0's and 1's :/
ok i tried for 2 variables but i am getting another problem of printing alternate 0's and 1's :/
this is the code. it generates all zeros. But i want to generate truth table of 2 bits.Code:#include<stdio.h> int main(void) { int a,b,i; for(i=1; i<=4; i++) { for(a=0; a<=0 ; ++a) { printf("%d",a); } for(b=0; b<=0; b++) { printf("%d",b); } printf("\n"); } return 0; }
How many times will the body be executed? Or asked another way: Is it possible that a gets another value than 0?Code:for(a=0; a<=0 ; ++a) { printf("%d",a); }
Bye, Andreas
I have modified my code by using compound termination condition
I am getting 001 in first line. But i want 00 in first line, 01 in second, 10 in third, 11 in fourth.Code:#include<stdio.h> int main(void) { int a,b,i; for(i=1; i<=4; i++) { for(a=0; a<=0 ; ++a) { printf("%d",a); } for(b=0; b<=1 && !(i%2==0); b++) { printf("%d",b); } printf("\n"); } return 0; }
"Nested" means one inside the other.
This is NOT nested:
This IS nested:Code:for (a=0; a<2; a++) { } for (b=0; b<2; b++) { }
And get rid of the 'i' loop. It is not necessary.Code:for (a=0; a<2; a++) { for (b=0; b<2; b++) { // do printing here } }
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
I think you are on the wrong track so step back.
Write one simple for-loop which will print the following two lines:
Use two variables (one for each column) but notice that only one changes its value. Additionally use only one printf()-call for each line.Code:0 0 0 1
And forget the outermost for-loop because you won't need it.
Bye, Andreas
That can't be the solution, because the OP stated:
Six integer variables - with the previously suggested six nested loops (required!), with one printf("%d%d...\n",i1,i2...); line of code, inside the innermost for loop.
Yeah it is mandatory to use 6 nested loop
Obviously the variables will be assigned to 0 at first, and obviously they will stop at less than 2.
Seems right to me.
I would start with a program that will print just a single 0 on one line and then a single 1 on one line.
Then once that works, do the same again, but the first time prepend a zero and the second time prepend a 1. This will give you 00, 01, 10, and 11 on each of four lines.
Then once that works, do the same again, prepending a zero the first time and a 1 the second time. etc...
The only other hint I will add for now is that it is quite rare for for loop conditions to use <= (less-than-or- equal). They usually use < (less-than).
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
azeem if you have solve this question.kindly guide me too
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.