Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

C++ Array

Yawar Abbas Abid


/* C++ Program - Linear Search */
#include<iostream>
using namespace std;
int main()
{
int arr[10]={10,20,30,40,50,60,70,80,90,100};
int i,n,loc=-1;

cout<<"Enter Value to find :";


cin>>n;

for(i=0; i<10; i++)


if(arr[i]==n)
loc=i;
if(loc==-1)
cout<<"value not found in array:";
else
cout<<"value found at index:"<<loc;
return 0;
}
/* C++ Program - Binary Search */ {
if(arr[middle] < search)
{
#include<iostream>
first = middle + 1;
Using namespace std;
int main()
}
{
else if(arr[middle] == search)
int n, i, arr[50], search, first, last, middle;
{
cout<<"Enter total number of elements :";
cout<<search<<" found at location
cin>>n;
"<<middle+1<<"\n";
cout<<"Enter "<<n<<" number :";
break;
for (i=0; i<n; i++)
}
{
else
cin>>arr[i];
{
}
last = middle - 1;
cout<<"Enter a number to find :";
}
cin>>search;
middle = (first + last)/2;
first = 0;
}
last = n-1;
if(first > last)
middle = (first+last)/2;
{
while (first <= last)
cout<<"Not found! "<<search<<" is not
present in the list.";
}
return o;
}
/* C++ Program - Selection Sort */
#include<iostream>
using namespace std;
int main() cout<<"Now the Array after sorting is :\n";
{ for(i=0; i<size; i++)
int size, arr[50], i, j, temp; {
cout<<"Enter Array Size : "; cout<<arr[i]<<" ";
cin>>size; }
cout<<"Enter Array Elements : "; return 0;
for(i=0; i<size; i++) }
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort...\n";
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
/* C++ Program - Bubble Sort */
#include<iostream>
cout<<"Elements sorted successfully..!!\n";
using namespace std;
cout<<"Sorted list in ascending order :\n";
int main()
{
for(i=0; i<n; i++)
int n, i, arr[50], j, temp; {
cout<<"Enter total number of elements :"; cout<<arr[i]<<" ";
cin>>n; }
cout<<"Enter "<<n<<" numbers :"; return 0;
for(i=0; i<n; i++) }
{
cin>>arr[i];
}
cout<<"Sorting array using bubble sort technique...\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
C++ Two Dimensional Array

data_type array_name[row_size][column_size];

int arr[5][2];

int arr[5][2] = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
/* C++ Two Dimensional Array */

#include<iostream>
using namespace std;
int main()
{

int arr[5][2] = { {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6} };
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<2; j++)
{
cout<<"arr["<<i<<"]["<<j<<"] = "<<arr[i][j]<<"\n";
}
}
return 0;
}
/* C++ Two Dimensional Array */

#include<iostream> cout<<"\nThe array elements are: \n";


using namespace std; for(i=0; i<5; i++)
int main() {
{ for(j=0; j<2; j++)
int arr[5][2]; {
int i, j; cout<<arr[i][j]<<" ";
int sum=0, avg=0; }
cout<<"\n";
cout<<"Enter 5*2 array elements: "; }
for(i=0; i<5; i++) cout<<"\n\nSum of all elements is: "<<sum;
{ avg = sum/10;
for(j=0; j<2; j++) cout<<"\nAnd average is: "<<avg;
{
cin>>arr[i][j]; return 0;
sum = sum + arr[i][j]; }
}
}
Multidimensional Array
• Three dimensional (3D) array contains three for loops in
programming. So, to initialize and print three dimensional array, you
have to use three for loops. Third for loop (the innermost loop) forms
1D array, Second for loop forms 2D array and the third for loop (the
outermost loop) forms 3D array, as shown here in the following
program.

• A three dimensional (3D) array can be thought of as an array of arrays


of arrays.
/* C++ Program - Three Dimensional Array Program */

#include<iostream>
using namespace std;
Int main()
{ cout<<"arr[0][0][0] = "<<arr[0][0][0]<<"\n";
int arr[3][4][2] = { cout<<"arr[0][2][1] = "<<arr[0][2][1]<<"\n";
{
cout<<"arr[2][3][1] = "<<arr[2][3][1]<<"\n";
{2, 4},
return 0;
{7, 8},
{3, 4}, }
{5, 6}
},
{
{7, 6},
{3, 4},
{5, 3},
{2, 3}
},
{
{8, 9},
{7, 2},
{3, 4},
{5, 1}
}
};
Passing Array to a Function in C++

• C++ Program to display marks of 5 students by passing one-


dimensional array to a function.
#include <iostream>
using namespace std;

void display(int marks[5]);

int main()
{
int marks[5] = {88, 76, 90, 61, 69};
display(marks);
return 0;
}

void display(int m[5])


{
cout << "Displaying marks: "<< endl;

for (int i = 0; i < 5; ++i)


{
cout << "Student "<< i + 1 <<": "<< m[i] << endl;
}
}
Passing Multidimensional Array to a
Function
• C++ Program to display the elements of two dimensional array by
passing it to a function.
#include <iostream> void display(int n[3][2])
using namespace std; {

void display(int n[3][2]); cout << "Displaying Values: " << endl;
for(int i = 0; i < 3; ++i)
int main() {
{ for(int j = 0; j < 2; ++j)
int num[3][2] = { {
{3, 4}, cout << n[i][j] << " ";
{9, 5}, }
{7, 1} }
}
};
display(num);

return 0;
}

You might also like