fork download
  1. //This program demos basic arrays
  2. #include <iostream>
  3. using namespace std;
  4. int CAP = 10;
  5. void printArr(int list[]){
  6. for (int i = 0; i < CAP; i++)
  7. cout << list[i] << " ";
  8. cout<<endl;
  9. }
  10. int main()
  11. {
  12. int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
  13. int i, delIndex, addIndex, newInt = 0;
  14. cout << "Your list is: " << endl;
  15. printArr(list);
  16.  
  17. //Deleting an index
  18. cout << "\nPlease enter index(0 indexed based) to delete from: ";
  19. cin >> delIndex;
  20. for (i = delIndex; i < CAP - 1; i++)
  21. list[i] = list[i + 1];
  22. CAP--;
  23. cout << "The index position you specified has been deleted." << endl;
  24. cout << "The new array is: " << endl;
  25. printArr(list);
  26.  
  27. //Adding an index
  28. cout << "\nNow, please enter the index position(0 indexed based) to add to: " << endl;
  29. cin >> addIndex;
  30. cout << "\nEnter the number to add to the index: " << endl;
  31. cin >> newInt;
  32. for (i = CAP; i > addIndex; i--)
  33. list[i] = list[i-1];
  34.  
  35. list[addIndex] = newInt;
  36. CAP++;
  37. cout << "The number has been added at the specified index position." << endl;
  38. cout << "The new array is: " << endl;
  39. printArr(list);
  40. return 0;
  41. }
Success #stdin #stdout 0s 4268KB
stdin
2

3
9
stdout
Your list is: 
12 34 45 2 8 10 16 180 182 22 

Please enter index(0 indexed based) to delete from: The index position you specified has been deleted.
The new array is: 
12 34 2 8 10 16 180 182 22 

Now, please enter the index position(0 indexed based) to add to: 

Enter the number to add to the index: 
The number has been added at the specified index position.
The new array is: 
12 34 2 9 8 10 16 180 182 22