1
0
Fork 0

EX2: Finished

This commit is contained in:
Eric Teunis de Boone 2019-12-03 10:32:24 +01:00
parent 82260a45db
commit 511a6c4c55
3 changed files with 94 additions and 8 deletions

View File

@ -2,7 +2,11 @@ d)
g++ -c max.cc;
g++ -c min.cc;
// compile directly
g++ max.cc min.cc test.cpp
// make archive
ar q libMinMax.a max.o min.o
g++ test.cpp -L. -llibMinMax.a
g++ test.cpp -L. -lMinMax

View File

@ -2,11 +2,19 @@
#include "max.hh"
#include "min.hh"
#define LIST_SIZE 5
int min( int a, int b ) {
if ( a < b ) {
return b;
}
return a;
}
int main() {
double dbl_a = 2.31233;
double dbl_b = 91.3239;
double dbl_b = 2.78787;
int list[ LIST_SIZE ] = { 0, 2 ,4 , 6, -9 };
@ -15,16 +23,23 @@ int main() {
std::cout << "max(" << dbl_a << ", " << dbl_b << ") = " << mylib::max(dbl_a, dbl_b ) << std::endl;
std::cout << "min(" << dbl_a << ", " << dbl_b << ") = " << mylib::min(dbl_a, dbl_b ) << std::endl;
std::cout << std::endl;
/*
std::cout << "Int & Doubles" << std::endl;
std::cout << "max(" << list[4] << ", " << dbl_b << ") = " << mylib::max(list[4], dbl_b ) << std::endl;
std::cout << "min(" << list[4] << ", " << dbl_b << ") = " << mylib::min(list[4], dbl_b ) << std::endl;
std::cout << "min(" << list[4] << ", " << list[2] << ") = " << min(list[4], list[2]) << std::endl;
std::cout << std::endl;
*/
std::cout << "List" << std::endl;
//std::cout << "max( list, " << LIST_SIZE << ") = " << mylib::max ) << std::endl;
//std::cout << "min( list, " << dbl_a << ", " << dbl_b << ") = " << mylib::max(dbl_a, dbl_b ) << std::endl;
std::cout << "List = [";
for (int i = 0; i < LIST_SIZE; i++ )
{
std::cout << list[i] << ", ";
}
std::cout << "]" << std::endl;
std::cout << "max( list, LIST_SIZE) = " << mylib::max( list, LIST_SIZE ) << std::endl;
std::cout << "min( list, LIST_SIZE) = " << mylib::min( list, LIST_SIZE ) << std::endl;
return 0;
}

67
ex2/ex2.5/ndim.cpp Normal file
View File

@ -0,0 +1,67 @@
/*
*
*/
#include "iostream"
#define Y_SIZE 3
double* multiply( double ivec[], int N, double mtx[][Y_SIZE] );
int main() {
int Nx = 4;
double ivec[Nx] = {1, 2, 50, 3.141517181920};
double mtx[Nx][Y_SIZE] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
// print vector
std::cout << "Vector = ";
std::cout << "[ ";
for ( int i = 0; i < Nx; i++){
std::cout << ivec[i] << ", " ;
}
std::cout << "]" << std::endl;
// print matrix
std::cout << "Matrix = ";
std::cout << "[";
for ( int i = 0; i < Nx; i++ ){
std::cout << "[ ";
for ( int j = 0; j < Y_SIZE; j++ ){
std::cout << mtx[i][j] << ", " ;
}
std::cout << "]," << std::endl;
}
std::cout << "]" << std::endl;
double * nvec = multiply( ivec, Nx, mtx );
// print new vector
std::cout << "Result = ";
std::cout << "[ ";
for ( int i = 0; i < Y_SIZE; i++){
std::cout << nvec[i] << ", ";
}
std::cout << "]" << std::endl;
delete nvec;
}
double* multiply( double ivec[], int N, double mtx[][Y_SIZE] ){
//allocate the new vector of length N
double * nvec = new double[Y_SIZE];
for ( int j = 0; j < Y_SIZE; j++ ){
for ( int i = 0; i < N; i++ ){
nvec[j] += ivec[i]*mtx[i][j];
}
}
return nvec;
}