Array function calling

closed account (SEwCM4Gy)
For some odd reason, my last function to find the index variable with the largest number is not working. It says the biggest number is 10, and it occurs 10x when it should say 7 and occurs 3x. HELP!

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
#include <iostream>
using namespace std;

int find_count(int a[], int size, int index);
int most_frequent(const int a[], int size, int& frequency);

int main()
{
	int arr[] = { 2, 7, 5, 6, 7, 1, 6, 2, 1, 7 };

	int firstElement = find_count(arr, 10, arr[0]);
	int secondElement = find_count(arr, 10, arr[1]);


	cout << "The first element in array occurs " << firstElement << " times\n";
	cout << "The second element in array occurs " << secondElement << " times\n";

	int freq = 0;
	int best = arr[most_frequent(arr, 10, freq)];
	cout << "The most frequent number in the array is:  "
		<< best << " (" << freq << " x)." << endl;

	system("pause");
	return 0;
}

int find_count(int a[], int size, int index)
{
	int counter = 0;

	for (int i = 0; i < size; i++)
	{
		if (index == a[i])
			counter++;
	}
	return counter;
}

int most_frequent(const int a[], int size, int& frequency)
{
	int max = a[0], index_of_max = 0;

	for (int i = 1; i < size; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
			index_of_max = i;
			frequency++;
		}
	}
	return index_of_max;
}
frequency is only getting updated when the if condition is true. This only happens once right now. If there's a second occurrence of the largest number, the if condition won't be true (e.g. 7 > 7 is not true) and those statements won't run.

1
2
3
4
5
6
		if (a[i] > max)
		{
			max = a[i];
			index_of_max = i;
			frequency++;
		}
closed account (SEwCM4Gy)
I changed it so that the if statement says

1
2
3
4
5
6
if(a[i]>=max)
{
max=a[i};
index_of_max=i;
frequency++;
}


However, it still doesn't work and displays 10 (10x)
Last edited on
This is the output I get running your code with just a change to the one line noted below.



The first element in array occurs 2 times 
The second element in array occurs 3 times 
The most frequent number in the array is: 7 (3 x).


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
#include <iostream>
using namespace std;

int find_count(int a[], int size, int index);
int most_frequent(const int a[], int size, int& frequency);

int main()
{
	int arr[] = { 2, 7, 5, 6, 7, 1, 6, 2, 1, 7 };

	int firstElement = find_count(arr, 10, arr[0]);
	int secondElement = find_count(arr, 10, arr[1]);


	cout << "The first element in array occurs " << firstElement << " times\n";
	cout << "The second element in array occurs " << secondElement << " times\n";

	int freq = 0;
	int best = arr[most_frequent(arr, 10, freq)];
	cout << "The most frequent number in the array is:  "
		<< best << " (" << freq << " x)." << endl;

	system("pause");
	return 0;
}

int find_count(int a[], int size, int index)
{
	int counter = 0;

	for (int i = 0; i < size; i++)
	{
		if (index == a[i])
			counter++;
	}
	return counter;
}

int most_frequent(const int a[], int size, int& frequency)
{
	int max = a[0], index_of_max = 0;

	for (int i = 1; i < size; i++)
	{
		if (a[i] >= max)//just changed here
		{
			max = a[i];
			index_of_max = i;
			frequency++;
		}
	}
	return index_of_max;
}
int firstElement = find_count(arr, 10, arr[0]);
int secondElement = find_count(arr, 10, arr[1]);

arr[x], if x!=0 or 1 , you will get wrong result.

Topic archived. No new replies allowed.