Pushing everything again, accidentally deleted my remote repository
This commit is contained in:
commit
1bee3e8e5b
101 changed files with 9428 additions and 0 deletions
28
ex1/ex1A_mean_values/.vscode/tasks.json
vendored
Normal file
28
ex1/ex1A_mean_values/.vscode/tasks.json
vendored
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: gcc build active file",
|
||||
"command": "/usr/bin/gcc",
|
||||
"args": [
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${fileDirname}/${fileBasenameNoExtension}"
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"detail": "Task generated by Debugger."
|
||||
}
|
||||
],
|
||||
"version": "2.0.0"
|
||||
}
|
||||
30
ex1/ex1A_mean_values/Makefile
Normal file
30
ex1/ex1A_mean_values/Makefile
Normal 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 ../ex1A_mean_values/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
|
||||
BIN
ex1/ex1A_mean_values/main.GCC_
Executable file
BIN
ex1/ex1A_mean_values/main.GCC_
Executable file
Binary file not shown.
35
ex1/ex1A_mean_values/main.cpp
Normal file
35
ex1/ex1A_mean_values/main.cpp
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include "means.h"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
double arithmetic_mean, geometric_mean, harmonic_mean;
|
||||
|
||||
// Fixed version
|
||||
calculate_means(1, 4, 16, arithmetic_mean, geometric_mean, harmonic_mean);
|
||||
cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl;
|
||||
|
||||
calculate_means(2, 3, 5, arithmetic_mean, geometric_mean, harmonic_mean);
|
||||
cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl;
|
||||
|
||||
calculate_means(1000, 4000, 16000, arithmetic_mean, geometric_mean, harmonic_mean);
|
||||
cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl;
|
||||
cout << "--------------------------------" << endl;
|
||||
|
||||
|
||||
|
||||
// Scalable version
|
||||
calculate_means(vector<int> {1, 4, 16}, arithmetic_mean, geometric_mean, harmonic_mean);
|
||||
cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl;
|
||||
|
||||
calculate_means(vector<int> {2, 3, 5}, arithmetic_mean, geometric_mean, harmonic_mean);
|
||||
cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl;
|
||||
|
||||
calculate_means(vector<int> {1000, 4000, 16000}, arithmetic_mean, geometric_mean, harmonic_mean);
|
||||
cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
ex1/ex1A_mean_values/main.o
Normal file
BIN
ex1/ex1A_mean_values/main.o
Normal file
Binary file not shown.
30
ex1/ex1A_mean_values/means.cpp
Normal file
30
ex1/ex1A_mean_values/means.cpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include "../ex1A_mean_values/means.h"
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
void calculate_means(int a, int b, int c, double &am, double &gm, double &hm)
|
||||
{
|
||||
am = (a + b + c)/3.0;
|
||||
gm = exp((log(a)+log(b)+log(c))/3);
|
||||
hm = 3.0/(1.0/a + 1.0/b + 1.0/c);
|
||||
}
|
||||
|
||||
void calculate_means(std::vector<int> numbers, double &am, double &gm, double &hm)
|
||||
{
|
||||
int n = numbers.size();
|
||||
|
||||
am = 0.;
|
||||
gm = 0.;
|
||||
hm = 0.;
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
am += numbers[i];
|
||||
gm += log(numbers[i]);
|
||||
hm += 1.0/numbers[i];
|
||||
}
|
||||
|
||||
am /= n;
|
||||
gm = exp(gm/n);
|
||||
hm = n/hm;
|
||||
}
|
||||
23
ex1/ex1A_mean_values/means.h
Normal file
23
ex1/ex1A_mean_values/means.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
This function calculates arithmetic mean, geometric mean and harmonic mean of three integers.
|
||||
@param[in] a first integer
|
||||
@param[in] b second integer
|
||||
@param[in] c third integer
|
||||
@param[out] am arithmetic mean
|
||||
@param[out] gm geometric mean
|
||||
@param[out] hm harmonic mean
|
||||
*/
|
||||
void calculate_means(int a, int b, int c, double &am, double &gm, double &hm);
|
||||
|
||||
/**
|
||||
This function calculates arithmetic mean, geometric mean and harmonic mean of an integer vector.
|
||||
@param[in] numbers vector containing integers
|
||||
@param[out] am arithmetic mean
|
||||
@param[out] gm geometric mean
|
||||
@param[out] hm harmonic mean
|
||||
*/
|
||||
void calculate_means(std::vector<int> numbers, double &am, double &gm, double &hm);
|
||||
|
||||
BIN
ex1/ex1A_mean_values/means.o
Normal file
BIN
ex1/ex1A_mean_values/means.o
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue