Passing arrays as function parameters

Well, I have always had problems with arrays and functions together.

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
#include <iostream>
#include <vector>

using namespace std;

struct Massive {
	int a;
	int b;
	int c;
	int d;
	int e;
};

Massive Sort(Massive smth);
int Compare(Massive array[], int N, int current);

int main() {
	int N;
	cout << "Insert an integer: ", cin >> N;
	vector <Massive> array(N);

	for (int i = 0; i < N; ++i)
		array[i] = Sort(array[i]);

	for (int j = 0; j < N; ++j)
		cout << "Equal with massive >>" << j << "<< " << Compare(array[N], N, j) << endl;

	return 0;
}

Massive Sort(Massive smth) {
	int temp[5];
	temp[0] = smth.a, temp[1] = smth.b, temp[2] = smth.c, temp[3] = smth.d, temp[4] = smth.e;
	
	for (int i = 0; i < 4; ++i) {
		if (temp[i] > temp[i + 1]) {
			temp[i] ^= temp[i + 1];
         		temp[i + 1] ^= temp[i];
         		temp[i] ^= temp[i + 1];
		}
	}

	smth.a = temp[0], smth.b = temp[1], smth.c = temp[2], smth.d = temp[3], smth.e = temp[4];
	return smth;
}

int Compare(Massive array[], int N, int current) {
	int counter = 0;
	for (int i = 0; i < N; ++i) {
		if (current != i && array[current].a == array[i].a)
			++counter;
	}
	return counter;
}


Problems with function Compare. Can anyone tell me what the hell I am doing wrong and how I should repair it.

I need it like in four hours or I am dead.
Last edited on
I'm still not sure exactly what you are doing. Your Compare function returns the number of elements with .a equal to the 'current' element, exclusive, occurs. Right?

Anyway, your compare function takes a pointer to array but
 
... Compare( array[n], ... ) ...

passes an element of the array. What you want is a pointer to a specific index:
 
... Compare( &array[n], ... ) ...

I'm not sure what you are doing, but generally such operations want to work across the entire vector/array:
 
... Compare( &array[0], ... ) ...


Hope this helps.
Topic archived. No new replies allowed.