# Thread: Finding maximum and the number to times entered without using arrays

1. ## Finding maximum and the number to times entered without using arrays

I am trying to find the maximum number using a loop and count the number to times the max was entered. I am having trouble with finding and checking for the maximum and counting the number of times it has been entered. Can someone please give me some advice.

Code:
```#include <stdio.h>

int main()
{
int max;
int i;
int current;
int numbers;
int count = 0;

printf("\nHow many numbers?: ");
scanf("%d", &numbers);

for (i = 0; i < numbers; i++)
{
scanf("%d", current);
if (max > current)
{
max = current;
count++;
}
}

printf("Max: %d     Number of times: %d\n", max, count);
system("PAUSE");
return 0;
}```

2. Your counting seems to be wrong. Every time you discover a new current maximum, you should reset the count, not increment it. But every time the current number is equal to the current maximum, you should increment the count.

3. I did what you said but I still cant seem to get the program to work. Can you please tell me what is wrong with my program.

Code:
```#include <stdio.h>

int main()
{
int max;
int current_max;
int current;
int i;
int numbers;
int count = 0;

printf("\nHow many numbers?: ");
scanf("%d", &numbers);

for (i = 0; i < numbers; i++)
{
scanf("%d", &current);
if (current_max > current)
{
max = current_max;
count = 1;
if (current == current_max)
count++;
}
}

printf("Max: %d     Number of times: %d\n", max, count);
system("PAUSE");
return 0;
}```

4. By current maximum I meant the max variable in your first code.

Note that current_max > current and current == current_max is a contradiction, so you should be moving the comparison of equality to its own branch.

5. I still cant get the program to work. Can you please show me what you mean.

6. I mean something like this:
Code:
```for (i = 0; i < numbers; i++)
{
scanf("%d", &current);
if (max > current)
{
max = current
count = 1;
}
else if (max == current)
{
count++;
}
}```

7. You should check if current > current_max, not current_max > current rfight?

Code:
```		  if (current > current_max)
{
current_max = current;
count = 1;
}
else if (current == current_max)
count++;```

8. thank you for your advice the program is starting to make sense now but i still cannot get it to work. Could you please take a closer look at it.

Code:
```#include <stdio.h>

int main()
{
int max;
int current;
int i;
int numbers;
int count = 0;

printf("\nHow many numbers?: ");
scanf("%d", &numbers);

for (i = 0; i < numbers; i++)
{
scanf("%d", &current);
if (max > current)
{
max = current;
count = 1;
}
else if (max == current)
{
count++;
}
}

printf("Max: %d     Number of times: %d\n", max, count);
system("PAUSE");
return 0;
}```

9. Oh, and one more thing: I notice that max was never initialised. You should either initialise it to the first input entered by the user, or to the maximum value for int (i.e., #include <limits.h> and use INT_MAX).

10. never mind i got it to work. thank you for everything i appreciate everything

11. No problem, but if you ever do decide to change and use INT_MAX, note that I mixed it up: it should be INT_MIN instead.