I think you have misunderstood your homework.
Your functions, on their own and without being passed parameters, make no sense.
For instance
smallest(), which is probably
findSmallest(), should be passed parameters (the array to be inspected and its size).
So to reiterate, it looks to me that your homework is about writing an
Array class.
You can think of a class as a collection of data (member data) and functions (member functions) that work on that data. Then you do not need to pass any parameters to
findSmallest() because it already knows what data it is working on.
http://www.cplusplus.com/doc/tutorial/classes/
Here's a crude start in that direction:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
using namespace std;
class Array
{
private:
int *p;
int size;
public:
// constructor
Array(): p(NULL)
{
}
// destructor
~Array()
{
delete[] p; // free new[]'d memory
}
// const functions don't change member data
int sizeOfArray() const
{
return size;
}
void getInputs()
{
// your code here
// ask for size, delete[] and new[] p
// then input all elements as you already did
}
int findSmallest() const
{
int min = p[0];
for (int i=1; i < size; i++)
if (p[i] < min)
min = p[i];
return min;
}
};
int main()
{
Array a;
a.getInput();
cout << "size is: " << a.sizeOfArray() << '\n';
cout << "smallest element is: " << a.findSmallest() << endl;
}
|
At this point I suggest you post your homework statement so that we know exactly what you need to do. Because maybe I'm totally wrong, and you're
not supposed to write a class.
If that's the case, let us know, and be sure to read this:
http://www.cplusplus.com/doc/tutorial/functions/