Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • fal/code-samples/comp290
1 result
Show changes
Commits on Source (3)
......@@ -11,13 +11,15 @@ target_sources(sorting
labs/sorting/main.cpp
labs/sorting/lab1.cpp
)
add_subdirectory(tests/labs/sorting)
add_subdirectory(tests/labs)
# Counting lab script
add_executable(counting)
target_include_directories(counting PRIVATE include)
target_sources(counting
PRIVATE
labs/counting/main.cpp
labs/counting/lab2.cpp
)
add_custom_command( TARGET counting
COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/labs/counting/generate.py
......
//
// Created by webpigeon on 29/12/22.
//
#ifndef COMP290_WORKSHEETS_LAB2_HPP
#define COMP290_WORKSHEETS_LAB2_HPP
#include <vector>
/**
* Calculate the average value of a vector of ints.
*
* @param numbers the numbers to calculate
* @return the average of the input values
*/
float calcAverage(std::vector<int> numbers);
/**
* Calculate the average value of a vector of ints.
*
* This version avoids modifying the vector, so doesn't have the overheads associated with that. It's here as a
* 'reasonable' approach to the problem. Assuming you can't calculate the sum as you go and avoid the need for
* the vector all-together...
*
* @param numbers the numbers to calculate
* @return the average of the input values
*/
float calcAverageFast(const std::vector<int>& numbers);
#endif //COMP290_WORKSHEETS_LAB2_HPP
//
// Created by webpigeon on 29/12/22.
//
#include "labs/counting/lab2.hpp"
/**
* Calculate the average value of a vector of ints.
*
* @param numbers the numbers to calculate
* @return the average of the input values
*/
float calcAverage(std::vector<int> numbers) {
int total = 0;
unsigned int size = 0; //probably should be size_t
while ( !numbers.empty() ) {
total += numbers[0];
numbers.erase( numbers.begin() );
size++;
}
return total / (float)size;
}
/**
* Calculate the average value of a vector of ints.
*
* This version avoids modifying the vector, so doesn't have the overheads associated with that. It's here as a
* 'reasonable' approach to the problem. Assuming you can't calculate the sum as you go and avoid the need for
* the vector all-together...
*
* @param numbers the numbers to calculate
* @return the average of the input values
*/
float calcAverageFast(const std::vector<int>& numbers) {
int total = 0;
for( auto& number : numbers ) {
total += number;
}
return total / (float)numbers.size();
}
\ No newline at end of file
......@@ -4,6 +4,8 @@
#include <vector>
#include <iostream>
#include "labs/counting/lab2.hpp"
/**
* Read a file into a vector.
*
......@@ -34,45 +36,6 @@ std::vector<int> readNumbers(const char* filename) {
return numbers;
}
/**
* Calculate the average value of a vector of ints.
*
* @param numbers the numbers to calculate
* @return the average of the input values
*/
float calcAverage(std::vector<int> numbers) {
int total = 0;
unsigned int size = 0; //probably should be size_t
while ( !numbers.empty() ) {
total += numbers[0];
numbers.erase( numbers.begin() );
size++;
}
return total / (float)size;
}
/**
* Calculate the average value of a vector of ints.
*
* This version avoids modifying the vector, so doesn't have the overheads associated with that. It's here as a
* 'reasonable' approach to the problem. Assuming you can't calculate the sum as you go and avoid the need for
* the vector all-together...
*
* @param numbers the numbers to calculate
* @return the average of the input values
*/
float calcAverageFast(const std::vector<int>& numbers) {
int total = 0;
for( auto& number : numbers ) {
total += number;
}
return total / (float)numbers.size();
}
int main(int argc, const char* argv[]) {
// check if the correct arguments were provided
if (argc != 2) {
......
find_package(benchmark)
if ( benchmark_FOUND )
add_subdirectory( sorting )
add_subdirectory( counting )
endif()
\ No newline at end of file
add_executable(count-benchmark)
target_link_libraries(count-benchmark benchmark::benchmark)
target_include_directories(count-benchmark PRIVATE ../../../include)
target_sources(count-benchmark
PRIVATE
count_bench.cpp
../../../labs/counting/lab2.cpp
)
\ No newline at end of file
//
// Created by webpigeon on 29/12/22.
//
#include <benchmark/benchmark.h>
// TODO use the sorting benchmark code as a guide, implement benchmarks for your lab2 script
BENCHMARK_MAIN();
\ No newline at end of file
find_package(benchmark REQUIRED)
add_executable(sort-benchmark)
target_link_libraries(sort-benchmark benchmark::benchmark)
target_include_directories(sort-benchmark PRIVATE ../../../include)
......