Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream> // used for input output
- #include<stdlib.h> // used for the random number generator
- #include<time.h> // used for seeding the random number generator
- using namespace std;
- class ArrayStuff
- {
- #define MAX 20 // constant
- // declare the public variables and functions
- public:
- int someArray[MAX]; // this is the array
- void seedRandom(); // this seeds the random number generator
- ArrayStuff(); // this is the constructor
- void printArray(); // function prints the array to the output panel
- void printReverseArray(); // function prints reverse array to the output panel
- int minimum();
- int maximum();
- int sum();
- void swap (int, int);
- int minPosition (int);
- void sort();
- };
- void seedRandom(unsigned int uSeed = 0)
- {
- if (uSeed == 0)
- uSeed=(unsigned)time(NULL);
- srand(uSeed);
- }
- ArrayStuff::ArrayStuff() // constructor
- {
- someArray[MAX];
- for ( int i = 0; i < MAX; i ++ )
- someArray[i] = rand()%99 + 1; // returns random integers 1-99
- }
- void ArrayStuff::printArray() // prints the array to the panel
- {
- for( int i = 0; i < MAX; i++ )
- cout << someArray[i] << " ";
- cout << endl;
- } // end printArray
- void ArrayStuff::printReverseArray()// prints the array to the panel
- {
- for( int i = MAX -1; i >= 0; i-- )
- cout << someArray[i] << " ";
- cout << endl;
- }// end printReverseArray()
- int ArrayStuff::minimum() // determines the smallest number in array
- {
- int smallest = someArray[0];
- for ( int i = 1; i < MAX; i++)
- if (someArray [i] < smallest)
- smallest = someArray[i];
- return smallest;
- } // end minimum()
- int ArrayStuff::maximum() // determines the largest number in the array
- {
- int largest = someArray[0];
- for ( int i = 1; i < MAX; i++)
- if (someArray [i] > largest)
- largest = someArray[i];
- return largest;
- }// end maximum()
- int ArrayStuff::sum() // int ArrayStuff::sum()
- {
- int total = someArray[0];
- for (int i = 1; i< MAX; i++)
- total += someArray[i];
- return total;
- }
- // end sum()
- void ArrayStuff::swap (int, int) // void arrayStuff::swap( int x, int y )
- {
- int temp = someArray[1];
- someArray[1] = someArray [5];
- someArray[5] = temp;
- } // end swap()
- int ArrayStuff::minPosition (int) // int arraystuff::minPosition( int start )
- {
- int min = 0;
- for (int i = 0 + 1; i < MAX; i++)
- if ( someArray [i] < someArray [min] )
- min = i;
- return min;
- }
- // end minPosition
- void ArrayStuff::sort() // void arrayStuff::sort()
- {
- int j;
- for ( int i = 0; i < MAX - 1; i++)
- {
- j = minPosition (i);
- swap (i, j);
- }
- }
- //end sort()
- void main()
- {
- seedRandom(); // call the seedRandom function
- ArrayStuff testArray; // create an instance of arrayStuff
- testArray.printArray(); // call the printArray function
- testArray.printReverseArray(); // call the printReverseArray
- cout << "The smallest element: " << testArray.minimum() << endl;
- cout << "The largest element: " << testArray.maximum() << endl;
- cout << "The sum of the elements: " << testArray.sum() << endl;
- testArray.printArray();
- testArray.swap(1, 5);
- testArray.printArray();
- int foo = testArray.minPosition(0);
- cout << "The index of the smallest value in the array: " << foo << endl;
- testArray.sort();
- testArray.printArray();
- } // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement