Hello,

I have a school assignment that i really need help with. I am supposed to create an X shape with asterisks using for loops. I am terrible at for loops so i was wondering if you guys could help me out a little in figuring out the best solutions. I'd really appreciate it.

This is the code i have so far. I am able to get one diagonal side but not the other.

#include <iostream>
using namespace std;

int main()
{
    int number;


    cout<< "Enter a number: ";
    cin>> number;

    while (number< 2 || number% 2 == 0)
    {
        cout << "Invalid response. Enter an odd number greater than or equal to 2:";
        cin>> number;
    }
    for( int lineNum=0; lineNum<number; lineNum++)
        {
            cout <<"*";
            for (int i=0; i<number-lineNum; i++)
                cout<<" ";
            cout<<endl;

            for (int x= 0; x<= lineNum; x++)
                cout<< " ";

        }

    cout<< endl;
    system( "Pause");
    return 0;

}

thanks again

So, the exercise is meant to get you to problem solve. Think about HOW you would go about doing this. What is the process?

What is the minimum number of columns you must have? Obviously it can't be 1 or 2. This about what it would look like to make an X with 3 columns. How tall can you make that? At what point would you add a middle column * to make it an X?

You also need to consider how a prompt will print out data.

As you type, you see letters go from left to right (unless you are using a right to left language. Either way, you can just reverse the process for right to left..)

At the end of each line, you get a newline break. You have now found a repeated process for your for loop.

For each line, you are going to figure out how wide your X will be (or, you will have that as a parameter passed in).

Based on the width of your X, you will need to go back to your original consideration of colums. What will your X be if you had 4 columns? What makes it different? How do you set that difference? Is there a pattern you can exploit?

Something else that will help (which is where the concept of test driven development comes) is to see the results you CAN have, and code to make those... so, I will give you a few ideas for output and maybe you can visually see what it being asked.

3x3

* *
 *
* *

3 x 5

* *
* *
 *
* *
* *

3 x 7

* *
* *
* *
 *
* *
* * 
* *

4 x 4

*  *
 **
 **
*  * (so, on evens you may have to double up the mid section)

6 x 4

*    *
  **
  **
*    *

Ok, enough examples. Break down what is happensing (I will use the 6 x 4 for this)
On the first row, we have our start points.
Row 1: Pos 1 and Pos 6 + newline
Row 2: Pos 3 and Pos 4 + newline (or, extra spaces if you prefer then newline)
Row 3: Pos 3 and Pos 4 + newline
Row 4: Pos 1 and Pos 6 (and newline if you prefer).

Your for loop will basically run through each "line" and print a * where it's needed based on your dimensions. You will have some math involved to figure out how to break your X up into a nice piece of ASCII art, but I will leave that little nugget up to you :)

  • I will give the hint that if your height is odd you need a single mid-row, if it's even you will need a double mid-row. (Mod) height % 2 == 0 is your friend!

I hope that helps :-D

Ryan

Basically change your thinking. Instead of thinking an 'X' think of a 'V' with an inverted 'V' underneath. By thinking like this you can easily see that whenever you print an * you want to print a corresponding one farther down the line. Once you accomplish this, you want to basically do the same in reverse to print the inverted 'V'. I did some code to get you started. This will print a 'V' to the screen. I took out the restriction for odd numbers. This works fine for any positive integer. With some extra tinkering you won't need any double rows, you can easily have one * in the center no matter whether your rows is odd or even. For instance with the input number as 5 you can get,

 *       *
  *     *
   *   *
    * *
     *
    * *
   *   *
  *     *
 *       *



#include <iostream>
using namespace std;
int main()
{
    int number;
    cout<< "Enter a number: ";
    cin>> number;
    int x = 0;
    for( int lineNum=0; lineNum<number; lineNum++)
    {

        for (int i=0; i<=lineNum; i++)
            cout<<" ";
        cout <<"*";
        for (x= (number-lineNum) *2; x>1; x--)
            cout<< " ";
        cout <<"*" << endl;
    }
    system("Pause");
    return 0;
}

thanks so much for replying,

@tinstaafl I see what youre saying but i think I may have been unclear about how it should look.
if i were to enter 5, then it should be two diagonal lines with 5 asterisks each.

    *   *
     * *
      *
     * *
    *   *

Add this after the cin:

    if(number % 2 != 0)
    {
        number = ((int)(number/2))+1;
    }
    else
    {
        number /= 2;
    }

With this you still don't have to worry about odd or even 6 produces the same result as 5

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.