Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 1. Array #929

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()

add_subdirectory(LibraryC)
#add_subdirectory(LibraryC)
add_subdirectory(LibraryCPP)
add_subdirectory(LibraryCPPClass)
add_subdirectory(LibraryCPPTemplate)
#add_subdirectory(LibraryCPPClass)
#add_subdirectory(LibraryCPPTemplate)

add_subdirectory(Lab1C)
6 changes: 3 additions & 3 deletions Lab1C/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_executable(Lab1C lab1.c)
target_include_directories(Lab1C PUBLIC ../LibraryC)
target_link_libraries(Lab1C LibraryC)
add_executable(Lab1C lab1.cpp)
target_include_directories(Lab1C PUBLIC ../LibraryCPP)
target_link_libraries(Lab1C LibraryCPP)

add_test(NAME TestLab1C COMMAND Lab1C ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
40 changes: 0 additions & 40 deletions Lab1C/lab1.c

This file was deleted.

64 changes: 64 additions & 0 deletions Lab1C/lab1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>

Check notice on line 1 in Lab1C/lab1.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab1C/lab1.cpp

File Lab1C/lab1.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44, 45, 46, 47, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62)
#include "array.h"

Array* array_create_and_read(FILE* input)
{
int n;
fscanf(input, "%d", &n);
/* Create array */
Array* arr = array_create(n);
/* Read array data */
for (int i = 0; i < n; ++i)
{
int x;
fscanf(input, "%d", &x);
array_set(arr, i, x);
}
return arr;
}

void task1(Array* arr) {
int maxElement = 0;
int firstMaxElement = 0;
int lastMaxElement = 0;

for (int i = 0; i < array_size(arr); i++) {
if (array_get(arr, i) > maxElement) {
maxElement = array_get(arr, i);
firstMaxElement = i;
lastMaxElement = i;
}
else if (array_get(arr, i) == maxElement) {
lastMaxElement = i;
}
}


printf("%d %d\n", firstMaxElement, lastMaxElement);
}

void task2(Array* arr) {
int maxSum = 0;
for (int i = 0; i < array_size(arr) - 5; i++) {
if (array_get(arr, i) + array_get(arr, i + 1) + array_get(arr, i + 2) + array_get(arr, i + 3) + array_get(arr, i + 4) > maxSum) {
maxSum = array_get(arr, i) + array_get(arr, i + 1) + array_get(arr, i + 2) + array_get(arr, i + 3) + array_get(arr, i + 4);
}
}
printf("%d\n", maxSum);

}

int main(int argc, char** argv)
{
Array* arr = NULL;
FILE* input = fopen_s(argv[1], "r");
arr = array_create_and_read(input);
task1(arr);
array_delete(arr);

/* Create another array here */
arr = array_create_and_read(input);
task2(arr);
array_delete(arr);
fclose(input);
}
8 changes: 4 additions & 4 deletions LibraryCPP/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# add_executable(TestArrayCPP array.cpp)
# target_include_directories(TestArrayCPP PUBLIC ..)
# target_link_libraries(TestArrayCPP LibraryCPP)
# add_test(TestArrayCPP TestArrayCPP)
add_executable(TestArrayCPP array.cpp)
target_include_directories(TestArrayCPP PUBLIC ..)
target_link_libraries(TestArrayCPP LibraryCPP)
add_test(TestArrayCPP TestArrayCPP)

# add_executable(TestListCPP list.cpp)
# target_include_directories(TestListCPP PUBLIC ..)
Expand Down
41 changes: 25 additions & 16 deletions LibraryCPP/array.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
#include "array.h"

Check notice on line 1 in LibraryCPP/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/array.cpp

File LibraryCPP/array.cpp does not conform to Custom style guidelines. (lines 1, 5, 6, 11, 12, 13, 14, 15, 16, 17, 18, 23, 24, 25, 30, 31, 35, 40, 41, 42)

struct Array
{

// Ñòðóêòóðà ìàññèâà
struct Array {

Check warning on line 5 in LibraryCPP/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/array.cpp:5:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: elements, size
Data* elements;
size_t size;
};

// create array
Array *array_create(size_t size)
// Ñîçäàíèå ìàññèâà
Array* array_create(size_t size)
{
Array* arr = new Array;
arr->elements = new Data[size];
arr->size = size;
if (size == 0) {
return nullptr;
}
return new Array;
}

// delete array, free memory
void array_delete(Array *arr)
// Óäàëèòü ìàññèâ, îñâîáîäèòü ïàìÿòü
void array_delete(Array* arr)
{
delete[] arr->elements;
delete arr;
}

// returns specified array element
Data array_get(const Array *arr, size_t index)
{
return (Data)0;
// Âîçâðàùåíèå óêàçàííîãî ýëåìåíòà ìàññèâà
Data array_get(const Array* arr, size_t index) {
return arr->elements[index];
}

// sets the specified array element to the value
void array_set(Array *arr, size_t index, Data value)
{
// Óñòàíîâèòü çíà÷åíèå óêàçàííîãî ýëåìåíòà â ìàññèâå
void array_set(Array* arr, size_t index, Data value) {
arr->elements[index] = value;
}

// returns array size
size_t array_size(const Array *arr)
// Âåðíóòü ðàçìåð ìàññèâà
size_t array_size(const Array* arr)
{
return 0;
return arr->size;
}
1 change: 1 addition & 0 deletions me.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Алексеев Артем Павлович 3091
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"requests":[{"kind":"cache","version":2},{"kind":"cmakeFiles","version":1},{"kind":"codemodel","version":2},{"kind":"toolchains","version":1}]}
Loading
Loading