Advertisement
adambkehl

Assignment #3 (3/3) - Page 446 #6

Feb 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template <class T>
  6. T maxn(T arrayElements[], T intsInArray) {
  7.     int biggestIndex = 0;
  8.     for (int i = 1; i < intsInArray; i++) {
  9.         if (arrayElements[i] > arrayElements[biggestIndex]) biggestIndex = i;
  10.     }
  11.     return arrayElements[biggestIndex];
  12. }
  13.  
  14. char* maxn(char **arrayElements, int intsInArray) {
  15.     int biggestIndex = 0;
  16.     for (int i = 1; i < intsInArray; i++) {
  17.         if (strlen(arrayElements[i]) > strlen(arrayElements[biggestIndex])) biggestIndex = i;
  18.     }
  19.     return arrayElements[biggestIndex];
  20. }
  21.  
  22. int main(int argc, char **argv) {
  23.  
  24.     int a[] = { 100, 200, 300, 120, 30 };
  25.     cout << maxn(a, 5) << endl;
  26.  
  27.     char **strings;
  28.     strings = new char*[5];
  29.     for (int i = 0; i < 5; i++) {
  30.         strings[i] = new char[12];
  31.     }
  32.     strcpy(strings[0], "Hi");
  33.     strcpy(strings[1], "Hello");
  34.     strcpy(strings[2], "Yes");
  35.     strcpy(strings[3], "Goodbye");
  36.     strcpy(strings[4], "Adios");
  37.  
  38.     cout << maxn(strings, 5) << endl;
  39.  
  40.     cin.get(), cin.get();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement