In C(notC++ or C#), I am trying to code a program without the use of arrays and strings that asks for values from the user and find out which number was entered the most number of times (mode) and how many times this number was entered (frequency). Stop asking for values once a -1 has been entered Following is my code but it seems to have an error. Can someone fix it.
In C(notC++ or C#), I am trying to code a program without the use of arrays and strings that asks for values from the user and find out which number was entered the most number of times (mode) and how many times this number was entered (frequency). Stop asking for values once a -1 has been entered
Following is my code but it seems to have an error. Can someone fix it.
#include <stdio.h>
int main()
{
int nNum,current,previous,frequency=1,maxFrequency=0,mode;
do
{
printf("Enter a Number: ");
scanf("%d", &nNum);
previous=nNum;
if(nNum==previous)
frequency++;
else
{
if(frequency>=maxFrequency)
{
maxFrequency=frequency;
mode=previous;
}
frequency=1;
}
} while (nNum!=-1);
printf("The mode is %d and its frequency is %d",mode,maxFrequency); //print mode and maxFrequency
return 0;
}
Step by step
Solved in 4 steps with 1 images