Solutions

This commit is contained in:
Markus Schmidt 2025-10-21 19:36:38 +02:00
commit d3aa42a3e0
64 changed files with 2726 additions and 0 deletions

58
sheet1/E/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,58 @@
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"format": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
}
}

30
sheet1/E/Makefile Normal file
View file

@ -0,0 +1,30 @@
#
# use GNU-Compiler tools
COMPILER=GCC_
# alternatively from the shell
# export COMPILER=GCC_
# or, alternatively from the shell
# make COMPILER=GCC_
# use Intel compilers
#COMPILER=ICC_
# use PGI compilers
# COMPILER=PGI_
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
PROGRAM = main.${COMPILER}
# uncomment the next to lines for debugging and detailed performance analysis
CXXFLAGS += -g
LINKFLAGS += -g
# do not use -pg with PGI compilers
ifndef COMPILER
COMPILER=GCC_
endif
include ../${COMPILER}default.mk

View file

BIN
sheet1/E/main.GCC_ Executable file

Binary file not shown.

74
sheet1/E/main.cpp Normal file
View file

@ -0,0 +1,74 @@
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm>
#include "timing.h"
#include <list>
using namespace std;
std::default_random_engine generator;
unsigned int randomInt(unsigned int n)
{
std::uniform_int_distribution<int> distribution(1,n);
return distribution(generator);
}
void insertVector(vector<unsigned int>& vec)
{
unsigned int n = vec.size();
unsigned int random;
for(unsigned int i = 0; i<n; i++)
{
random = randomInt(n);
vector<unsigned int>::iterator it = lower_bound(vec.begin(), vec.end(), random);
vec.insert(it, random);
}
}
void insertList(list<unsigned int>& l)
{
unsigned int n = l.size();
unsigned int random;
for(unsigned int i = 0; i<n; i++)
{
random = randomInt(n);
list<unsigned int>::iterator it = lower_bound(l.begin(), l.end(), random);
l.insert(it, random);
}
}
int main()
{
unsigned int n = 10000;
vector<unsigned int> vec = {};
list<unsigned int> l = {};
for(unsigned int i=1; i<=n; i++)
{
vec.push_back(i);
l.push_back(i);
}
tic();
insertVector(vec);
double vecTime = toc();
tic();
insertList(l);
double listTime = toc();
cout << is_sorted(vec.begin(), vec.end())<< endl;
cout << is_sorted(l.begin(), l.end())<< endl;
cout << "vector time: " << vecTime << endl;
cout << "list time: " << listTime << endl;
return 0;
}

BIN
sheet1/E/main.o Normal file

Binary file not shown.

51
sheet1/E/timing.h Normal file
View file

@ -0,0 +1,51 @@
//
// Gundolf Haase, Oct 18 2024
//
#pragma once
#include <chrono> // timing
#include <stack>
//using Clock = std::chrono::system_clock; //!< The wall clock timer chosen
using Clock = std::chrono::high_resolution_clock;
using TPoint= std::chrono::time_point<Clock>;
// [Galowicz, C++17 STL Cookbook, p. 29]
std::stack<TPoint> MyStopWatch; //!< starting time of stopwatch
/** Starts stopwatch timer.
* Use as @code tic(); myfunction(...) ; double tsec = toc(); @endcode
*
* The timining can be nested and the recent time point is stored on top of the stack.
*
* @return recent time point
* @see toc
*/
auto tic()
{
MyStopWatch.push(Clock::now());
return MyStopWatch.top();
}
/** Returns the elapsed time from stopwatch.
*
* The time point from top of the stack is used
* if time point @p t_b is not passed as input parameter.
* Use as @code tic(); myfunction(...) ; double tsec = toc(); @endcode
* or as @code auto t_b = tic(); myfunction(...) ; double tsec = toc(t_b); @endcode
* The last option is to be used in the case of
* non-nested but overlapping time measurements.
*
* @param[in] t_b start time of some stop watch
* @return elapsed time in seconds.
*
*/
double toc(TPoint const &t_b = MyStopWatch.top())
{
// https://en.cppreference.com/w/cpp/chrono/treat_as_floating_point
using Unit = std::chrono::seconds;
using FpSeconds = std::chrono::duration<double, Unit::period>;
auto t_e = Clock::now();
MyStopWatch.pop();
return FpSeconds(t_e-t_b).count();
}

View file