Advertisement
AshfaqFardin

Inserting value in the specific index in the array

Jul 31st, 2021
1,592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int size = 10;
  8.     int arr[10];
  9.    
  10.     for(int i = 0; i < 10; i++){
  11.         arr[i] = i + 1;    
  12.     }
  13.    
  14.     int index = 0;
  15.     int value = 0;
  16.    
  17.     cout << "Before inserting value: \n";
  18.     for(int i = 0; i < size; i++){
  19.         cout << arr[i] << " ";
  20.     }
  21.    
  22.     cout << endl;
  23.     cout << "Enter index number: ";
  24.     cin >> index;
  25.     cout << "Enter index value: ";
  26.     cin >> value;
  27.    
  28.     for(int i = size-2; i >= 0; i--){
  29.         if(i >= index){
  30.             arr[i+1] = arr[i];
  31.         }
  32.     }
  33.     arr[index] = value;
  34.    
  35.     cout << "After inserting value: \n";
  36.     for(int i = 0; i < size; i++){
  37.         cout << arr[i] << " ";
  38.     }
  39.    
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement