SHARE
TWEET

Untitled

a guest May 17th, 2017 212 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T>
  5. T sum(T iA, T iB)
  6.   {
  7.   return iA + iB;
  8.   }
  9. template<class T>
  10. T prod(T iA, T iB)
  11.   {
  12.   return iA * iB;
  13.   }
  14. template<class T>
  15. T minus(T iA, T iB)
  16.   {
  17.   return iA - iB;
  18.   }
  19. template<class T>
  20. void matrixes(T *tResult[5], int& tElements)
  21.   {
  22.   T (*fct[3])(T a, T b) = {sum, minus, prod};
  23.   cout << "How many elements do you wish to process? > ";
  24.   cin >> tElements;
  25.  
  26.   for (int i = 0; i < 5; i++) {
  27.     tResult[i] = new T[tElements];
  28.     }
  29.  
  30.   cout << "Enter two elements on each line!" << endl;
  31.   for (int i = 0; i < tElements; i++) {
  32.     cout << "Enter 2 elements: > ";
  33.     cin >> tResult[0][i] >> tResult[1][i];
  34.     cin.clear();
  35.     }
  36.  
  37.   for (int i = 0; i < 3; i++) {
  38.     for (int j = 0; j < tElements; j++) {
  39.       tResult[i+2][j] = fct[i](tResult[0][j], tResult[1][j]);
  40.       }
  41.     }
  42.   }
  43.  
  44. int main()
  45.   {
  46.   double *d[5];
  47.   int iElements = 0;
  48.   char cOperator[] = "+-*";
  49.  
  50.   matrixes(d, iElements);
  51.   for (int j = 0; j < 3; j++) {
  52.     for (int i = 0; i < iElements; i++) {
  53.       cout << setw(3) << d[0][i] << cOperator[j] << setw(3) << d[1][i] << " = " << setw(4) << d[j + 2][i] << endl;
  54.       }
  55.     }
  56.   // free allocated resources
  57.   for (int i = 0; i < 5; i++) {
  58.     delete [] d[i];
  59.     d[i] = NULL;
  60.     }
  61.  
  62.   system("pause");
  63.   return 0;
  64.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top