arrays in c++
•Arrays
are used to store multiple values in a single variable, instead of declaring
separate variable
•Syntax : string car[5];
•The
index always start from zero
•So in
example [0],[1][2][3][4] that are indexes of car[5]
There are three types of an
array
1 dimensional array2 dimensional array3 dimensional array
1d array
•A
simple data structure that stores a collection of similar type data in a
contiguous block of memory
it store data in the form of list
program:
#include<iostream>
using namespace std;
int main()
{
int s[5],i,sum=0;
cout<<"enter values of an array"<<endl;
for(int i=0;i<5;i++)
{
cin>>s[i];
}
cout<<"-------------------------------------"<<endl;
cout<<"your arrays"<<endl;
for(int j=0;j<5;j++)
{
cout<<s[j]<<" "<<endl;
}
return 0;
}
it store multiple values in a single data structure this program take five integer values from user and display on screen.
video lecture
2 dimensional array:
#include<iostream>
using namespace std;
int main()
{
int s[2][3],i,j,k,l;
cout<<"enter values of an array"<<endl;
for(int i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cin>>s[i][j];
}
}
for(k=0;k<2;k++)
{
for(l=0;l<3;l++)
{
cout<<s[k][l]<<" ";
}
}
return 0;
}
video lecture:
3 dimensional array
#include<iostream>
using namespace std;
int main()
{
int s[2][3][4],i,j,k,l,m,n;
cout<<"enter values of an array"<<endl;
for(int i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<4;k++)
{
cin>>s[i][j][k];
}
}
}
for(l=0;l<2;l++)
{
for(m=0;m<3;m++)
{
for(n=0;n<4;n++)
{
cout<<s[l][m][n]<<" ";
}
}
}
return 0;
}
video lecture:
No comments:
Post a Comment