pattern

Pages: 12
sorry,i cant understand your problem , can explain clear than?
If you exclude the loops that are used to print array, then this program does the task in 2 loop, 1 outer while loop and 1 inner for loop ...

#include <iostream>
using namespace std;

//Function Definition
void display_arr(int arr[],int size)
{

for(int i=0;i<size;i++)
cout << arr[i] << " ";

for(int i=size-2;i>=0;i--)
cout << arr[i] << " ";
}

int main()
{
//Declaration
int n=4, t=n-1, *arr=new int[n];

//Initialization
for(int i=0;i<n;i++)
arr[i]=1;

//Borders START
for(int i=0;i<(n*2)-1;i++)
cout << 1 << " ";

cout << endl;

while(t!=0)
{
//Incrementing Elements
for(int i=n-t;i<n;i++)
{
arr[i]++;
}
//Displaying Array
display_arr(arr,n);

cout << endl;

t--;
}

fflush(stdin);
cin.ignore();
return 0;
}
best code is here,if you dont think so , please tell me...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
main()
{
    int i,j,n,r,c;
    int m2;
    cin>>n;
    for (i=1 ; i<2*n ; i++)
    {
        r=n-abs(n-i);
        for(j=1; j<2*n; j++)
        {
            c=n-abs(n-j);
            m2=(r<=c)?r:c;
            printf("%2d", m2);
        }
        printf("\n");
    }
    getch();
}
Topic archived. No new replies allowed.
Pages: 12