commit d3ed218ee2e75a064a3534b1fcab5d4fdeb29bad Author: Claudio Maggioni Date: Tue Sep 20 16:38:03 2022 +0200 Added project0 diff --git a/Project0/src/fraction_summing/fraction_toolbox.cpp b/Project0/src/fraction_summing/fraction_toolbox.cpp new file mode 100644 index 0000000..857c77e --- /dev/null +++ b/Project0/src/fraction_summing/fraction_toolbox.cpp @@ -0,0 +1,74 @@ +#include + +#include "fraction_toolbox.hpp" + +void print_fraction(fraction frac) +{ + std::cout << frac.num << '/' << frac.denom << std::endl; +} + +void print_fraction_array(fraction frac_array[], int n) +{ + std::cout << "[ " << frac_array[0].num << '/' << frac_array[0].denom << std::endl; + for (int i = 1; i < n-1; i++) + { + std::cout << " "; + print_fraction(frac_array[i]); + } + std::cout << " " << frac_array[n-1].num << '/' << frac_array[n-1].denom << " ]" << std::endl; +} + +fraction square_fraction(fraction frac) +{ + //TODO: implement function 2 +} + +//TODO: implement function 3 + + +double fraction2double(fraction frac) +{ + //TODO: implement function 4 +} + +int gcd(int a, int b) +{ + //TODO: implement function 5 +} + +//TODO: implement function 6 + + +void reduce_fraction_inplace(fraction & frac) +{ + //TODO: implement function 7 + + //TODO: add short comment to explain which of the gcd() functions your code is calling +} + +fraction add_fractions(fraction frac1, fraction frac2) +{ + //TODO: implement function 8 +} + +double sum_fraction_array_approx(fraction frac_array[], int n) +{ + //TODO: implement function 9 + + //TODO: add short comment to explain why this function is approximate +} + +//TODO: implement function 10 + + +void fill_fraction_array(fraction frac_array[], int n) +{ + fraction temp_frac; + temp_frac.num = 1; + for (int i = 1; i <= n; i++) + { + temp_frac.denom = i * (i+1); + frac_array[i-1] = temp_frac; + } +} + diff --git a/Project0/src/fraction_summing/fraction_toolbox.hpp b/Project0/src/fraction_summing/fraction_toolbox.hpp new file mode 100644 index 0000000..8d3df39 --- /dev/null +++ b/Project0/src/fraction_summing/fraction_toolbox.hpp @@ -0,0 +1,27 @@ +#include + +//TODO: implement fraction datatype + +void print_fraction(fraction frac); + +void print_fraction_array(fraction frac_array[], int n); + +fraction square_fraction(fraction frac); + +//TODO: add function declaration for function 3 + +double fraction2double(fraction frac); + +int gcd(int a, int b); + +int gcd(fraction frac); + +//TODO: add function declaration for function 7 + +fraction add_fractions(fraction frac1, fraction frac2); + +double sum_fraction_array_approx(fraction frac_array[], int n); + +fraction sum_fraction_array(fraction frac_array[], int n); + +void fill_fraction_array(fraction frac_array[], int n); \ No newline at end of file diff --git a/Project0/src/fraction_summing/main.cpp b/Project0/src/fraction_summing/main.cpp new file mode 100644 index 0000000..e65eaed --- /dev/null +++ b/Project0/src/fraction_summing/main.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +#include "fraction_toolbox.hpp" + +using namespace std; + +// read command line arguments +static void readcmdline(fraction & frac, int argc, char* argv[]) +{ + if (argc!=3) + { + printf("Usage: n d\n"); + printf(" n numerator of fraction\n"); + printf(" d denominator of fraction\n"); + exit(1); + } + + // read n + frac.num = atoi(argv[1]); + + // read d + frac.denom = atoi(argv[2]); +} + +static void test23467(int argc, char* argv[]) +{ + //TODO: implement function +} + +static void test5() +{ + //TODO: implement function +} + +static void test_array_functions(int n) +{ + //TODO: implement function + + //TODO: find n for which sum function breaks. Explain what is happening. +} + +static void test_toolbox(int argc, char* argv[]) +{ + cout << "\n=============== test23467 =============== " << endl; + test23467(argc, argv); + + cout << "\n================= test5 ================= " << endl; + test5(); + + cout << "\n========== test_array_functions ========= " << endl; + int n = 5; + test_array_functions(n); +} + +int main(int argc, char* argv[]) +{ + +} \ No newline at end of file diff --git a/Project0/src/fraction_summing/makefile b/Project0/src/fraction_summing/makefile new file mode 100644 index 0000000..eeed78a --- /dev/null +++ b/Project0/src/fraction_summing/makefile @@ -0,0 +1,25 @@ +# by default select gcc +CXX=g++ +CXXFLAGS=-O3 + + +SOURCES = fraction_toolbox.cpp +HEADERS = fraction_toolbox.hpp +OBJ = fraction_toolbox.o + +.SUFFIXES: .cpp + +all: main + +fraction_toolbox.o: fraction_toolbox.cpp fraction_toolbox.hpp + $(CXX) $(CXXFLAGS) -c fraction_toolbox.cpp -o fraction_toolbox.o + +main: $(OBJ) main.cpp $(HEADERS) + $(CXX) $(CXXFLAGS) *.o main.cpp -o main + +clean: + rm -f main + rm -f *.o + rm -f *.i + rm -f *.lst + rm -f output.* \ No newline at end of file diff --git a/Project0/src/read_environment.cpp b/Project0/src/read_environment.cpp new file mode 100644 index 0000000..e7456d8 --- /dev/null +++ b/Project0/src/read_environment.cpp @@ -0,0 +1,23 @@ +// THIS PROGRAM REQUIRES NO MODIFICATION + +#include +#include + +int main(int argc, char *argv[]) +{ + const char *uname = std::getenv("USER"); + const char *gcc = std::getenv("GCC_ROOT"); + const char *nodes = std::getenv("SLURM_JOB_NODELIST"); + + if (uname && gcc && nodes && argc == 2) + { + std::cout << "Your name is: " << argv[1] << '\n'; + std::cout << "Your username is: " << uname << '\n'; + std::cout << "Your gcc root is: " << gcc << '\n'; + std::cout << "You're connected to: " << nodes << '\n'; + } + else + { + std::cout << "Something went wrong...\n"; + } +}