-
C problem
I am new to learning C and was wondering how I can print out the following results using nested if's and only using (1) printf:
1--------- 1
1----------2
1--------- 3
2--------- 1
2--------- 2
2--------- 3
3--------- 1
3 -------- 2
3 -------- 3
(I added ------- to represent spacing)
As you can tell I am VERY new to learning this so I think somebody should know the answer in 2 seconds! :)
You can post results here or to [email protected]
Thanks a ton!!!!!!
-
my homework detector is ringing again ;)
it would be possible using nested if statements and multiple printf's and i can see doing it with only 1 printf and no nested ifs, but nested if's and one printf ill have to think about.....
-
Re
It is for homework but not for a grade.....I have tried and tried but cannot figure it out. Struggling with this one......
-
Here are some "hints". Looking at the data, you have a pattern. On the left side, there is a set of numbers (1...3), and on the right, there is a set of numbers (1...3) for each number in the first set. So, you could use to integers to store the data (one for the current number in the first set, and one for the current number in the second). You could then loop over these values, and using 'if' statements within the loop, to print the current number from the first set, a space, and then the current number from the second set, and then a newline ('\n') character.
I hope this helps. :)
-
code
main()
{
int i=1,j=0;
while(1)
{
if (j==3)
{
i++;
if (i==4)
break;
j=0;
}
j=j+1;
printf("%d %d\n",i,j);
}
}
-
No offense to Mangesh, but his code is probably hard for a n00b to understand. I re-wrote it the more logical way (even though it might not be exactly what shibz wants. At least he'll get the idea on what is going on.
Code:
#include <stdio.h>
int main(void)
{
int i, j;
for (i = 1; i <= 3; i++)
for (j = 1; j <= 3; j++)
printf("%d ------ %d\n", i, j);
return 0;
}
REMINDER: Everybody use code tags
-
use of nested if's
Dear biosx,
Shibz' first sentence :
>I am new to learning C and was wondering how I can print out the following results using nested if's and only using (1) printf:
I wrote this code to implement it using nested if's.
Regards,
Mangesh.