Advertisement
bensnap

max.cpp

Oct 14th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. // the c-string max() doesn't work yet, but the array max() should pass all tests
  2.  
  3. int max (int a, int b) {
  4. if (a > b)
  5. return a;
  6. else
  7. return b;
  8.  
  9. }
  10.  
  11. int max(int a, int b, int c, int d, int e, int f){
  12. return max( max( max(e,f) , max(c,d)), max(a,b));
  13. }
  14.  
  15. double max (double a, double b){
  16. if (a > b)
  17. return a;
  18. else
  19. return b;
  20. }
  21.  
  22. char max (char a, char b){
  23. if (a > b)
  24. return a;
  25. else
  26. return b;
  27. }
  28.  
  29. int* max (int* A, int aLen, int* B, int bLen){
  30.  
  31. int cLen = max(aLen, bLen);
  32. int* C = new int[cLen];
  33.  
  34. int curMax = 0;
  35.  
  36. for (int i = 0; i < cLen; i++) {
  37. // if both have an index at i, compare the two and get curMax
  38. if (aLen >= i && bLen >= i) {
  39. curMax = max(A[i], B[i]);
  40.  
  41. } else if (aLen >= i && bLen < i) {
  42. curMax = A[i];
  43.  
  44. } else if (bLen >= i && aLen < i) {
  45. curMax = B[i];
  46.  
  47. }
  48. // else curMax = the one that IS still there
  49.  
  50. C[i] = curMax;
  51.  
  52. }
  53.  
  54. return C;
  55.  
  56. }
  57.  
  58. char* max (char* a, char* b) {
  59.  
  60. return a;
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement