Commit ca3716c3 authored by Michael's avatar Michael

michael.davityan:exercise_07_33v2

parent a1c6f0b6
progname=exercise_07_33
CC=g++
CFLAGS=-Wall -Wextra -Werror -g3
all: test1 test2
debug/$(progname): debug/$(progname).o
$(CC) $(CFLAGS) $< -o $@
debug/%.o: debug/%.s
$(CC) -c $(CFLAGS) $< -o $@
debug/%.s: debug/%.ii
$(CC) -S $(CFLAGS) $< -o $@
debug/%.ii: %.cpp | debug
$(CC) -E $(CFLAGS) $< -o $@
debug:
mkdir -p debug
clean:
rm -r debug Tests/*.out
test%: debug/$(progname)
@./debug/$(progname) < Tests/$@.in > Tests/$@.out 2>&1 || echo "$@ is executing..."
@diff Tests/$@.out Tests/$@.expected && echo PASSED || echo FAILED
.PRECIOUS: debug/%.ii debug/%.s
.SECONDARY: debug/%.ii debug/%.s
#include <iostream>
#include <unistd.h>
int
recursiveLinearSearch(const int integersArray[ ] ,const int searchKey, const size_t sizeOfArray,
const size_t firstIdx = 0)
{
if (integersArray[firstIdx] == searchKey) {
return firstIdx;
}
if (firstIdx == sizeOfArray) {
return -1;
}
return recursiveLinearSearch(integersArray, searchKey, sizeOfArray, firstIdx + 1);
}
int
main()
{
const size_t ARRAY_SIZE = 100;
int integersArray[ARRAY_SIZE] ;
for (size_t index = 0; index < ARRAY_SIZE; ++index) {
integersArray[index] = 2 * index;
}
if (::isatty(STDIN_FILENO)) {
std::cout << "Enter integer search key: ";
}
int searchKey;
std::cin >> searchKey;
const int element = recursiveLinearSearch(integersArray, searchKey, ARRAY_SIZE) ;
if (element != -1) {
std::cout << "Found value in element " << element << "\n";
} else {
std::cout << "Value not found" << std::endl;
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment