Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Wednesday, September 21, 2016

UPPER TRIANGULAR MATRIX

PROGRAM TO DISPLAY UPPER TRIANGULAR MATRIX.

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a[20][20],i,j,n;
cout<<"Enter Size of Square Matrix: ";
cin>>n;
cout<<"\nEnter "<<(n*n)<<" Elements of Matrix: ";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cin>>a[i][j];
}

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i>j)
a[i][j]=0;
}
}
cout<<"\nUpper Triangular Matrix is:\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<"\n\n";
}
getch();
return 0;
}





Monday, September 19, 2016

Reverse An Array

//program to print array elements in reverse
#include<iostream>
using namespace std;
main()
{
int a[10],b[10];
cout<<"ENTER ELEMENTS";
for(int i=0;i<10;i++)
cin>>a[i];
int i=9;
int j=0;
 while(j<10&&i>=0)
 {
  b[j]=a[i];
     i--;
     j++;
 }
 for(i=0;i<10;i++)
 {
  cout<<b[i]<<"\t";
 }
}




Also see program of operator overloading CLICK HERE

Monday, September 5, 2016

PROGRAM TO DELETE ANY ELEMENT FROM AN ARRAY

PROGRAM TO DELETE ANY ELEMENT FROM AN ARRAY.

#include <iostream>
using namespace std;
int main()
{
int a[100],index,size,i,num;
cout<<"Enter Size of Array: ";
cin>>size;
cout<<"\nEnter "<<size<<" Array Elements: ";
for(i=0;i<size;i++)
cin>>a[i];
cout<<"\nEnter Element to be Deleted: ";
cin>>num;
for(i=0;i<size;i++)
{
if(a[i]==num)
{
index=i;
break;
}
}
for(i=index;i<size;i++)
{
a[i]=a[i+1];
}
cout<<"\nArray after Deletion is:\n";
for(i=0;i<size-1;i++)
cout<<a[i]<<" ";
return 0;
}





PROGRAM TO PRINT THE FOLLOWING PATTERN:

PROGRAM TO PRINT THE FOLLOWING PATTERN:
6
65
654
6543
65432
654321

#include <iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"Enter Length of Pattern: ";
cin>>n;
cout<<"\nPattern is:\n";
for(i=n;i>=1;i--)
{
for(j=n;j>=i;j--)
cout<<j;
cout<<endl;
}
return 0;
}





PROGRAM TO DISPLAY STAR TRIANGLE PATTERN:

PROGRAM TO DISPLAY  STAR TRIANGLE PATTERN  IN AN ARRAY

#include <iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"Enter Length of Pattern: ";
    cin>>n;
    cout<<"\nPattern is:\n";

    for(i=n; i>=1; i--)
    {
        for(j=n; j>=i; j--)
            cout<<"*";
        cout<<endl;
    }
    return 0;
}


PROGRAM TO PERFORM PUSH AND POP OPERATIONS ON STACK:

PROGRAM TO PERFORM PUSH AND POP OPERATIONS ON STACK IMPLEMENTED AS AN ARRAY.

#include <iostream>
using namespace std;
void push(int stack[],int max,int i,int top);
void pop(int stack[],int max,int i,int top);
int main()
{
    int choice,stack[30],max,i;
start:
    int top=-1;
    cout<<"\n\n***Operations on STACK***\n\n";
    cout<<"Enter Size of Stack: ";
    cin>>max;
    cout<<"\nEnter "<<max<<" Elements of Stack: ";

    for(i=0; i<max; i++)
    {
        cin>>stack[i];
        top++;
    }
    cout<<"\n1: Push\n2: Pop\n";
    cout<<"Enter your Choice(Press F6 to Exit): ";

    while(cin>>choice)
    {

        switch(choice)
        {
        case 1:
            push(stack,max,i,top);
            cout<<"\nEnd of Program";
            break;
        case 2:
            pop(stack,max,i,top);
            cout<<"\nEnd of Program";
            break;
        default:
            cout<<"\nInvalid Input";
        }
        goto start;
    }
    return 0;
}

void push(int stack[],int max,int i,int top)
{
    int item;
    if(top==29)
        cout<<"\nStack Overflow!!";
    else
    {
        cout<<"\nEnter Element to be Inserted(Pushed): ";
        cin>>item;
        top++;
        stack[top]=item;
        cout<<"\nNew Stack is:\n";
        for(i=0; i<=top; i++)
            cout<<stack[i]<<" ";
    }
}


void pop(int stack[],int max,int i,int top)
{
    if(top==-1)
        cout<<"\nStack UnderFlow!!";
    else
    {
        top--;
        cout<<"\nNew Stack is:\n";
        for(i=0; i<=top; i++)
            cout<<stack[i]<<" ";
    }
}




Saturday, September 3, 2016

Program to add two matrices:

PROGRAM TO FIND MAXIMUM AND MINIMUM ELEMENT OF AN ARRAY:

Friday, September 2, 2016

PROGRAM TO PRINT THE FOLLOWING PATTERN
1
22
333
4444
55555

:
#include<iostream>
using namespace std;
class pattern
{
int i,j;
public:
void patt()
{
for(i=1;i<6;i++)
{for(j=1;j<=i;j++)
cout<<i;
cout<<"\n";
}
 
}

};
main()
{pattern p;
p.patt();
}
PROGRAM TO SEARCH A NUMBER IN AN ARRAY:



#include<iostream>
#include<cmath>
using namespace std;
main()
{int a[100],b,c,d,s,e,m;
cout<<"ENTER THE NO OF ELEMENTS";
cin>>b;
cout<<"ENTER THE ELEMENTS";
for(c=0;c<b;c++)
cin>>a[c];
cout<<"ENTER THE VALUE TO SEARCH";
cin>>d;
s=0;
e=b;
m=(s+e)/2;
while(b<=e  && a[m]!=d)
{
if(a[m]>d)
{e=m-1;}
else if(a[m]<d)
s=m+1;
}
m=m+1; 
cout<<"VALUE "<<d<<" EXIST AT "<<m;
}