« first day (120 days earlier)      last day (21 days later) » 

10:33 AM
@JohnRennie Hi
#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++){
for(int k=1;k<=n-i;k++){
cout<<" ";
}
for(int k=i;k<=2*i-1;k++){
cout<<k<<" ";
}
for(int k=2*i-2;k>=i;k--){
cout<<k<<" ";
}
for(int k=1;k<=n-i;k++){
cout<<" ";
}
cout<<endl;
}
}
return 0;
}
 
@user69608 hi :-)
 
@JohnRennie free?
 
Yes :-)
When you post code, click the "Fixed font" button and it will preserve the indenting.
 
@JohnRennie ok
 
Can you repost the program using the fixed font?
 
10:37 AM
#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++){
	        for(int k=1;k<=n-i;k++){
	            cout<<"  ";
	        }
	        for(int k=i;k<=2*i-1;k++){
	            cout<<k<<" ";
	        }
	        for(int k=2*i-2;k>=i;k--){
	            cout<<k<<" ";
	        }
	        for(int k=1;k<=n-i;k++){
	            cout<<"  ";
	        }
	        cout<<endl;
	    }
	}
	return 0;
}
 
Cool :-)
 
for input 4 i get output:
      1
      1
      1
      1
      1
      1
      1
    2 3 2
    2 3 2
    2 3 2
    2 3 2
    2 3 2
    2 3 2
    2 3 2
  3 4 5 4 3
  3 4 5 4 3
  3 4 5 4 3
  3 4 5 4 3
  3 4 5 4 3
  3 4 5 4 3
  3 4 5 4 3
4 5 6 7 6 5 4
4 5 6 7 6 5 4
4 5 6 7 6 5 4
4 5 6 7 6 5 4
4 5 6 7 6 5 4
4 5 6 7 6 5 4
4 5 6 7 6 5 4
but the output i wanted was:
                    1
		2	3	2
	3	4	5	4	3
4	5	6	7	6	5	4
also can we decrease time complexity?
1 got slightly displaced
 
It looks like you have one loop too many ...
 
yea
 
You have three loops in the variables i, j and k, but you only need two.
You need to draw n rows, so you need an outer loop for i going from 1 to 4 - that's fine.
But then you just need the code to draw one row starting with the number i.
Which is what you do with all the k loops. So the j loop is unnecessary.
Try this:
#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++){
          for(int k=1;k<=n-i;k++){
              cout<<"  ";
          }
          for(int k=i;k<=2*i-1;k++){
              cout<<k<<" ";
          }
          for(int k=2*i-2;k>=i;k--){
              cout<<k<<" ";
          }
          for(int k=1;k<=n-i;k++){
              cout<<"  ";
          }
          cout<<endl;
//      }
  }
  return 0;
}
 
10:46 AM
oh yeah done
 
I just commented out the j loop to remove it. When I run this I get:
D:\rhs\c>test
4
      1
    2 3 2
  3 4 5 4 3
4 5 6 7 6 5 4
Which looks pretty good :-)
 
yes .can we decrease time complexity?
 
I can't think of an easy way to massively reduce the time taken ...
 
oh ok
 

« first day (120 days earlier)      last day (21 days later) »