diff --git a/arrays/2D-array.cpp b/arrays/2D-array.cpp new file mode 100644 index 0000000..a50eb79 --- /dev/null +++ b/arrays/2D-array.cpp @@ -0,0 +1,28 @@ +// declaration and initialization of array +#include +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; +} \ No newline at end of file diff --git a/arrays/2D-array.exe b/arrays/2D-array.exe new file mode 100644 index 0000000..f7f0464 Binary files /dev/null and b/arrays/2D-array.exe differ diff --git a/arrays/arryinsertion.cpp b/arrays/arryinsertion.cpp new file mode 100644 index 0000000..277cb69 --- /dev/null +++ b/arrays/arryinsertion.cpp @@ -0,0 +1,26 @@ +// declaration and initialization of array +#include +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; +} \ No newline at end of file diff --git a/arrays/arryinsertion.exe b/arrays/arryinsertion.exe new file mode 100644 index 0000000..5611ffc Binary files /dev/null and b/arrays/arryinsertion.exe differ diff --git a/arrays/binarysearcharray.cpp b/arrays/binarysearcharray.cpp new file mode 100644 index 0000000..3d5cae1 --- /dev/null +++ b/arrays/binarysearcharray.cpp @@ -0,0 +1,36 @@ +// linear search using arrays +#include +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; +} \ No newline at end of file diff --git a/arrays/binarysearcharray.exe b/arrays/binarysearcharray.exe new file mode 100644 index 0000000..4392eca Binary files /dev/null and b/arrays/binarysearcharray.exe differ diff --git a/arrays/linearsearcharray.cpp b/arrays/linearsearcharray.cpp new file mode 100644 index 0000000..27e03ab --- /dev/null +++ b/arrays/linearsearcharray.cpp @@ -0,0 +1,27 @@ +// linear search using arrays +#include +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; +} \ No newline at end of file diff --git a/arrays/linearsearcharray.exe b/arrays/linearsearcharray.exe new file mode 100644 index 0000000..94b786c Binary files /dev/null and b/arrays/linearsearcharray.exe differ