'C The structure that I have to calculate the number that is repeated the most in an array doesent work
I'm new to programming and I'm facing this problem. The program is supposed to receive a number from 1 to 11. When you receive all the numbers, you have to compare which number is the most repeated and print the most repeated number. But, the values it prints are not what I expected.
Here is my code,
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int limit=2;
int nro=0;
struct CLIENT
{
int num;
};
int main()
{
int nummo[limit];
const int tam=11;
int num_rep=0;
int pos_num=0;
int c=0,n=0;
int number=0;
int num=0;
struct CLIENT Array[limit];
for (nro=0;nro<limit;nro++){
do
{
printf("\n\t number to compare: \n");
printf("\nopcion: ");
scanf("%d",&num);
}
while (num<1 || num>11);
Array[nro].num=num;
}
for (n=0; n<tam; n++)
{
for(c=0; c<nro; c++)
{
number=Array[c].num;
if (number==n+1)
{
nummo[n]=nummo[n]+1;
}
}
}
for (n=0; n<tam; n++)
{
number=nummo[n];
if (number>num_rep)
{
num_rep=number;
pos_num=n;
}
}
printf("\n \t\t\t\t ----------- report ---------------\n");
printf("\n the number most repeated is: %d\n",pos_num);
return 0;
}
this is a code more reproducible, in this I got 8 and the in be 1
this is the error
[
2
This is the expected result:
problem solved https://aticleworld.com/c-program-to-find-the-most-popular-element-in-an-array/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


