Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1f07f72
Homework 3 task 1
ilya-krivtsov Sep 25, 2024
4600352
Fixed build script
ilya-krivtsov Sep 26, 2024
d81ef99
Homework 3 task 2
ilya-krivtsov Sep 26, 2024
83ce466
Homework 3 task 3
ilya-krivtsov Sep 26, 2024
abd4185
Added free() to task 1 and task 2
ilya-krivtsov Sep 26, 2024
d1ce705
Use same array to compare sortings in hw3 t3
ilya-krivtsov Sep 26, 2024
b8cd913
Removed unnecessary srand()
ilya-krivtsov Sep 26, 2024
d619273
Edited error message in hw3 task 1
ilya-krivtsov Sep 26, 2024
5e7c6df
Overhauled build script
ilya-krivtsov Sep 26, 2024
8b0c17b
Added missing string.h to hw3 task 3
ilya-krivtsov Sep 26, 2024
0aff8a6
Added clear option to build script
ilya-krivtsov Sep 27, 2024
886d231
Added buildAll and testAll commands
ilya-krivtsov Sep 27, 2024
0d5b00b
Change directory to task's one when running/testing
ilya-krivtsov Sep 27, 2024
3d0de98
Removed minimal version requirement
ilya-krivtsov Sep 27, 2024
0a1cf86
Synced with main-hw2-and-hw3 (hw-3)
ilya-krivtsov Sep 29, 2024
f4bbb47
Fixed indentation and set bash as shell explicitly
ilya-krivtsov Sep 29, 2024
c275b7d
Call build script explicitly by bash
ilya-krivtsov Sep 29, 2024
59988b3
Added alloc checks and added format strings where needed (hw3)
ilya-krivtsov Oct 4, 2024
c4cff52
Added missing return statement
ilya-krivtsov Nov 2, 2024
6f008ab
Merge branch 'build-system' into hw-3
ilya-krivtsov Nov 7, 2024
a3e89b5
Merge branch 'build-system' into hw-3
ilya-krivtsov Nov 7, 2024
7115bcf
Merge branch 'main' into hw-3
ilya-krivtsov Nov 20, 2024
22d2374
Merge branch 'main' into hw-3
ilya-krivtsov Nov 23, 2024
e0e6d6d
Merge branch 'main' into hw-3
ilya-krivtsov Nov 23, 2024
870deb0
Merge branch 'main' into hw-3
ilya-krivtsov Nov 29, 2024
cc2d238
Removed 'PRIVATE' dependency modifiers (hw 3)
ilya-krivtsov Dec 8, 2024
73bae43
Fixed includes (hw 3)
ilya-krivtsov Dec 10, 2024
3536343
Added seed for random array test (hw3 task 3)
ilya-krivtsov Dec 14, 2024
5744067
Added README (hw 3)
ilya-krivtsov Dec 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ set(CMAKE_C_STANDARD_REQUIRED TRUE)
add_compile_options(-Wall -Wextra)

add_subdirectory(homework_2)
add_subdirectory(homework_3)
Empty file modified build.sh
100755 → 100644
Empty file.
7 changes: 7 additions & 0 deletions homework_3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
project(homework_3)

set(homeworkName "${PROJECT_NAME}")

add_subdirectory(task_1)
add_subdirectory(task_2)
add_subdirectory(task_3)
7 changes: 7 additions & 0 deletions homework_3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Homework 3

[Task 1. Most frequent element](/homework_3/task_1)

[Task 2. Search](/homework_3/task_2)

[Task 3. Smart QSort](/homework_3/task_3)
9 changes: 9 additions & 0 deletions homework_3/task_1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project("${homeworkName}_task_1")

add_library(frequencySearch frequencySearch.c)

add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} frequencySearch)

add_executable(${PROJECT_NAME}_test test.c)
target_link_libraries(${PROJECT_NAME}_test frequencySearch)
84 changes: 84 additions & 0 deletions homework_3/task_1/frequencySearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "frequencySearch.h"

#include <stdbool.h>
#include <stdlib.h>

int partition(int *array, int left, int right) {
int first = array[left], middle = array[(left + right) / 2], last = array[right];

int pivot = first;
if (first <= middle && middle <= last) {
pivot = middle;
} else if (middle <= first && first <= last) {
pivot = first;
} else {
pivot = last;
}

--left;
++right;

while (true) {
do {
++left;
} while (array[left] < pivot);
do {
--right;
} while (array[right] > pivot);

if (left >= right) break;

int t = array[left];
array[left] = array[right];
array[right] = t;
}

return right;
}

void quickSort(int *array, int left, int right) {
if (left >= right) {
return;
}

int part = partition(array, left, right);
quickSort(array, left, part);
quickSort(array, part + 1, right);
}

bool findMostFrequentElement(int *array, int arrayLength, int *mostFrequentElement, int *occurrences) {
if (arrayLength == 0) {
return false;
}

quickSort(array, 0, arrayLength - 1);

int element = array[0], count = 0;
int maxCountElement = element, maxCount = count;

for (int i = 0; i < arrayLength; ++i) {
if (array[i] != element) {
if (count > maxCount) {
maxCount = count;
maxCountElement = element;
}
count = 0;
element = array[i];
}
++count;
}

if (count > maxCount) {
maxCount = count;
maxCountElement = element;
}

if (mostFrequentElement != NULL) {
*mostFrequentElement = maxCountElement;
}
if (occurrences != NULL) {
*occurrences = maxCount;
}

return true;
}
5 changes: 5 additions & 0 deletions homework_3/task_1/frequencySearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <stdbool.h>

bool findMostFrequentElement(int *array, int arrayLength, int *mostFrequentElement, int *occurrences);
42 changes: 42 additions & 0 deletions homework_3/task_1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "frequencySearch.h"

void randomizeArray(int *array, int arrayLength, int minValue, int maxValue) {
for (int i = 0; i < arrayLength; ++i) {
array[i] = rand() * (maxValue - minValue) / RAND_MAX + minValue;
}
}

int readValue(const char *prompt, const char *incorrectValueMessage) {
int value;
printf("%s", prompt);
while ((scanf("%d", &value) != 1) || value < 0) {
while (getchar() != '\n') {}
printf("%s", incorrectValueMessage);
}
return value;
}

int main(void) {
int arrayLength = readValue("enter array length: ", "incorrect value: array length cannot be less than zero; try again: ");

int *array = calloc(arrayLength, sizeof(int));
if (array == NULL) {
return 1;
}

srand(time(NULL));
randomizeArray(array, arrayLength, 0, 128);

int mostFrequentElement = 0, occurrences = 0;
if (findMostFrequentElement(array, arrayLength, &mostFrequentElement, &occurrences)) {
printf("most frequent element is %d; it appears %d times in array", mostFrequentElement, occurrences);
} else {
printf("couldn't find most frequent element: the array is empty\n");
}

free(array);
}
47 changes: 47 additions & 0 deletions homework_3/task_1/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#define CTEST_MAIN
#define CTEST_SEGFAULT
#include "../../ctest/ctest.h"

#include <stdlib.h>

#include "frequencySearch.h"

int main(int argc, const char *argv[]) {
return ctest_main(argc, argv);
}

CTEST(frequencySearchTests, emptyArrayTest) {
ASSERT_FALSE(findMostFrequentElement(NULL, 0, NULL, NULL));
}

CTEST(frequencySearchTests, oneElementArrayTest) {
#define size 1
int array[size] = { 42 };
int mostFrequentExpected = 42;
int mostFrequentResult = 0;
ASSERT_TRUE(findMostFrequentElement(array, size, &mostFrequentResult, NULL));
ASSERT_EQUAL(mostFrequentResult, mostFrequentExpected);
#undef size
}

CTEST(frequencySearchTests, sameElementsArrayTest) {
#define size 8
int array[size] = { 42, 42, 42, 42, 42, 42, 42, 42 };
int mostFrequentExpected = 42, occurrencesExpected = size;
int mostFrequentResult = 0, occurrencesResult = 0;
ASSERT_TRUE(findMostFrequentElement(array, size, &mostFrequentResult, &occurrencesResult));
ASSERT_EQUAL(mostFrequentResult, mostFrequentExpected);
ASSERT_EQUAL(occurrencesResult, occurrencesExpected);
#undef size
}

CTEST(frequencySearchTests, constantArrayTest) {
#define size 10
int array[size] = { 1, 2, 3, 3, 3, 7, 9, 12, 8, 3 };
int mostFrequentExpected = 3, occurrencesExpected = 4;
int mostFrequentResult = 0, occurrencesResult = 0;
ASSERT_TRUE(findMostFrequentElement(array, size, &mostFrequentResult, &occurrencesResult));
ASSERT_EQUAL(mostFrequentResult, mostFrequentExpected);
ASSERT_EQUAL(occurrencesResult, occurrencesExpected);
#undef size
}
9 changes: 9 additions & 0 deletions homework_3/task_2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project("${homeworkName}_task_2")

add_library(sortAndSearch sortAndSearch.c)

add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} sortAndSearch)

add_executable(${PROJECT_NAME}_test test.c)
target_link_libraries(${PROJECT_NAME}_test sortAndSearch)
53 changes: 53 additions & 0 deletions homework_3/task_2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "sortAndSearch.h"

int getRandom(int minValue, int maxValue) {
int r = rand();
return r * (maxValue - minValue) / RAND_MAX + minValue;
}

int readValue(const char *prompt, const char *incorrectValueMessage) {
int value;
printf("%s", prompt);
while ((scanf("%d", &value) != 1) || value < 0) {
while (getchar() != '\n') {}
printf("%s", incorrectValueMessage);
}
return value;
}

int main(void) {
int arrayLength = readValue("enter array length: ", "incorrect value: array length cannot be less than zero; try again: ");
int numbersToCheckCount = readValue("enter amount of numbers to check: ", "incorrect value: amount cannot be less than zero; try again: ");

int *array = calloc(arrayLength, sizeof(int));
if (array == NULL) {
return 1;
}

int minRandom = 0,
maxRandom = 128;

srand(time(NULL));

for (int i = 0; i < arrayLength; ++i) {
array[i] = getRandom(minRandom, maxRandom);
}

quickSort(array, arrayLength);

int foundNumbers = 0;
for (int i = 0; i < numbersToCheckCount; ++i) {
int number = getRandom(minRandom, maxRandom);
if (containsElement(array, arrayLength, number)) {
++foundNumbers;
}
}

printf("out of %d randomly generated numbers %d %s in the array\n", numbersToCheckCount, foundNumbers, foundNumbers == 1 ? "was" : "were");

free(array);
}
73 changes: 73 additions & 0 deletions homework_3/task_2/sortAndSearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "sortAndSearch.h"

#include <stdbool.h>

int partition(int *array, int left, int right) {
int first = array[left], middle = array[(left + right) / 2], last = array[right];

int pivot = first;
if (first <= middle && middle <= last) {
pivot = middle;
} else if (middle <= first && first <= last) {
pivot = first;
} else {
pivot = last;
}

--left;
++right;

while (true) {
do {
++left;
} while (array[left] < pivot);
do {
--right;
} while (array[right] > pivot);

if (left >= right) break;

int t = array[left];
array[left] = array[right];
array[right] = t;
}

return right;
}

void quickSortInternal(int *array, int left, int right) {
if (left >= right) {
return;
}

int part = partition(array, left, right);
quickSortInternal(array, left, part);
quickSortInternal(array, part + 1, right);
}

void quickSort(int *array, int arrayLength) {
quickSortInternal(array, 0, arrayLength - 1);
}

bool containsElement(int *array, int arrayLength, int value) {
if (arrayLength == 0) {
return false;
}

int left = 0, right = arrayLength - 1;

while (left <= right) {
int middlePoint = (left + right) / 2,
middleElement = array[middlePoint];

if (value < middleElement) {
right = middlePoint - 1;
} else if (value > middleElement) {
left = middlePoint + 1;
} else {
return true;
}
}

return array[left] == value;
}
8 changes: 8 additions & 0 deletions homework_3/task_2/sortAndSearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdbool.h>

void quickSort(int *array, int arrayLength);

/// array must be sorted before calling this function
bool containsElement(int *array, int arrayLength, int value);
Loading