So I need to write a code to create an array of 10 random integers and sort that array. Having done Java, I wanted to do Object Oriented Programming so I could do multiple instances if I desired. Here's my code:
#include <iostream>
usingnamespace std;
class ArrayProject
{
public:
int sort ();
int arrayValue;
int array [10];
};
ArrayProject* myArray = new ArrayProject ();
/**
* Compares the value of two elements in myArray[]
* and sorts them in order by value
*/
void sort ()
{
for(int y = 0; y < 10; y ++)
{
for(int j = 0; j < 10; j ++)
{
if (myArray[j+1].array > myArray[j].array)
{
int temp = myArray[j].arrayValue;
myArray[j].arrayValue = myArray[j+1].arrayValue;
myArray[j+1].arrayValue = temp;
}
}
}
for(int t = 0; t < 10; t ++)
{
cout << myArray[t].arrayValue;
cout << " ";
}
while(1);
}
/**
* The main () method starts everything
*/
int main ()
{
for(int x = 0; x < 10; x ++)
{
myArray[x].arrayValue = rand() % 100;
cout << myArray[x].arrayValue;
cout << " ";
}
cout << " ";
sort (); // Runs the sort () method
}
When I do this, however, I get a big problem and I get some 10 or so digit number that wasnt there originally. How can I make a 'new' array so I can get different random numbers, as well as fix this problem with the sort???
class ArrayProject
{
public:
int sort ();
int array [10];
};
int ArrayProject::sort() // You must use scope resolution to use ArrayProject's sort
{
for (int i = 0; i < 9; i++)
for (int j = i + 1; j < 10; j++)
if (this->array[i] > this->array[j])
//swap
}
int main()
{
ArrayProject *myArray = new ArrayProject(); // There's really no need to be a pointer.
for (int i = 0; i < 10; i++)
myArray->array[i] = randomNumber;
// print contents of pre sorted array
myArray->sort();
// print contents of post sorted array
delete myArray; // You must do this yourself, no garbage collection in STD c++
return 0;
}
To be honest, I'm not sure what I mean. This is the first time I've ever worked with C++ so I'm in the dark about 95% of it. So by the looks of it, it seems like I was on the right track, but I wasn't using a 'legal' pointer and needed -> instead? And just to move a few things around to make it easier?
#include <iostream>
#include <string>
usingnamespace std;
class Name
{
private:
string myName;
public:
Name(string n)
{
myName = n;
cout << "Name Constructed with name: " << myName << '\n';
}
~Name(void) // The desctructor is called when the object goes out of scope
{
cout << myName << " Destructed\n";
}
void speak(void)
{
cout << myName << " says \"Hello\"\n";
}
};
int main(void)
{
cout << "Declaring and Instantiating John:\n";
Name john("John");
cout << "\declaring Mary\n";
Name *mary;
cout << "Instantiating Mary\n";
mary = new Name("Mary");
john.speak();
mary->speak();
std::cout << '\n';
// Objects not going out of scope is a memory leak, and a problem
// In order for Mary to go out of scope, you need this next line
// You can comment it out to see the difference
delete mary;
// Note that Mary's destructor is called first.
// John goes out of scope at the end of main()
return 0;
}