-
Notifications
You must be signed in to change notification settings - Fork 291
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
lab1 #842
Closed
Closed
lab1 #842
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dfc9296
Романов Дмитрий Александрович 3092
ssezmar da62022
Array
ssezmar 3b4d5e4
Array tests
ssezmar a5865ad
Array tests
ssezmar d8398d1
lab1
ssezmar 4006f86
lab1
ssezmar dd605fb
lab1
ssezmar 60be199
lab1
ssezmar 4ca2da5
lab1
ssezmar 738691b
lab1
ssezmar 9683e89
lab1
ssezmar dfec1f7
lab1
ssezmar 553923a
lab1
ssezmar a2c08de
lab1
ssezmar bfddf68
lab1
ssezmar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.c) | ||
# target_include_directories(Lab1C PUBLIC ../LibraryC) | ||
# target_link_libraries(Lab1C LibraryC) | ||
|
||
add_test(NAME TestLab1C COMMAND Lab1C ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) | ||
# add_test(NAME TestLab1C COMMAND Lab1C ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
add_executable(Lab1CPPClass lab1.cpp) | ||
target_include_directories(Lab1CPPClass PUBLIC ../LibraryCPPClass) | ||
target_link_libraries(Lab1CPPClass LibraryCPPClass) | ||
|
||
add_test(NAME TestLab1CPPClass COMMAND Lab1CPPClass ${CMAKE_CURRENT_SOURCE_DIR}/input.txt ${CMAKE_CURRENT_SOURCE_DIR}/output.txt ${CMAKE_CURRENT_SOURCE_DIR}/randomInput.txt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
9 | ||
5 4 3 2 5 4 3 2 5 | ||
7 | ||
1 3 2 3 4 3 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#include <iostream> | ||
Check notice on line 1 in Lab1CPPClass/lab1.cpp
|
||
#include <fstream> | ||
#include <cstdlib> | ||
#include <ctime> | ||
#include <bits/stdc++.h> | ||
#include "array.h" | ||
|
||
Array* array_create_and_read(std::ifstream& input) | ||
{ | ||
size_t n; | ||
input >> n; | ||
|
||
Array* arr = new Array(n); | ||
|
||
for (size_t i = 0; i < n; ++i) | ||
{ | ||
int x; | ||
input >> x; | ||
arr->set(i, x); | ||
} | ||
return arr; | ||
} | ||
|
||
|
||
Array* array_create_and_random_input(std::ifstream& input, int left, int right) { | ||
size_t n; | ||
input >> n; | ||
|
||
Array* arr = new Array(n); | ||
|
||
for (size_t i = 0; i < n; ++i) { | ||
int random_num = left + rand() % (right - left + 1); | ||
arr->set(i, random_num); | ||
} | ||
return arr; | ||
} | ||
|
||
|
||
void task1(const Array& arr) | ||
{ | ||
Array marks(4); | ||
for(size_t i = 0; i < arr.size(); i++) { | ||
marks.set(arr.get(i)-2, marks.get(arr.get(i)-2) + 1); // marks[arr[i] - 2]++; | ||
} | ||
for(size_t i = 0; i < 4; i++) { | ||
std::cout << "Mark " << i+2 << " : " << marks.get(i) << '\t'; | ||
} | ||
std::cout << '\n'; | ||
} | ||
|
||
void task2(const Array& arr) | ||
{ | ||
int min_value = INT_MAX; | ||
int max_value = INT_MIN; | ||
|
||
for (size_t i = 0; i < arr.size(); ++i) { | ||
int current = arr.get(i); | ||
if (current < min_value) min_value = current; | ||
if (current > max_value) max_value = current; | ||
} | ||
|
||
size_t frequency_size = max_value - min_value + 1; | ||
Array frequency(frequency_size); | ||
|
||
for (size_t i = 0; i < arr.size(); ++i) { | ||
int index = arr.get(i) - min_value; | ||
frequency.set(index, frequency.get(index) + 1); | ||
} | ||
|
||
int max_frequency = 0; | ||
int most_frequent_number = min_value; | ||
|
||
for (size_t i = 0; i < frequency_size; ++i) { | ||
if (frequency.get(i) > max_frequency) { | ||
max_frequency = frequency.get(i); | ||
most_frequent_number = i + min_value; | ||
} | ||
} | ||
|
||
std::cout << most_frequent_number << '\n'; | ||
} | ||
|
||
|
||
|
||
//be work for any nums, not only for the segment [-100, 100] | ||
void task2_v2(const Array& arr) { | ||
std::unordered_map<int, int> mp; | ||
|
||
for(size_t i = 0; i < arr.size(); i++) { | ||
int num = arr.get(i); | ||
if(mp.find(num) == mp.end()) { | ||
mp[num] = 0; | ||
} else { | ||
mp[num]++; | ||
} | ||
} | ||
|
||
int max_frequency = 0; | ||
int most_frequent_number = INT_MIN; | ||
|
||
for(auto it : mp) { | ||
if(it.second > max_frequency) { | ||
max_frequency = it.second; | ||
most_frequent_number = it.first; | ||
} | ||
} | ||
std::cout << most_frequent_number << '\n'; | ||
} | ||
|
||
bool check_output(std::ifstream& output, const Array& arr) { | ||
for(size_t i = 0; i < arr.size(); i++) { | ||
int num; | ||
output >> num; | ||
if(num != arr.get(i)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
//For random | ||
std::srand(std::time(nullptr)); | ||
|
||
// With my tests | ||
std::ifstream input(argv[1]); | ||
std::ifstream output(argv[2]); | ||
if (!input.is_open()) { | ||
std::cerr << "Failed to open file: " << argv[1] << "\n"; | ||
return 1; | ||
} | ||
|
||
Array* arr = array_create_and_read(input); | ||
task1(*arr); | ||
if(check_output(output, *arr)) { | ||
return 1; | ||
} | ||
delete arr; | ||
|
||
arr = array_create_and_read(input); | ||
task2(*arr); | ||
if(check_output(output, *arr)) { | ||
return 1; | ||
} | ||
delete arr; | ||
input.close(); | ||
|
||
// With random | ||
std::ifstream randomInput(argv[3]); | ||
if (!randomInput.is_open()) { | ||
std::cerr << "Failed to open file: " << argv[3] << "\n"; | ||
return 1; | ||
} | ||
|
||
arr = array_create_and_random_input(randomInput, 2, 5); | ||
task1(*arr); | ||
delete arr; | ||
|
||
arr = array_create_and_random_input(randomInput, -100, 100); | ||
task2(*arr); | ||
delete arr; | ||
|
||
randomInput.close(); | ||
|
||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
2 2 2 3 | ||
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
10 | ||
10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,59 @@ | ||
#include "array.h" | ||
Check notice on line 1 in LibraryCPPClass/array.cpp
|
||
|
||
Array::Array(size_t size) | ||
Array::Array(size_t size) : m_size(size), m_data(new Data[size]()) | ||
{ | ||
for(size_t i = 0; i < m_size; i++) { | ||
m_data[i] = 0; | ||
} | ||
} | ||
|
||
Array::Array(const Array &a) | ||
Array::Array(const Array &a) : m_size(a.m_size), m_data(new Data[a.m_size]) | ||
{ | ||
for (size_t i = 0; i < m_size; ++i) { | ||
m_data[i] = a.m_data[i]; | ||
} | ||
} | ||
|
||
Array &Array::operator=(const Array &a) | ||
{ | ||
if (this == &a) { | ||
return *this; | ||
} | ||
|
||
delete[] m_data; | ||
|
||
m_size = a.m_size; | ||
m_data = new Data[m_size]; | ||
|
||
for (size_t i = 0; i < m_size; ++i) { | ||
m_data[i] = a.m_data[i]; | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
Array::~Array() | ||
{ | ||
delete[] m_data; | ||
} | ||
|
||
Data Array::get(size_t index) const | ||
{ | ||
return Data(0); | ||
if (index >= m_size) { | ||
throw std::out_of_range("Index out of range\n"); | ||
} | ||
return m_data[index]; | ||
} | ||
|
||
void Array::set(size_t index, Data value) | ||
{ | ||
if (index >= m_size) { | ||
throw std::out_of_range("Index out of range"); | ||
} | ||
m_data[index] = value; | ||
} | ||
|
||
size_t Array::size() const | ||
{ | ||
return 0; | ||
return m_size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Романов Дмитрий Александрович 3092 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Здесь пусть остаётся, а в другие разы основная программа должна только выводить результат, а не проверять его.
Для проверки надо использовать вспомогательную программу, которая сравнит вывод основной и эталонный файл.