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

5
sheet1/A/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"files.associations": {
"vector": "cpp"
}
}

30
sheet1/A/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 means.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/A/main.GCC_ Executable file

Binary file not shown.

42
sheet1/A/main.cpp Normal file
View file

@ -0,0 +1,42 @@
#include <iostream>
#include <cmath>
#include <vector>
#include "means.h"
using namespace std;
int main() {
int a = 1, b = 4, c = 16;
double ar, ge, ha;
// c)
means(a, b, c, ar, ge, ha);
cout << "Arithmetic mean: " << ar
<< ", geometric mean: " << ge
<< ", harmonic mean: " << ha << endl;
// d)
a = 2; b = 3; c = 5;
means(a, b, c, ar, ge, ha);
cout << "Arithmetic mean: " << ar
<< ", geometric mean: " << ge
<< ", harmonic mean: " << ha << endl;
// e)
a = 1000; b = 4000; c = 16000;
means(a, b, c, ar, ge, ha);
cout << "Arithmetic mean: " << ar
<< ", geometric mean: " << ge
<< ", harmonic mean: " << ha << endl;
// f)
vector<int> input = {1, 2, 3, 4, 5, 6};
means_vector(input, ar, ge, ha);
cout << "Arithmetic mean: " << ar
<< ", geometric mean: " << ge
<< ", harmonic mean: " << ha << endl;
return 0;
}

BIN
sheet1/A/main.o Normal file

Binary file not shown.

36
sheet1/A/means.cpp Normal file
View file

@ -0,0 +1,36 @@
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
void means(int a, int b, int c, double &ar, double &ge, double &ha) {
ar = (a+b+c) / 3.0;
ge = pow(a,1.0/3.0) * pow(b,1.0/3.0) * pow(c,1.0/3.0); //do it instead of pow(a*b*c,1.0/3.0) to prevent integer overflow
ha = 3.0 / (1.0/a +1.0/b +1.0/c);
}
void means_vector(const vector<int> &input, double &ar, double &ge, double &ha) {
int size = input.size();
if (size == 0) {
cout << "Empty input" << endl;
return;
}
ar = 0;
ge = 1;
ha = 0;
for (int i = 0; i < size; i++) {
ar += input.at(i);
ge *= pow(input.at(i), 1.0 / size);
ha += 1.0 / input.at(i);
}
ar /= size;
ha = size / ha;
}

9
sheet1/A/means.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef MEANS_H_INCLUDED
#define MEANS_H_INCLUDED
#include <vector>
void means(int a, int b, int c, double &ar, double &ge, double &ha);
void means_vector(const std::vector<int> &input, double &ar, double &ge, double &ha);
#endif // MEANS_H_INCLUDED

BIN
sheet1/A/means.o Normal file

Binary file not shown.