Skip to content

Commit

Permalink
C++ Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishak798 authored Jan 20, 2024
1 parent 6ded281 commit 60157fc
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 0 deletions.
28 changes: 28 additions & 0 deletions arrays/2D-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// declaration and initialization of array
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[5][5];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << "enter array element " << i << j << " : ";
cin >> arr[i][j];
cout << endl;
}
}
cout << "created array: [ ";
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
// using for loop
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << "]";
return 0;
}
Binary file added arrays/2D-array.exe
Binary file not shown.
26 changes: 26 additions & 0 deletions arrays/arryinsertion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// declaration and initialization of array
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[5];
for (int i = 0; i < 5; i++)
{
cout << "enter array element " << i << " : ";
cin >> arr[i];
cout << endl;
}
cout << "created array: [ ";
for (int i = 0; i < 5; i++)
{
// using for loop
cout << arr[i] << " , ";
}
// using for each loop
for (int x : arr)
{
cout << x << " ";
}
cout << "]";
return 0;
}
Binary file added arrays/arryinsertion.exe
Binary file not shown.
36 changes: 36 additions & 0 deletions arrays/binarysearcharray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// linear search using arrays
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[10];
int key = 0;
cout << "Enter key value to be searched: ";
cin >> key;
for (int i = 0; i < 8; i++)
{
cout << "enter array element " << i << " : ";
cin >> arr[i];
cout << endl;
}
int low = 0, high = 8, mid = 0;
while (low <= high)
{
mid = (low + high) / 2;
if (key == arr[mid])
{
cout << "Key found " << mid;
return 0;
}
else if (key < arr[mid])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
cout << "Key not found!";
return 0;
}
Binary file added arrays/binarysearcharray.exe
Binary file not shown.
27 changes: 27 additions & 0 deletions arrays/linearsearcharray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// linear search using arrays
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[10];
int key = 0;
cout << "Enter key value to be searched: ";
cin >> key;
for (int i = 0; i < 8; i++)
{
cout << "enter array element " << i << " : ";
cin >> arr[i];
cout << endl;
}

for (int i = 0; i < 8; i++)
{
if (key == arr[i])
{
// using for loop
cout << "Key found at index: " << i;
}
}

return 0;
}
Binary file added arrays/linearsearcharray.exe
Binary file not shown.

0 comments on commit 60157fc

Please sign in to comment.