Pattern mountain (progg. funda)

Nov 26, 2020 at 4:41am
whats wrong with my code??
#include
using namespace std;

int main() {
int n;
cin>>n;

int i,j;

for(i=1;i<=n;i++)
{

for(j=1;j<=(2*n-1);j++)
{

if(j<=i)
cout<<j+" ";

else if((i+j)>=2*n)
cout<<(2*n-j)+" ";


else
cout<<" ";
}
cout<<endl;

} https://krogerfeedback.bid/

return 0;
}




Last edited on Nov 28, 2020 at 4:49am
Nov 26, 2020 at 4:47am
The first thing I see wrong is that you have no <filename> after your #include directive.

Please edit your post to use code formatting:

[code] { your source code here } [/code]
Last edited on Nov 26, 2020 at 4:47am
Nov 26, 2020 at 6:07am
> whats wrong with my code??
Well you didn't show us
a) what you actually want it to do
b) what you currently get.

Nov 26, 2020 at 7:17am
OP's code, formatted and put into code tags, with a couple of error messages attached:

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
using namespace std;

int main() {
  int n;
  cin >> n;

  int i, j;

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= (2 * n - 1); j++) {
      if (j <= i)
        cout << j + " ";

      else if ((i + j) >= 2 * n)
        cout << (2 * n - j) + " ";

      else
        cout << " ";
    }
    cout << endl;
  }

  return 0;
}
1:9: error: expected "FILENAME" or <FILENAME>
#include
        ^
13:19: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
        cout << j + " ";
                ~~^~~~~
13:19: note: use array indexing to silence this warning
16:29: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
        cout << (2 * n - j) + " ";
                ~~~~~~~~~~~~^~~~~
16:29: note: use array indexing to silence this warning


-Albatross
Nov 26, 2020 at 10:25am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main() {
	int n {};
	cin >> n;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= (2 * n - 1); j++) {
			if (j <= i)
				cout << j << " ";
			else
				if ((i + j) >= 2 * n)
					cout << (2 * n - j) << " ";
				else
					cout << " ";
		}
		cout << endl;
	}

	return 0;
}



10
1                  1
1 2                2 1
1 2 3              3 2 1
1 2 3 4            4 3 2 1
1 2 3 4 5          5 4 3 2 1
1 2 3 4 5 6        6 5 4 3 2 1
1 2 3 4 5 6 7      7 6 5 4 3 2 1
1 2 3 4 5 6 7 8    8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9  9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1

Last edited on Nov 26, 2020 at 10:27am
Topic archived. No new replies allowed.