Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: C++  |  size: 2.04 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. int getRandFromInterval(int min, int max);
  9. int max(const int* iArr, size_t size);
  10. int min(const int* iArr, size_t size);
  11.  
  12. int main()
  13. {
  14.         srand(time(0));
  15.  
  16.         int N, M;
  17.  
  18.         cout << "n = ";
  19.         cin >> N;
  20.         cout << "m = ";
  21.         cin >> M;
  22.         int* a = new int[N];
  23.    int* b = new int[M];
  24.  
  25.         // 1. Set values
  26.         for (int i = 0; i < N; i++)
  27.         {      
  28.                 a[i] = getRandFromInterval(100, 999);
  29.         }
  30.         for (int i = 0; i < M; i++)
  31.         {
  32.                 b[i] = getRandFromInterval(100, 999);
  33.         }
  34.  
  35.         // 2. Print arrays
  36.         cout << endl << endl << "A:" << endl;
  37.         for (int i = 0; i < N; i++)
  38.         {
  39.                 cout << a[i] << '\t';
  40.         }
  41.         cout << endl << endl;
  42.         cout << "B:" << endl;
  43.         for (int i = 0; i < M; i++)
  44.         {
  45.                 cout << b[i] << '\t';
  46.         }
  47.         cout << endl << endl;
  48.  
  49.         // 3. Calculate min and max values
  50.         if (N > M)
  51.         {
  52.                 int iMaxA = max(a, N);
  53.                 cout << endl << "Max A = " << iMaxA;
  54.                
  55.                 int iMinB = min(b, M);
  56.                 cout << endl <<"Min B = " << iMinB << endl;
  57.         }
  58.         else
  59.         {
  60.                 int iMinA = min(a, N);
  61.                 cout << endl << "Min A = " << iMinA;
  62.                
  63.                 int iMaxB = max(b, M);
  64.                 cout << endl <<"Max B = " << iMaxB << endl;
  65.         }
  66.        
  67.         // 4. Remove garbage
  68.         delete a;
  69.         delete b;
  70.         a = 0;
  71.         b = 0;
  72.  
  73.         // 5. Return execution to the OS
  74.         return 0;
  75. }
  76. // ============================================================================
  77.  
  78. int getRandFromInterval(int min, int max)
  79. {
  80.         return min + rand() % (max - min + 1);
  81. }
  82.  
  83. // ============================================================================
  84.  
  85. int max(const int* iArr, size_t size)
  86. {
  87.         int max = iArr[0];
  88.  
  89.         for(int i = 0; i < size; i++)
  90.         {
  91.                 if(iArr[i] > max)
  92.                 {
  93.                         max = iArr[i];
  94.                 }
  95.         }
  96.  
  97.         return max;
  98. }
  99.  
  100. // ============================================================================
  101.  
  102. int min(const int* iArr, size_t size)
  103. {
  104.         int min = iArr[0];
  105.  
  106.         for(int i = 0; i < size ;i++)
  107.         {
  108.                 if(iArr[i] < min)
  109.                 {
  110.                         min = iArr[i];
  111.                 }
  112.         }
  113.  
  114.         return min;
  115. }
  116.  
  117. // ============================================================================