Added project0

This commit is contained in:
Claudio Maggioni 2022-09-20 16:38:03 +02:00
commit d3ed218ee2
5 changed files with 209 additions and 0 deletions

View file

@ -0,0 +1,74 @@
#include <iostream>
#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;
}
}

View file

@ -0,0 +1,27 @@
#include <iostream>
//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);

View file

@ -0,0 +1,60 @@
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#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[])
{
}

View file

@ -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.*

View file

@ -0,0 +1,23 @@
// THIS PROGRAM REQUIRES NO MODIFICATION
#include <iostream>
#include <cstdlib>
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";
}
}