Advertisement
emaansahmed

Assignment #6

Apr 26th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include<iostream> // used for input output
  2. #include<stdlib.h> // used for the random number generator
  3. #include<time.h> // used for seeding the random number generator
  4.  
  5. using namespace std;
  6.  
  7. class ArrayStuff
  8.  
  9. {
  10.  
  11. #define MAX 20 // constant
  12.  
  13.     // declare the public variables and functions
  14. public:
  15.         int someArray[MAX]; // this is the array
  16.         void seedRandom();  // this seeds the random number generator
  17.         ArrayStuff();       // this is the constructor
  18.         void printArray();  // function prints the array to the output panel
  19.         void printReverseArray(); // function prints reverse array to the output panel
  20.         int minimum();
  21.         int maximum();
  22.         int sum();
  23.         void swap (int, int);
  24.         int minPosition (int);
  25.         void sort();
  26.  
  27. };
  28.  
  29. void seedRandom(unsigned int uSeed = 0)
  30.     {
  31.         if (uSeed == 0)
  32.             uSeed=(unsigned)time(NULL);
  33.         srand(uSeed);
  34.  
  35.     }
  36.  
  37. ArrayStuff::ArrayStuff()  // constructor
  38.     {
  39.     someArray[MAX];
  40.     for ( int i = 0; i < MAX; i ++ )
  41.             someArray[i] = rand()%99 + 1; // returns random integers 1-99
  42.     }
  43.  
  44. void ArrayStuff::printArray() // prints the array to the panel
  45.     {
  46.     for( int i = 0; i < MAX; i++ )
  47.             cout << someArray[i] << " ";
  48.     cout << endl;
  49.     }   // end printArray
  50.  
  51. void ArrayStuff::printReverseArray()// prints the array to the panel
  52.     {
  53.     for( int i = MAX -1; i >= 0; i-- )
  54.             cout << someArray[i] << " ";
  55.     cout << endl;
  56.     }// end printReverseArray()
  57.  
  58. int ArrayStuff::minimum() // determines the smallest number in  array
  59. {
  60.     int smallest = someArray[0];
  61.     for ( int i = 1; i < MAX; i++)
  62.         if (someArray [i] < smallest)
  63.             smallest = someArray[i];
  64.     return smallest;
  65.  
  66.  
  67. } // end minimum()
  68.  
  69. int ArrayStuff::maximum() // determines the largest number in the array
  70. {
  71.     int largest = someArray[0];
  72.     for ( int i = 1; i < MAX; i++)
  73.         if (someArray [i] > largest)
  74.             largest = someArray[i];
  75.     return largest;
  76.  
  77.  
  78. }// end maximum()
  79.  
  80. int ArrayStuff::sum()   // int ArrayStuff::sum()
  81. {
  82. int total = someArray[0];
  83. for (int i = 1; i< MAX; i++)
  84.     total += someArray[i];
  85. return total;
  86. }
  87. // end sum()
  88.  
  89. void ArrayStuff::swap (int, int)    // void arrayStuff::swap( int x, int y )
  90. {
  91.     int temp = someArray[1];
  92.     someArray[1] = someArray [5];
  93.     someArray[5]  = temp;
  94. }   // end swap()
  95.  
  96. int ArrayStuff::minPosition (int)   // int arraystuff::minPosition( int start )
  97. {
  98.     int min = 0;
  99.         for (int i = 0 + 1; i < MAX; i++)
  100.             if ( someArray [i] < someArray [min] )
  101.                 min = i;
  102.  
  103.     return min;
  104. }
  105. // end minPosition
  106.  
  107. void ArrayStuff::sort() // void arrayStuff::sort()
  108. {  
  109.     int j;
  110.     for ( int i = 0; i < MAX - 1; i++)
  111.     {
  112.         j = minPosition (i);
  113.  
  114.     swap (i, j);
  115.     }
  116. }
  117. //end sort()
  118.  
  119. void main()
  120. {
  121.     seedRandom();           // call the seedRandom function
  122.     ArrayStuff testArray;   // create an instance of arrayStuff
  123.     testArray.printArray(); // call the printArray function
  124.     testArray.printReverseArray(); // call the printReverseArray
  125.     cout << "The smallest element: " << testArray.minimum() << endl;
  126.     cout << "The largest element: " << testArray.maximum() << endl;
  127.     cout << "The sum of the elements: " << testArray.sum() << endl;
  128.     testArray.printArray();
  129.     testArray.swap(1, 5);
  130.     testArray.printArray();
  131.     int foo = testArray.minPosition(0);
  132.     cout << "The index of the smallest value in the array: " << foo << endl;
  133.     testArray.sort();
  134.     testArray.printArray();
  135.  
  136. } // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement