RSS

Category Archives: Patterns

PROGRAM TO FILL AN ARRAY ACCORDING TO A GIVEN PATTERN

/* THIS PROGRAM READS A 1D ARRAY AND FILLS A 2D ARRAY WITH IT

e.g if the entered array is 1 2 3 4 5

then the 2D array is 1 0 0 0 0
1 2 0 0 0
1 2 3 0 0
1 2 3 4 0
1 2 3 4 5
*/

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int size;
cout<<“\nENTER THE SIZE OF THE 1D ARRAY \n”;
cin>>size;
int *arr1 = new int[size];

cout<<“\nENTER THE 1D ARRAY \n”;

for(int i = 0; i<size; i++)
cin>>arr1[i];

int **arr2 = new int *[size];

for(i = 0; i<size; i++)
{
*(arr2+i) = new int[size];
}

for(i = 0; i<size; i++)
{
for(int j = 0; j<=i; j++)
{
arr2[i][j] = arr1[j];
}

for(int k = i+1; k<size; k++)
{
arr2[i][k] = 0;
}
}

cout<<“\nTHE NEW ARRAY IS \n”;

for(i = 0; i<size; i++)
{
for(int j = 0; j<size; j++)
cout<<arr2[i][j]<<‘ ‘;
cout<<‘\n’;
}
getch();
}

Advertisements
 
Leave a comment

Posted by on November 21, 2015 in Patterns, Pointers and Arrays

 

PATTERN6

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
int k=7;
for(int a=1;a<=4;a++)
{
int b=k;
for(int c=1;c<=(a-1);c++)
{
cout<<” “<<setw(3);
}
while(b>=1)
{
cout<<“*”<<setw(3);
b–;
}
cout<<“\n”;
k-=2;
}
getch();
}

 
Leave a comment

Posted by on November 16, 2014 in Patterns

 

PATTERN5

/*
D
DI
DIK
DIKS
DIKSH
DIKSHA
*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

void main()
{
clrscr();
char d=’D’,i=’I’,k=’K’,s=’S’,h=’H’,a=’A’;
int v=2,w=3,x=4,y=5,z=6;
for(int u=1;u<=6;u++)
{
cout<<d<<setw(2)<<” “;
for(;v<=u;v++)
{
cout<<i<<setw(2)<<” “;
for(;w<=u;w++)
{
cout<<k<<setw(2)<<” “;
for(;x<=u;x++)
{
cout<<s<<setw(2)<<” “;
for(;y<=u;y++)
{
cout<<h<<setw(2)<<” “;
for(;z<=u;z++)
{
cout<<a<<setw(2)<<” “;
}
}
}
}
}
cout<<“\n\n”;
}

getch();
}

 
Leave a comment

Posted by on November 16, 2014 in Patterns

 

PATTERN4

/*
A
BB
CCC
DDDD
*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
char range;
cout<<“Enter the range\n”;
cin>>range;
cout<<“\n”;
for(char a=’A’;a<=range;a++)
{
for(int b=65;b<=a;b++)
{
cout<<setw(2)<<a<<” “;
}
cout<<“\n\n”;
}
getch();
}

 
Leave a comment

Posted by on November 16, 2014 in Patterns

 

PATTERN3

/*  A
AB
ABC     */
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
char range;
cout<<“Enter the range\n”;
cin>>range;
cout<<“\n”;
for(char a=’A’;a<=range;a++)
{
for(char b=’A’;b<=a;b++)
{
cout<<setw(2)<<b<<” “;
}
cout<<“\n\n”;
}
getch();
}

 
Leave a comment

Posted by on November 16, 2014 in Patterns

 

PATTERN2

/*    *
**
***         */
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<iomanip.h>
void main()
{
clrscr();
for(int a=1;a<=10;a++)
{
for(int b=1;b<=a;b++)
{
cout<<“*”<<setw(2);
}
cout<<“\n”;
}
getch();
}

 
Leave a comment

Posted by on November 16, 2014 in Patterns

 

PATTERN1

/* A
BC
DEF */
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
clrscr();
char p=65,c=65,range;
int a=1;
cout<<setw(55)<<“Upto which character you wanna print?\n”;
cin>>range;
while(p<=range)
{
while(c<=p)
{
cout<<setw(2)<<c<<“”;
c++;
}
cout<<“\n\n”;
p=p+a;
a++;
}
getch();
}

 
Leave a comment

Posted by on November 16, 2014 in Patterns