so i am new to C+ and i need help understanding and doing an assignment.
Here is the assignment:
Create a class called SortArray;
Your class has a property for an array of size 10. It also has functions or methods. Initialize the array to a set of random integers. Write a function called moveUP that takes the number with the lowest value and moves it to the top of the array (position 0).
It has it written out pretty well for you. Create a class that has an array member of size 10. Either init the array in the constructor or write a function to do it. Then write a function that finds the smallest element of the array, and swap places with the 0th element.
#include "iostream"
#include <ctime>
#include <cstdlib>
using std::cout;
using std::endl;
///Sort Array Class
class SortArray
{
private:
int randomArrayOfTen[10];
public:
void MoveUp();
void OutputNumbers();
void GenerateRandomNumbers();
};
int main()
{
SortArray cSort;
srand(time(0));
///Gets ten random numbers from 1 through 100
cSort.GenerateRandomNumbers();
cout<<"The numbers before they are sorted"<<endl;
///Outputs The Numbers Before Sorting Them
cSort.OutputNumbers();
cout<<endl<<endl;
///Sorts Numbers
cSort.MoveUp();
cout<<"The numbers after they have been sorted"<<endl;
///Outputs Number After They Have Been Sorted
cSort.OutputNumbers();
cout<<endl;
return 0;
}
void SortArray::MoveUp()
{
int placeHolder;
for(int g=0; g<10; g++)
{
///Since I am comparing array positions next to eachother I initialize the variable numberAbove to one higher than g
for(int numberAbove=g+1; numberAbove<10; numberAbove++)
{
///If the number lower in array position is higher in value than number higher in array position...proceed to switch
if(randomArrayOfTen[g] > randomArrayOfTen[numberAbove])
{
///Puts Lesser Value Into A Placeholder
placeHolder = randomArrayOfTen[numberAbove];
///Puts Bigger Value In Upper Position
randomArrayOfTen[numberAbove] = randomArrayOfTen[g];
///Puts Lesser Value in Lower Position
randomArrayOfTen[g] = placeHolder;
}
}
}
}
void SortArray::GenerateRandomNumbers()
{
for(int i=0; i<10; i++)
{
randomArrayOfTen[i] = rand() % 100 + 1;
}
}
void SortArray::OutputNumbers()
{
for(int j=0; j<10; j++)
{
cout<<randomArrayOfTen[j]<<endl;
}
}