Advertisement
Cinestra

Class Challenge

Jan 30th, 2023
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Track {
  5. public:
  6.     Track(int initial);
  7.     void Submit(int enter);
  8.     bool Find(int wanted_number);
  9.     void print(void);
  10. private:
  11.     int array[100];
  12.     int count;
  13. };
  14.  
  15. Track::Track(int initial) {
  16.     for (int i = 0; i < 100; i++)
  17.     {
  18.         array[i] = 0;
  19.     }
  20.  
  21.     array[0] = initial;
  22.     count = 1;
  23. }
  24.  
  25. void Track::Submit(int enter) {
  26.     if (count > 100)
  27.     {
  28.         cout << "Error, array is full (Max is 101 entries)";
  29.     }
  30.  
  31.     else
  32.     {
  33.         array[count] = enter;
  34.         count++;
  35.     }
  36. }
  37.  
  38. bool Track::Find(int wanted_number) {
  39.     for (int i = 0; i < count; i++) {
  40.         if (array[i] = wanted_number) {
  41.             return true;
  42.         }
  43.     }
  44.  
  45.     return false;
  46.  
  47. }
  48.  
  49. void Track::print(void)
  50. {
  51.     for (int i = 0; i < count; i++) {
  52.         cout << array[i] << " ";
  53.     }
  54. }
  55.  
  56. int main()
  57. {
  58.     Track first(7);
  59.     int x1, x2, x3, x4, x5;
  60.     cout << "Enter 5 numbers, press enter between each one.";
  61.     cin >> x1;
  62.     cin >> x2;
  63.     cin >> x3;
  64.     cin >> x4;
  65.     cin >> x5;
  66.     first.Submit(x1);
  67.     first.Submit(x2);
  68.     first.Submit(x3);
  69.     first.Submit(x4);
  70.     first.Submit(x5);
  71.     first.print();
  72.     if (first.Find(20)) {
  73.         cout << "20 is within the dataset.";
  74.     }
  75.     else {
  76.         cout << "20 is not within the dataset";
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement