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

Индивидуальное задание 1 Семенов Вячеслав 9091 #75

Closed
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Expand Up @@ -9,7 +9,9 @@ add_subdirectory(TestListC)
add_subdirectory(TestQueueC)
add_subdirectory(TestStackC)
add_subdirectory(TestVectorC)

add_subdirectory(TestArrayCPP)

add_subdirectory(TestArrayCPPTemplate)

add_subdirectory(IndividualTask1)

3 changes: 3 additions & 0 deletions IndividualTask1/CMakeLists.txt
@@ -0,0 +1,3 @@
add_executable(IndividualTask1 main.cpp)
target_include_directories(IndividualTask1 PUBLIC ../LibraryCPP)
target_link_libraries(IndividualTask1 LibraryCPP)
45 changes: 45 additions & 0 deletions IndividualTask1/main.cpp
@@ -0,0 +1,45 @@
#include <iostream>
#include "array.h"

using namespace std;

void ArrRandFill(Array* arr)
{
for (size_t i = 0; i < array_size(arr); i++)
array_set(arr, i, rand() % 201 - 100);
}

void ArrPrint(Array* arr)
{
for (size_t i = 0; i < array_size(arr); i++)
cout << array_get(arr, i) << " ";
cout << endl;
}

int ArrCheck(Array* arr)
{
int count = 0;
for (size_t i = 0; i < array_size(arr); i++)
for (Data j = 2; j <= 9; j++)
if (array_get(arr, i) % j == 0)
{
count++;
break;
}
return count;
}

int main()
{
int count;
size_t ArrSize;
cout << "Type array size: ";
cin >> ArrSize;
Array* arr = array_create(ArrSize);
ArrRandFill(arr);
ArrPrint(arr);
count = ArrCheck(arr);
cout << "Amount of numbers: " << count;
array_delete(arr);
return 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В конце нужно освобождать память, выделенную под массив.

}
16 changes: 12 additions & 4 deletions LibraryCPP/array.cpp
Expand Up @@ -3,33 +3,41 @@

struct Array
{
size_t ArrSize;
Data* ArrData;
};

// create array
Array *array_create(size_t size)
{
return new Array;
Array* NewArr = new Array;
NewArr->ArrSize = size;
NewArr->ArrData = new Data[size];
return NewArr;
}

// delete array, free memory
void array_delete(Array *arr)
{
delete arr;
delete[] arr->ArrData;
delete arr;
}

// returns specified array element
Data array_get(const Array *arr, size_t index)
{
return (Data)0;
return arr->ArrData[index];
}

// sets the specified array element to the value
void array_set(Array *arr, size_t index, Data value)
{
if (arr->ArrSize > index && index >= 0 )
arr->ArrData[index] = value;
}

// returns array size
size_t array_size(const Array *arr)
{
return 0;
return arr->ArrSize;
}