-4

I have an exercise in Objective-C making use of for, if-else and printf to print a Zig-Zag like this in the console:

Please see image:

enter image description here

I have tried a code with C programming and print a triangle then try to edit this one, but can't do anything more to get my Zig-Zag.


I have solve my problems already. Thank you guys so much.

 for (int i = 0; i < 5; i++) {
    for(int j = 1; j<= 21; j++ ){
        if(j<=9){
            if(j - i == 5 || j+ i == 5){
                printf("*") ;
            }else{
                printf(" ");
            }
        }else{
            if(j+i == 13 || j - i == 13 || j + i == 21){
                printf("*") ;
            }
            else{
                printf(" ");
            }
        }
    }
    printf("\n");
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
sunsunai
  • 161
  • 4
  • 11
  • Its simple formatting using logic in looping constructs. – vish4071 Aug 14 '15 at 06:42
  • You need a tutor, not a question on Stack Overflow. This site isn't designed for the kind of back-and-forth personal help that you require. If you can't find anyone to help you in person, have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). – jscs Aug 14 '15 at 06:48
  • Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour) and read [How to Ask](http://stackoverflow.com/help/how-to-ask) to learn what we expect from questions here. Please be aware that we do not provide _from-scratch_ coding service. Please show us what you've tried already, how it failed and we might be able to help.:-) – Sourav Ghosh Aug 14 '15 at 06:53

2 Answers2

0

OK, attempt 2 based on your updated question. Let's start with the basic structure. Set some consts for the width and height - we will refer to these in multiple places, so consts make for nice easy changes - and create a loop with basic logic to start to get the display you want.

const int numRows = 5;
const int numCols = 21;
for (int row = 0; row < numRows; ++row)
{
    for (int col = 0; col < numCols; ++col)
    {
        if (col == row)
        {
            printf("X");
        }
        else
        {
            printf(" ");
        }
    }
    printf("\n");
}

This gives us:

X                    
 X                   
  X                  
   X                 
    X                

which is the wrong way up and only gives us one "downstroke", but it is a good start. Let's use the modulus function to make the pattern repeat. The repeat happens on the 9th character, which suggests we need to use ((numRows - 1) * 2) for our modulus.

const int modulusVal = ((numRows - 1) * 2);
for (int row = 0; row < numRows; ++row)
{
    for (int col = 0; col < numCols; ++col)
    {
        int modCol = (col % modulusVal);
        if (modCol == row)
        {
            printf("X");
        }
        else
        {
            printf(" ");
        }
    }
    printf("\n");
}

So now we get:

X       X       X    
 X       X       X   
  X       X       X  
   X       X       X 
    X       X       X

which is starting to get a lot closer to what we want. So, on to the upstrokes. The downstrokes are displayed when modCol is in the range of row, which is 0-4. When modCol is past this range, we bring it back into range by subtracting the number of rows from it.

Then we "invert" it by subtracting it from the highest value that row can be, which is numRows - 1. This means that when modCol is 0 it will become 4, when it is 1 it will become 3, when it is 2 it will be unchanged.

for (int row = 0; row < numRows; ++row)
{
    for (int col = 0; col < numCols; ++col)
    {
        int modCol = (col % modulusVal);
        if (modCol >= numRows)
        {
            modCol -= numRows;
            modCol = ((numRows - 1) - modCol);
        }

        if (modCol == row)
        {
            printf("X");
        }
        else
        {
            printf(" ");
        }
    }
    printf("\n");
}

Now we get:

X       X       X    
 X       X       X   
  X    X  X    X  X  
   X  X    X  X    X 
    XX      XX      X

Close, but no cigar. Column 5 is being treated as column 0, but we need it to be treated as column 1. Make this change to fix it:

modCol = ((numRows - 1) - (modCol + 1));

Output:
X       X       X    
 X     X X     X X   
  X   X   X   X   X  
   X X     X X     X 
    X       X       X

This has the pattern we want, but starts at the top left when we want the bottom left. Easy fix. Invert modCol after it has been calculated in the same we we invert it when it is greater/equal than/to numRows.

for (int row = 0; row < numRows; ++row)
{
    for (int col = 0; col < numCols; ++col)
    {
        int modCol = (col % modulusVal);
        if (modCol >= numRows)
        {
            modCol -= numRows;
            modCol = ((numRows - 1) - (modCol + 1));
        }
        modCol = ((numRows - 1) - modCol);

        if (modCol == row)
        {
            printf("X");
        }
        else
        {
            printf(" ");
        }
    }
    printf("\n");
}

Finally we get the output we want:

    X       X       X
   X X     X X     X 
  X   X   X   X   X  
 X     X X     X X   
X       X       X    

I hope this helps and is reasonably easy to understand.

Mick Waites
  • 147
  • 13
-1

I'm not familiar with console programming using Objectionable C and you haven't explained exactly what you've tried, what has worked and what has not.

Assuming your problem is the code to choose the appropriate position to display the '+' character and not displaying text at a particular location on the console, here's what I would do:

int currentY = MIN_Y;
int incrementY = 1;
for (int currentX = MIN_X; currentX <= MAX_X; ++currentX)
{
    SetCursorPosition(currentX, currentY);
    printf("+");

    currentY += incrementY;
    if (currentY == MAX_Y)
    {
        incrementY = -1;
    }
    else if (currentY == MIN_Y)
    {
        incrementY = 1;
    }
}

This assumes you have a function for SetCursor() and consts/defines for MIN/MAX X/Y.

The 'if' statement could be optimised to:

if (currentY == MAX_Y || currentY == MIN_Y)
{
    incrementY -= incrementY;
}

but you required the use of the 'else' statement. Hopefully this code is pretty self explanatory and of some use to you.

Mick Waites
  • 147
  • 13
  • Mick you should remove this `Objectionable C` as OP changed tag to `C` . And FYI- Its `Objective C`. Cheers !! :) – ameyCU Aug 14 '15 at 08:25
  • @MartinJames - Yeah, someone used the term to me once and I lol'd, so now I use it whenever I can. Just cannot get to grips with that language at all. – Mick Waites Aug 14 '15 at 09:52