mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge branch 'master' into my-algo-contribution
This commit is contained in:
commit
d67d450c38
@ -22,7 +22,8 @@ int LinearSearch(int *array, int size, int key)
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < size; ++i)
|
for (int i = 0; i < size; ++i)
|
||||||
{
|
{
|
||||||
if (array[i] == key) {
|
if (array[i] == key)
|
||||||
|
{
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,10 +36,12 @@ int LinearSearch(int *array, int size, int key)
|
|||||||
* @brief Self-test implementations
|
* @brief Self-test implementations
|
||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
static void tests() {
|
static void tests()
|
||||||
|
{
|
||||||
int size = 4;
|
int size = 4;
|
||||||
int *array = new int[size];
|
int *array = new int[size];
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
array[i] = i;
|
array[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +50,8 @@ static void tests() {
|
|||||||
assert(LinearSearch(array, size, 2) == 2);
|
assert(LinearSearch(array, size, 2) == 2);
|
||||||
|
|
||||||
size = 6;
|
size = 6;
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
array[i] = i;
|
array[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +67,8 @@ static void tests() {
|
|||||||
* @brief Main function
|
* @brief Main function
|
||||||
* @returns 0 on exit
|
* @returns 0 on exit
|
||||||
*/
|
*/
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
int mode = 0;
|
int mode = 0;
|
||||||
|
|
||||||
std::cout << "Choose mode\n";
|
std::cout << "Choose mode\n";
|
||||||
@ -71,13 +76,14 @@ int main() {
|
|||||||
|
|
||||||
std::cin >> mode;
|
std::cin >> mode;
|
||||||
|
|
||||||
if (mode == 2) {
|
if (mode == 2)
|
||||||
|
{
|
||||||
int size = 0;
|
int size = 0;
|
||||||
std::cout << "\nEnter the size of the array: ";
|
std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
|
||||||
std::cin >> size;
|
std::cin >> size;
|
||||||
|
|
||||||
while ((size <= 1) || (size >= 30)) {
|
while (size <= 0 || size > 30){
|
||||||
std::cout << "Size cannot be less than zero. Please choose another value: ";
|
std::cout << "Size can only be 1-30. Please choose another value: ";
|
||||||
std::cin >> size;
|
std::cin >> size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +92,8 @@ int main() {
|
|||||||
|
|
||||||
// Input for the array elements
|
// Input for the array elements
|
||||||
std::cout << "Enter the array of " << size << " numbers: ";
|
std::cout << "Enter the array of " << size << " numbers: ";
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
std::cin >> array[i];
|
std::cin >> array[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +111,8 @@ int main() {
|
|||||||
}
|
}
|
||||||
delete[] array;
|
delete[] array;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
tests(); // run self-test implementations
|
tests(); // run self-test implementations
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user