Functions with Arrays

My functions are suppose to set amount of arrays (sizeOfArray), get user input for each array (getInputs), find the smallest of the arrays entered (findSmallest), and then display it (display). I am doing them in the order i listed. While doing getInputs, i keep getting "getInputs function does not take 0 arguments." How do i go on fixing this and getting the inputs then sending it back to be ready to display?

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

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>

using namespace std;

void smallest(void);
int sizeOfArray(void);
double getInputs(double theArray[], int sizeOfArray);


int main()
{
	smallest();
	sizeOfArray();
	getInputs();

	system("pause");
	return 0;
}

void smallest(void)
{
	int size;
    cout << "How many elements in the array? ";
    cin >> size;

    int* array = new int[size];
}

int sizeOfArray(void)
{
	cout << " ";
	return 0;
}

double getInputs(double theArray[], int sizeOfArray)
{
	double input;
	int i;
	for(i=0; i < sizeOfArray; i++)
	{
		cout << "Enter value for element " << (i) << endl;
		cin >> input;
		theArray[i] = input;
		
	}
	return i;
}
on line 18, you're calling getInputs() without any arguments.
Also, you should find a way of delete[]ing the allocated array.
you mean do this?

getInputs(double theArray[], int sizeOfArray);
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/
Topic archived. No new replies allowed.