Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void deleteArgFromArr(int * arr, const int size, int argument) {
  6.     int counter = 0;
  7.     for (int i = 0; i < size; i++)
  8.     {
  9.         if (arr[i] == argument )
  10.         {
  11.             for (int k = i; k < size - 1; k++)
  12.             {
  13.                 arr[k] = arr[k + 1];
  14.             }
  15.             counter++;
  16.             break;
  17.         }
  18.     }
  19.     if (counter == 0)
  20.     {
  21.         cout << "No such element exists in your array!" << endl;
  22.     }
  23.     else
  24.     {
  25.         cout << "The element has been deleted successfully!" << endl;
  26.         cout << "New array: " << endl;
  27.         for (int i = 0; i < size - 1; i++)
  28.         {
  29.             cout << arr[i] << " ";
  30.         }
  31.         cout << endl;
  32.     }
  33. }
  34.  
  35. void adElemEnd(int * arr, const int size, int elem) {
  36.     for (int i = 0; i < size; i++)
  37.     {
  38.         if (i == size - 1)
  39.         {
  40.             arr[i] = elem;
  41.             break;
  42.         }
  43.     }
  44.    
  45.     cout << "The new arr is: " << endl;
  46.     for (size_t i = 0; i < size; i++)
  47.     {
  48.         cout << arr[i] << " ";
  49.     }
  50.     cout << endl;
  51. }
  52.  
  53. void elemAtPos(int * arr, const int size, int elem, int atPosition) {
  54.     for (int i = 0; i < size; i++)
  55.     {
  56.         if (i == atPosition)
  57.         {
  58.             for (int j = size; j > i; j--)
  59.             {
  60.                 arr[j] = arr[j-1];
  61.             }
  62.             arr[atPosition] = elem;
  63.             break;
  64.         }
  65.        
  66.     }
  67.     cout << "The new arr is: " << endl;
  68.     for (int i = 0; i < size; i++)
  69.     {
  70.         cout << arr[i] << " ";
  71.     }
  72.     cout << endl;
  73. }
  74.  
  75. int main() {
  76.     //подточка А)
  77.     /*const int num = 10;
  78.     int arr[num] = { 1,2,3,4,5,6,7,8,9,10 };
  79.     cout << "Enter the num you wish to be deleted: ";
  80.     int numToDel;
  81.     cin >> numToDel;
  82.     deleteArgFromArr(arr, num, numToDel);*/
  83.  
  84.     //подточка В)
  85.     /*const int size = 11;
  86.     int arr[size] = { 1,2,3,4,5,6,7,8,9,10 };
  87.     int elem;
  88.     cin >> elem;
  89.     adElemEnd(arr, size, elem);*/
  90.  
  91.     //подточка С)
  92.    
  93.     const int size = 11;
  94.     int myArr[size] = { 1,2,3,4,5,6,7,8,9,10,0};
  95.     int elem, position;
  96.     cin >> elem >> position;
  97.     elemAtPos(myArr, size, elem, position);
  98.  
  99.     //подточка D)
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement