avr39ripe

cppMaxWithPointers

Aug 20th, 2021
866
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. int max(int* ptrA, int* ptrB)
  4. {
  5.     return *ptrA > *ptrB ? *ptrA : *ptrB;
  6. }
  7.  
  8. int main()
  9. {
  10.     int numA{ 10 };
  11.     int numB{ 20 };
  12.  
  13.     int* ptrA{ &numA };
  14.     int* ptrB{ &numB };
  15.  
  16.     int maxVal{ *ptrA > *ptrB ? *ptrA : *ptrB };
  17.  
  18.     std::cout << "Enter numA:\n";
  19.     std::cin >> numA;
  20.  
  21.     std::cout << "Enter numB:\n";
  22.     std::cin >> numB;
  23.  
  24.     maxVal = max(ptrA, ptrB);
  25.  
  26.     std::cout << "Max = " << maxVal << '\n';
  27.  
  28.     return 0;
  29. }
RAW Paste Data