-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ded281
commit 60157fc
Showing
8 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.