can someone help me

(sample)
size: 5
First number: 5
5*4*3*2*1*
__5*4*3*2*
____5*4*3*
______5*4*
________5*
(I typed wrong,sorry.)
(Don't care the _ , it's nothing.)
This is my work and I have no idea how to do it like the sample.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int i, j, size;
	char firstnumber;
	cout << "Size: ";
	cin >> size;
	cout << "First number: ";
	cin >> firstnumber;
	for (i = size; i >= 1; i--)
	{
		for (j = 1; j <= size - i; j++)
		{
			cout << " ";
		}
		for (j = 1; j <= i; j++)
			cout << j;
		cout << endl;
	}

    return 0;
}
Last edited on
Hello ayanokoji,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy
Hello ayanokoji,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

int main()
{
    int size{};  // <--- ALWAYS a good idea to initialize your variables.
    char firstnumber{};  // <--- Why is this a "char" when the name suggests an "int"?

    std::cout << "Size: ";  // <--- Size of what?
    std::cin >> size;

    std::cout << "First number: ";
    std::cin >> firstnumber;

    for (int i = size; i >= 1; i--)  // <--- Better to define the itterators' type in the for loop. This keeps it local to the for loop.
    {
        for (int j = 1; j <= size - i; j++)
        {
            std::cout << " ";
        }

        for (int j = 1; j <= i; j++)
            std::cout << j;

        std::cout << '\n';
    }


    // <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

That covers some of the problems.

After that it makes no sense at all. Not that I have tried to run the program, but it looks like the for loops are an attempt to get the right output.

Note: It is always best to post a complete program that can be compiled and run. Do not make people guess at what you might have done. Also in the missing code there may be something that you do not see yet.

The for loops work the way that you want. Now they need something to work with.

Andy
this is more crazy logic than c++ so I will give you a starting point. I can't make sense of 'size' so I left it off but it should be easy to adapt the logic below to your problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  int n{5};
  for(int i = 0; i < n; i++)
  {
	for(int j = i; j; j--)
        cout <<"_";
	for(int j = n; j>i; j--)	
       cout << j <<"*";
   cout << endl;		
	  
  }
}


1
2
3
4
5
6
5*4*3*2*1*
_5*4*3*2*
__5*4*3*
___5*4*
____5*


if you want the right side aligned you need to add TWO '_' for each number 0-9 and three for 10-99 etc (log10+1 can get you the amount) because you need them for each digit and the * symbol. Its not clear if that was the goal or what.

there is likely a recursive answer too.
Last edited on
@jonnin,

My understanding is the "size" is the amount of numbers on the first line.

With "size = 4" and "firstnumber = 5" the first line would be:
5 4 3 2
Then it would follow

5 4 3 2
  4 3 2
    3 2
      2


The (_) is just to show that it is a space.

Had that working until I tried 10 for "firstnumber" then the spacing was off.

Andy
Last edited on
the size is rows,
there have more sample
row: 4
First number: 9
9*8*7*6*
__9*8*7*
____9*8*
______9*
Yes, the (_) is space

but I don't know how to do this, I want to type the row and first number, and show the triangular pattern. The triangular pattern must be inverted
Last edited on
For first number 0 < fn < 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>

int main()
{
	int sz {}, fst {};

	std::cout << "Enter size: ";
	std::cin >> sz;

	std::cout << "Enter first number: ";
	std::cin >> fst;

	for (int r = 0; r < sz; ++r) {
		std::cout << std::setw(r * 2) << std::setfill(' ') << "";
		for (int n = fst; n > fst - sz + r; --n)
			std::cout << n << '*';

		std::cout << '\n';
	}
}



Enter size: 4
Enter first number: 9
9*8*7*6*
  9*8*7*
    9*8*
      9*

Enter size: 5
Enter first number: 5
5*4*3*2*1*
  5*4*3*2*
    5*4*3*
      5*4*
        5*

Last edited on
Hello ayanokoji,

Thank you for fixing your OP, (Original Post), That helps.

I would say that if you are writing the code to benefit the compiler then there is to much white space in your code. If you want someone to read and understand you code then you need some blank lines.

It took me a little while, but I came up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

int main()
{
    int numOfRows{};
    int firstNumber{};  // <--- Why is this a "char" when the name suggests an "int"?

    std::cout << "Number of rows?: ";  // <--- Comment these 2 lines and uncomment for a complete triangle.
    std::cin >> numOfRows;

    std::cout << "First number (< 10): ";
    std::cin >> firstNumber;

    //numOfRows = firstNumber;

    for (int row = 0; row < numOfRows; row++)  // <--- Better to define the itterators' type in the for loop. This keeps it local to the for loop.
    {
        for (int colSpace = row; colSpace; colSpace--, std::cout << ' ')
        {
            std::cout << " ";
        }

        for (int colNum = firstNumber; colNum > row; colNum--)
        {
            std::cout << colNum << ' ';
        }

        std::cout << '\n';
    }


    // <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

I want you to notice how the blank lines and a good variable make the code easier to read and follow.

With a variable like like "numOfRows". You do not have to use it. It is just an idea of what could be done. "numRows" would work just as well. And do not be afraid to use long variable names. The point is to use something that makes it easy to understand.

When I run the above program I get this:

Number of rows?: 5
First number (< 10): 8
8 7 6 5 4 3 2 1
  8 7 6 5 4 3 2
    8 7 6 5 4 3
      8 7 6 5 4
        8 7 6 5


 Press Enter to continue:


Line 2, the end part, may appear strange to you and I will admit that for now I do not understand how it works, but it does. I think it is because that the "cout" is done after the "colSpace--", so it starts the next line with a space B4 anything else is added to the next line.

I also that when "numOfRows" is less than "firstNumber" you get the above output. When I comment lines comment out lines 11 and 2 and uncomment line 17 It does make a proper triangle.

In you example output you are limiting the number of columns to be the same as the number of rows. It will take a little work, but you can achieve what you want.

Andy

P.S., Excuse the header files. They are just something I consider as standard for a program and since you did not originally provide them I used what I had. Just forgot to delete what was not needed.
Hello ayanokoji,

It took trying different things in the last for loop, but I did manage to get this:


 8*7*6*5
   8*7*6
     8*7
       8


 Press Enter to continue:


I thing that this is what you are looking for. If you need the (*) at the end that is easy to fix.

Andy
Oh I get it now, thx a lot.

I just learn c++.
Also, English isn’t my first language, so please excuse any mistakes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
	int sz{}, fn{};

	cout << "Size: ";
	cin >> sz;

	cout << "First number: ";
	cin >> fn;

	for (int i = 0; i < sz; i++)
	{
		for (int j =i; j; j--)
			cout << "  ";

		for (int j= fn; j>= fn - sz+i;j--)
			cout << j <<"*";

		cout << endl;
	}
}


I tried this why I can't display a triangular pattern, also I just wanted to use <iostream>.

This is the output.
Size: 4
First number: 9
9*8*7*6*5*
  9*8*7*6*
    9*8*7*
      9*8*

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
   int rows, n;
   cout << "Input rows and first number (0 < rows <= n < 10): ";   cin >> rows >> n;
   for ( int r = 0; r < rows; r++ )
   {
      for ( int i = 0; i < r           ; i++ ) cout << "  ";
      for ( int i = n; i > n - rows + r; i-- ) cout << i << '*';
      cout << '\n';
   }
}


Input rows and first number (0 < rows <= n < 10): 4 9
9*8*7*6*
  9*8*7*
    9*8*
      9*
Thank you.
Topic archived. No new replies allowed.