Advertisement
SteelK

Untitled

Apr 21st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int one_max(int *in_mas, size_t in_dlina) {
  8.     int a=0,max=0,b=0;
  9.     max=in_mas[0];
  10.     for ( int i = 1; i < in_dlina; i++) {
  11.         a=in_mas[i];
  12.         if (a>max) {
  13.             max=a;
  14.             b=i;
  15.         }
  16.     }
  17.     return b;
  18. }
  19.  
  20.  
  21. void mascin(int *in_mas, unsigned int size)
  22. {
  23.     cout << "Введите каждый элемент массива" << endl;
  24.     for (unsigned int i = 0; i < size; i++)
  25.     {
  26.         cout << "mas[" << i << "] = ";
  27.         cin >> in_mas[i];
  28.     }
  29. }
  30.  
  31.  
  32. void masrand(int *in_mas, unsigned int size)
  33. {
  34.     for (unsigned int i = 0; i < size; i++)
  35.     {
  36.         in_mas[i] = rand() % 10 - 5;
  37.         cout << "mas[" << i << "] = " << in_mas[i] << endl;
  38.     }
  39. }
  40.  
  41.  
  42. int* creat_mas(unsigned int size)
  43. {
  44.     int *mas = new int[size];
  45.     char answer;
  46.     cout << "Вводить ли значения элементов массива(1) или задавать их рандомно(2)? (1/2)" << endl;
  47.     while (true)
  48.     {
  49.         cin >> answer;
  50.         if (answer == '1' || answer == '2')
  51.             break;
  52.         else
  53.             cout << "Ошибка, повторите ввод...\n";
  54.     }
  55.  
  56.     if (answer == '1')
  57.         mascin(mas, size);
  58.     else
  59.         masrand(mas, size);
  60.     return mas;
  61. }
  62.  
  63.  
  64. void newmas(int*& mas, unsigned int& size) {
  65.  
  66.     cout << "Введите длину массива..." << endl;
  67.     cin >> size;
  68.     while (!size)
  69.     {
  70.         cout << "Ошибка, введите повторно..." << endl;
  71.         cin >> size;
  72.     }
  73.     mas = creat_mas(size);
  74. }
  75.  
  76. int main() {
  77.     setlocale(0, "");
  78.     srand(time(NULL));
  79.     unsigned int size;
  80.     int* mas;
  81.     newmas(mas, size);
  82.     cout << "Номер первого максимального элемента = " <<one_max(mas, size) << endl;
  83.     delete[] mas;
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement