Basic usage of TNT library #1


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #include <iostream>
  2.  
  3. /*
  4.  Here we use the TNT library which contains headers for 1D,2D & 3D arrays supporting both C and FORTRAN
  5.  style arrays. The classes are templated allowing any array types. Moreover the classes support overloaded
  6.  operators for common operations
  7. */
  8. #include "tnt.h" //this is the only necessary header to use the entire library. However it is possible to only use the
  9. //needed headers depending on the desired functionality.
  10.  
  11. int main (int argc, char * const argv[]) {
  12. /* TNT::Stopwatch */
  13. TNT::Stopwatch timer1; //using TNT's 'Stopwatch' class to create a timer
  14. timer1.start(); //using the 'start' function of the timer to start counting time
  15.  
  16.  
  17. //use TNT's 'Array2D' class to create 3 30x30 arrays of which the first two are filled with the
  18. //value '2.0' while the 3rd is full of zeros
  19. TNT::Array2D<int> num(5,5,2.0);
  20. TNT::Array2D<int> num2(5,5,2.0);
  21. TNT::Array2D<int> num3(5,5,0.0);
  22.  
  23. std::cout<<num[0][0]<<"n"; //access a specific value in the matrix
  24.  
  25. num3=num2*num; //element-wise multiplication of the two arrays using the overloaded '*' operator
  26.  
  27. std::cout << num3 <<"n"; //using the overloaded operator '<<' to print the dimensions and contents of the array
  28.  
  29. //using the 'matmult' function to multiply two matrices and then we print the result
  30. std::cout << TNT::matmult(num,num2) <<"n";
  31.  
  32. std::cout<< num.subarray(1, 3, 1, 3); //using the 'subarray' function we print a part of the 'num' array
  33.  
  34. /* TNT::Stopwatch */
  35. //using the 'stop' function of the timer to stop counting. This function returns the time
  36. //elapsed from the call of the 'start' function in seconds.
  37. std::cout << timer1.stop()<<"n";
  38.  
  39.  
  40.  
  41.  
  42. return 0;
  43. }
  44.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.