Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I've been learning C language and I am stuck on a pattern program. Here is the pattern:
C#
        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5



I'm new to this site so I don't know if the pattern got correctly pasted here. Assuming it did, here is it's code:

C#
#include <stdio.h>
int main()
{
   int i,space,rows,k=0,count=0,count1=0;
   printf("Enter the number of rows: ");
   scanf("%d",&rows);
   for(i=1;i<=rows;++i)   
   {
       for(space=1;space<=rows-i;++space)
       {
          printf("  ");
          ++count;
        }
        while(k!=2*i-1)
        {
           if (count<=rows-1) 
           {
             printf("%d ",(i+k));
             ++count;
           }
           else
           {
             ++count1;
              printf("%d ", (i+k-2*count1)); 
           }
           ++k;
        }
        count1=count=k=0;
        printf("\n");
    }
    return 0;
}


What I understand in this code is if the outer for loop 1 time, the inner loop will execute (rows-i) times. Then what I don't understand is after printing the blank space, how will the value of count change by ++count. And how will the control flow in this code,'cause there is a while loop too.
What I'm assuming is first inner and outer loop will execute. But if it goes that way how will while loop get executed.

I have done the basic patterns. Now I'm stuck on this. Please help me. I will be really grateful. Thank you :)

What I have tried:

I have tried searching the control flow of loops but I didn't find anything helpful which could have helped me in understanding this problem.
Posted
Updated 28-Feb-16 5:34am
Comments
PIEBALDconsult 28-Feb-16 11:38am    
That's because there's nothing to explain; you may be over-thinking the situation -- "I'm assuming is first inner and outer loop will execute" -- makes it sound like you haven't full grasped the fundamentals of these flow control statements. Probably because you haven't been paying attention.

The best advice I can give is that when trying to do homework, it is best not to try to download someone else's bad code and then try to understand it -- doing such will cripple you. Please start fresh and do it yourself. It is not a difficult task; doing it yourself will likely prove easier than trying to understand that crap you pasted and you will be better for it.

OK, let's rip out the "extras" and just look at the flow control stuff:
C#
for(i=1;i<=rows;++i)
{
    for(space=1;space<=rows-i;++space)
    {
    }
    while(k!=2*i-1)
    {
       if (count<=rows-1)
       {
       }
       else
       {
       }
    }
}
So you have an outer loop:
C#
for(i=1;i<=rows;++i)
{
    ...
}
Which contains two inner loops:
C#
for(space=1;space&lt;=rows-i;++space)
{
}
while(k!=2*i-1)
{
   ...
}
Which are executed one after the other - the first for loop runs, then the while loop runs when the for loop is complete.
Inside the second loop, you have a conditional which means that one side or the other will be executed each time it goes round.
Try it: use the debugger, and put a breakpoint on the first line of the code. When you run your program, it will stop when it hits the breakpoint and let you decide what to do next. Simply stepping each lien through will show you exactly what is happening!
(I can't tell you how to set a breakpoint or step a line, because that will depend on which development IDE you are using - google for "breakpoint" and the name of your IDE and you should find it pretty easily).
 
Share this answer
 
Comments
Patrice T 28-Feb-16 11:47am    
+5
Arthur V. Ratz 20-Mar-16 11:30am    
+5. Good solution.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see what the code is really doing.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900