Problem in finding the mode of a number in an array - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Problem in finding the mode of a number in an array

Posts: 43Member
Hi, I have got a problem as follows:

[b]The mode of an array of numbers is the number [i]m[/i] in the array that is repeated most frequently. If more than one number is repeated with equal maximal frequencies, there is no mode. Write a program that accepts an array of numbers and returns the mode or an indication that the mode does not exist[/b]

For this I have written the following code:

[code]
/* Question C.c (too long to write)
Compiler: Turbo C++ 3 */

#include
#define MAX 5

int mode (int arr[MAX])
{
int i=0, j, t=0, p=1;

while (i!=MAX)
{

for (j=1; j<=MAX; j++)
{
if (arr[i]==arr[j])
t++;
}

if (p<t)
{
p=t;
t=0;
}
/*else if (p==t)
{
return 0;
} */
else
{
t=0;
}

i++;
}
return p;
}

void main()
{
int arr[MAX];
int i, f;

for (i=1; i<=MAX; i++)
{
printf("Enter a number: ");
scanf("%d", &arr[i]);
}

f=mode(arr);

printf("The maximal frequency of the inputted array is: %d", f);

getch();
}
[/code]

However, in case of
[code] if (p==t) [/code]
it always comes true and the return value is termed 0. However, without that statement and the condition that there cannot be a '0 mode' the program works fine.

Abhishek.

• Posts: 9,765Member ✭✭✭
: Hi, I have got a problem as follows:
:
: [b]The mode of an array of numbers is the number [i]m[/i] in the array that is repeated most frequently. If more than one number is repeated with equal maximal frequencies, there is no mode. Write a program that accepts an array of numbers and returns the mode or an indication that the mode does not exist[/b]
:
: For this I have written the following code:
:
: [code]
: /* Question C.c (too long to write)
: Compiler: Turbo C++ 3 */
:
: #include
: #define MAX 5
:
: int mode (int arr[MAX])
: {
: int i=0, j, t=0, p=1;
:
: while (i!=MAX)
: {
:
: for (j=1; j<=MAX; j++)
: {
: if (arr[i]==arr[j])
: t++;
: }
:
: if (p<t)
: {
: p=t;
: t=0;
: }
: /*else if (p==t)
: {
: return 0;
: } */
: else
: {
: t=0;
: }
:
: i++;
: }
: return p;
: }
:
: void main()
: {
: int arr[MAX];
: int i, f;
:
: for (i=1; i<=MAX; i++)
: {
: printf("Enter a number: ");
: scanf("%d", &arr[i]);
: }
:
: f=mode(arr);
:
: printf("The maximal frequency of the inputted array is: %d", f);
:
: getch();
: }
: [/code]
:
: However, in case of
: [code] if (p==t) [/code]
: it always comes true and the return value is termed 0. However, without that statement and the condition that there cannot be a '0 mode' the program works fine.
:
: Abhishek.
:

>>the program works fine.
no it doesn't. It contains some buffer overflows.
>>for (i=1; i<=MAX; i++)
above is example of buffer overflow. arrays are numbered from 0 to but not including MAX. should be coded like this
[code]for (i=0; i < MAX; i++)[/code]

In function mode(), why not just create a 2-dimensional array that contains the value in the original array and the count of the number of times it appears in the original array.
[code]
int mode (int arr[MAX])
{
int totals[2][MAX] = {0};
for(i = 0; i < MAX; i++)
{
// search totals to see if the value is already there. If not, then
// add it. If its already in totals array then just increment
// the counter for it.
int j;
int found = 0;
for(j = 0; found == 0 && j < nItemsAdded; ++j)
{
if( totals[0][j] == arr[j])
{
found = 1; // set flag indicating value found
++totals[1][j]; // bump count
}
}
if( found == 0)
{
}
}

[/code]

After the above is finished, it is a trivel matter to check the totals array for max count.
• Posts: 43Member
Ya that could be it but I was trying to reduce memory usuage. Anyways thanks for the reply...

: : Hi, I have got a problem as follows:
: :
: : [b]The mode of an array of numbers is the number [i]m[/i] in the array that is repeated most frequently. If more than one number is repeated with equal maximal frequencies, there is no mode. Write a program that accepts an array of numbers and returns the mode or an indication that the mode does not exist[/b]
: :
: : For this I have written the following code:
: :
: : [code]
: : /* Question C.c (too long to write)
: : Compiler: Turbo C++ 3 */
: :
: : #include
: : #define MAX 5
: :
: : int mode (int arr[MAX])
: : {
: : int i=0, j, t=0, p=1;
: :
: : while (i!=MAX)
: : {
: :
: : for (j=1; j<=MAX; j++)
: : {
: : if (arr[i]==arr[j])
: : t++;
: : }
: :
: : if (p<t)
: : {
: : p=t;
: : t=0;
: : }
: : /*else if (p==t)
: : {
: : return 0;
: : } */
: : else
: : {
: : t=0;
: : }
: :
: : i++;
: : }
: : return p;
: : }
: :
: : void main()
: : {
: : int arr[MAX];
: : int i, f;
: :
: : for (i=1; i<=MAX; i++)
: : {
: : printf("Enter a number: ");
: : scanf("%d", &arr[i]);
: : }
: :
: : f=mode(arr);
: :
: : printf("The maximal frequency of the inputted array is: %d", f);
: :
: : getch();
: : }
: : [/code]
: :
: : However, in case of
: : [code] if (p==t) [/code]
: : it always comes true and the return value is termed 0. However, without that statement and the condition that there cannot be a '0 mode' the program works fine.
: :
: : Abhishek.
: :
:
: >>the program works fine.
: no it doesn't. It contains some buffer overflows.
: >>for (i=1; i<=MAX; i++)
: above is example of buffer overflow. arrays are numbered from 0 to but not including MAX. should be coded like this
: [code]for (i=0; i < MAX; i++)[/code]
:
:
: In function mode(), why not just create a 2-dimensional array that contains the value in the original array and the count of the number of times it appears in the original array.
: [code]
: int mode (int arr[MAX])
: {
: int totals[2][MAX] = {0};
: int i, nItemsAdded = 0;
: for(i = 0; i < MAX; i++)
: {
: // search totals to see if the value is already there. If not, then
: // add it. If its already in totals array then just increment
: // the counter for it.
: int j;
: int found = 0;
: for(j = 0; found == 0 && j < nItemsAdded; ++j)
: {
: if( totals[0][j] == arr[j])
: {
: found = 1; // set flag indicating value found
: ++totals[1][j]; // bump count
: }
: }
: if( found == 0)
: {
: }
: }
:
: [/code]
:
: After the above is finished, it is a trivel matter to check the totals array for max count.
:

Thank you.
Abhishek
http://freepgs.com/abhishek/pages/home.php