2 D ARRAY
#include<iostream.h>
#include<conio.h>
main()
{
int a[10][10], m,n,i,j;
cout<<"enter rows and columns";
cin>>m>>n;
cout<<"enter elements of array";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"elements are";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
getch();
}
PASSING ARRAY TO FUNCTIONS
#include<iostream.h>
#include<conio.h>
main()
{
void sort(int[],int);
int a[100],n,i;
cout<<"enter n";
cin>>n;
cout<<"enter data now";
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,n);
cout<<"sorting array"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}
void sort(int a[], int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
maximum and minimum element in array
#include<iostream.h>
#include<conio.h>
main()
{
int a[100],n,i,max,min;
cout<<"enter no. of valuse want to store";
cin>>n;
cout<<"enter data";
for(i=0;i<n;i++)
{
cin>>a[i];
}
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
cout<<"max"<<max<<" ";
cout<<"min"<<min;
getch();
}
3 d matrix
#include<iostream.h>
#include<conio.h>
main()
{
int a[10][10],b[10][10],c[10][10],i,j,k;
int sum=0;
//clrscr();
cout<<"\n Enter First Matrix : \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"\n Enter Second Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>b[i][j];
}
cout<<"The First Matrix Is:\n";
for(i=0;i<3;i++)//print the first matrix
{
for(j=0;j<3;j++)
cout<<a[i][j];
cout<<"\n";
}
cout<<"The Second Matrix Is:\n";
for(i=0;i<3;i++) // print the second matrix
{
for(j=0;j<3;j++)
cout<<b[i][j];
cout<<"\n";
}
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
sum = 0;
for(k=0;k<=2;k++)
sum = sum + a[i][k] * b[k][j];
c[i][j]=sum;
}
cout<<"\n Multiplication Of Two Matrices :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<c[i][j];
cout<<"\n";
}
//getch();
}
average in array
#include<iostream.h>
#include<conio.h>
main()
{
int x[50],n,sum=0,i;
float avg;
cout<<"how many numbers";
cin>>n;
cout<<"enter elements";
for(i=0;i<n;i++)
{
cin>>x[i];
sum=sum+x[i];
}
avg=sum/n;
cout<<"average"<<avg<<endl;
getch();
}
initilization of 2 d array
#include<iostream.h>
#include<conio.h>
main()
{
int a[2][3]={{3,-5,9},
{43,78,12}};
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<'\n';
}
getch();
}
#include<iostream.h>
#include<conio.h>
main()
{
int a[10][10], m,n,i,j;
cout<<"enter rows and columns";
cin>>m>>n;
cout<<"enter elements of array";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"elements are";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
getch();
}
PASSING ARRAY TO FUNCTIONS
#include<iostream.h>
#include<conio.h>
main()
{
void sort(int[],int);
int a[100],n,i;
cout<<"enter n";
cin>>n;
cout<<"enter data now";
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,n);
cout<<"sorting array"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}
void sort(int a[], int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
maximum and minimum element in array
#include<iostream.h>
#include<conio.h>
main()
{
int a[100],n,i,max,min;
cout<<"enter no. of valuse want to store";
cin>>n;
cout<<"enter data";
for(i=0;i<n;i++)
{
cin>>a[i];
}
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
cout<<"max"<<max<<" ";
cout<<"min"<<min;
getch();
}
3 d matrix
#include<iostream.h>
#include<conio.h>
main()
{
int a[10][10],b[10][10],c[10][10],i,j,k;
int sum=0;
//clrscr();
cout<<"\n Enter First Matrix : \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"\n Enter Second Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>b[i][j];
}
cout<<"The First Matrix Is:\n";
for(i=0;i<3;i++)//print the first matrix
{
for(j=0;j<3;j++)
cout<<a[i][j];
cout<<"\n";
}
cout<<"The Second Matrix Is:\n";
for(i=0;i<3;i++) // print the second matrix
{
for(j=0;j<3;j++)
cout<<b[i][j];
cout<<"\n";
}
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
sum = 0;
for(k=0;k<=2;k++)
sum = sum + a[i][k] * b[k][j];
c[i][j]=sum;
}
cout<<"\n Multiplication Of Two Matrices :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<c[i][j];
cout<<"\n";
}
//getch();
}
average in array
#include<iostream.h>
#include<conio.h>
main()
{
int x[50],n,sum=0,i;
float avg;
cout<<"how many numbers";
cin>>n;
cout<<"enter elements";
for(i=0;i<n;i++)
{
cin>>x[i];
sum=sum+x[i];
}
avg=sum/n;
cout<<"average"<<avg<<endl;
getch();
}
initilization of 2 d array
#include<iostream.h>
#include<conio.h>
main()
{
int a[2][3]={{3,-5,9},
{43,78,12}};
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<'\n';
}
getch();
}
MORE EXAMPLES ON ARRAY
Reviewed by Shobhit Goel
on
August 10, 2015
Rating:
No comments:
Post a Comment