Advertisement
phamt

Delete Array Item Function

Nov 30th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. //Please fill in the function at the bottom of the file.  (deleteItem)
  2. //DO NOT CHANGE ANYTHING ELSE.
  3. //main has all the code needed to test your function.  Once your function is written, please build and make sure it works fine
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. //constants
  10. const int CAP = 100;
  11.  
  12. //function prototypes
  13. bool openFile(ifstream&);
  14. void readData(ifstream&, int[], int&);
  15. void printData(const int[], int);
  16. void deleteItem(int[], int&, int);
  17.  
  18.  
  19. int main()
  20. {
  21.     ifstream inFile;
  22.     int list[CAP], size = 0;
  23.  
  24.     if (!openFile(inFile))
  25.     {
  26.         cout << "Program terminating!! File not found!" << endl;
  27.         return -1;
  28.     }
  29.     //read the data from the file
  30.     readData(inFile, list, size);
  31.     inFile.close();
  32.     cout << "Data in file:" << endl;
  33.     printData(list, size);
  34.     //delete a few items
  35.     deleteItem(list, size, 20);
  36.     deleteItem(list, size, 210);
  37.     deleteItem(list, size, 110);
  38.     deleteItem(list, size, 220);
  39.  
  40.     printData(list, size);
  41.     //end program
  42.     cin.ignore(100, '\n');
  43.     cout << "Press any key to continue...";
  44.     getchar();
  45.  
  46.     return 0;
  47. }
  48.  
  49. //function to open file
  50. bool openFile(ifstream& inFile)
  51. {
  52.     inFile.open("numbers.txt");
  53.     if (!inFile)
  54.     {
  55.         return false;
  56.     }
  57.     return true;
  58. }
  59.  
  60. //reads the data from the file
  61. void readData(ifstream& inFile, int list[], int& size)
  62. {
  63.     while (!inFile.eof())
  64.     {
  65.         inFile >> list[size++];
  66.     }
  67. }
  68.  
  69. //print the contents of the array
  70. void printData(const int list[], int size)
  71. {
  72.     for (int i = 0; i < size; i++)
  73.     {
  74.         cout << list[i] << endl;
  75.     }
  76.     cout << endl;
  77. }
  78.  
  79.  
  80. //delete an item (num) from the array
  81. //if num is not found, display 'Item Not Found'
  82. void deleteItem(int list[], int& size, int num)
  83. {
  84.     int numFound = 0;
  85.     for (int i = 0; i < size; i++)
  86.     {
  87.         if (list[i] == num)
  88.         {
  89.             for (int j = i; j < (size - 1); j++)
  90.             {
  91.                 list[j] = list[j + 1];
  92.  
  93.             }
  94.             list[size - 1] = NULL;
  95.             size--;
  96.             numFound++;
  97.             break;
  98.         }
  99.    
  100.         // condition if flag isnt greater than 0
  101.         if (numFound == 0) {
  102.             cout << " Item Not Found! " << endl;
  103.         }
  104.     }
  105.     return;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement