Advertisement
wojtas626

[C++] JiMP2 lab2 najwiekszy iloczyn

Mar 8th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int findHighest(int *arr, int length)
  8. {
  9.     int x, y;
  10.  
  11.     x = 0;
  12.     y = 0;
  13.  
  14.     if (length >= 1)
  15.     {
  16.         x = arr[0];
  17.         if (length >= 2)
  18.         {
  19.             y = arr[1];
  20.             if (x > y)
  21.             {
  22.                 int bufor;
  23.                 bufor = x;
  24.                 x = y;
  25.                 y = bufor;
  26.             }
  27.             for (int i = 2; i < length; i++)
  28.             {
  29.                 if(arr[i] > x)
  30.                 {
  31.                     x = arr[i];
  32.                     if (x > y)
  33.                     {
  34.                         int bufor;
  35.                         bufor = x;
  36.                         x = y;
  37.                         y = bufor;
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.         else y = x;
  43.     }
  44.  
  45.     return x*y;
  46. }
  47.  
  48. void fillWithRandomNumbers(int **arr, int length)
  49. {
  50.     for (int i = 0; i < length; i++)
  51.     {
  52.         (*arr)[i] = rand() % 100;
  53.     }
  54. }
  55.  
  56. void showArray(int *arr, int length)
  57. {
  58.     for (int i = 0; i < length; i++)
  59.     {
  60.         cout << arr[i] << " ";
  61.     }
  62. }
  63.  
  64. int main()
  65. {
  66.     int length;
  67.     int *arr;
  68.     cout << "Podaj wymiar tablicy: ";
  69.     cin >> length;
  70.  
  71.     arr = new int[length];
  72.  
  73.     fillWithRandomNumbers(&arr, length);
  74.     showArray(arr, length);
  75.  
  76.     cout << findHighest(arr, length);
  77.  
  78.     delete [] arr;
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement