Professional Documents
Culture Documents
Examples:
Representing the marks of 60 students of class in 5 subjects, we
can take a 2D array of row size:60 and column size:5
Representing the sales done by all(50) sales persons of a
particular branch for all months(12), we can take a 2D array of row
size:50 and column size:12
CSE101-Computer Programming
Initializing Arrays
• Array is same as the variable can prompt for
value from the user at run time.
• Array is a group of elements so we use for
loop to get the values of every element
instead of getting single value at a time.
• Example: int array[5]; // array of size 5
for(int i=0;i<5;i++){// loop begins from
0 to 4
cin>>array[i];
}
@LPU CSE202 C++ Programming
2D-Array
A two – dimensional array can be seen as a table with ‘x’ rows and
‘y’ columns where the row number ranges from 0 to (x-1) and
column number ranges from 0 to (y-1). A two – dimensional array
‘x’ with 3 rows and 3 columns is shown below:
Examples:
Representing the marks of 60 students of class in 5 subjects, we
can take a 2D array of row size:60 and column size:5
Representing the sales done by all(50) sales persons of a
particular branch for all months(12), we can take a 2D array of row
size:50 and column size:12
CSE101-Computer Programming
Operations on arrays
• Insertion of element into an array
• Deletion of element from an array
• Search of element in an array
• Linear search
• Binary Search
• Sorting
77 56 4 10 34 2 Original array
56 4 10 34 2 77 After pass 1
4 10 34 2 56 77 After pass 2
4 10 2 34 56 77 After pass 3
4 2 10 34 56 77 After pass 4
2 4 10 34 56 77 After pass 5
{
int main() • Program to
int a[100],i,n,k,
item; insert an
element
cout<<"how many
no to store in
array";
cin>>n;
cout<<"Enter the number";
into an
for(i=0;i<=n-1;i++)
cin>>a[i]; array
cout<<"Enter the no. and its position";
cin>>tem>>k;
k=k-1;
for(i=n-1;i>=k;i--)
{
a[i+1]=a[i];
}
a[k]=item;
cout<<"Contents of
the array\n"; for(i=0;i<=n;i++)
{
cout<<a[i];
}
return 0;
@LPU CSE202 C++ Programming}
How many no to store in array: 4
Enter the number: 12
14
Output
5
11
Enter the no. and the position: 20 3
Content of the array
12
14
5
20
11