Hi I need to find the mode and I'm just not getting the right output for the mode. The program is supposed to find the mode and display it if it exists and if it doesn't exist it's supposed to display -1. Any help will be very much appreciated. Thanks. Here is what I have:

#include <iostream>
using namespace std;

void findMode(int *array, int size, int large);

int main()
{
	int size;
	int *array;
	int large=0;
	cout<<"Please enter the number of elements you want   to process: ";
	cin>> size;
	
	array = new int[size];
	
	findMode(array,size,large);
	

	for (int count=0; count<size;count++)
	{
		cout<<"Please enter value #"<<(count+1)<<": ";
		cin>> array[count];
	}

	return 0;
}

void findMode(int *array, int size, int large)
{
	int *array2;
	int count;
	int x;
	int mode = 0;
	int highest = 0;

	array2 = new int[large+1];

	for (count = 0; count < large+1; count++)
		array2[count] = 0;

	for (count = 0; count < size; count++)
	{
		x = array[count];
		array2[x]++;
	}

	for (count = 0; count < large+1; count++)
	{
		if (array2[count] > highest)
		{
			highest = array2[count];
			mode = count;
		}
		
	}
	cout << "The mode is: " << mode << endl;
}

What doesn't work?

new doesn't set the allocated memory to 0, could that be a problem?

Be a bit more specific please.

okay I found someone else's post and the program is working. Does it look right?

#include <iostream>
using namespace std;

int findMode(int *, int);
const int constant= 100;
int main()
{
	int size;
	int *array;
	int mode=0;
	cout<<"Please enter the number of elements you want to process: ";
	cin>> size;
	
	array = new int[size];
	
	 
	  

	for (int count=0; count<size;count++)
	{
		cout<<"Please enter value #"<<(count+1)<<": ";
		cin>> array[count];
	}
	
	mode = findMode(array, size);
    cout << "The mode is " << mode ;
	return 0;
}

int findMode(int *array, int size)
{
    int mode = -1, position = 0, highest;
    int frequency[constant];

    for(int i = 0; i < size; i++)        //initailize all frequencies to 0
        *(frequency + i) = 0;

    
    for (int k = 0; k < size; k++)
    {
        for (int i = 1; i < size; i++)
        {
            if (*(array + k) == *(array + i) && &*(array + k) != &*(array + i))  //avoid counting itself in the scan
                *(frequency + k) += 1;                 //increment the frequency of current number
        }
    }
    
    highest = *(frequency + 0);
    for (int count = 1; count < size; count++)    // get position of highest number
    {
        if (*(frequency + count) > highest)
        {
            highest = *(frequency + count);
            position =+ count;                //get postion in frequency array, to put into numbers array
        }
    }

    for(int i = 0; i < size; i++)
        if(*(frequency + i) != *(frequency + (i + 1)) && (i + 1) < size)
        {
            mode = *(array + position);
            return mode;    //returns number that occurs most 
        }
    return mode;        //returns -1 if all same frequency
}

Sure, it looks fine, but you need to write it yourself if you want to learn C++, not somebody else.

I mean, do you know what this &*(array + k) does? (It looks rather redundant...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.