In C, I'm only allowed to use stdio.h and I'm not allowed to use arrays.
The question is: I need to write the function "void MaxLegnth()" that inputs integers as long as they're positive, if it inputs a negative number, the inputting part will stop. I need to find the longest sequence of same numbers, and how many times it occurred. For example:
input:
19 19 97 97 97 97 681 681 681 681 97 36 36 36 97 97 97 97 36 -19
output:
maximum length - 4
occurred - 3
I've been struggling with this question and came up with this, but I can't figure out how to fix it:
void MaxLength()
{
int num1, temp, currentmax = 0, countmax = 0, globalmax = 0;
printf("Please enter positive integers: ");
scanf_s("%d", &num1);
temp = num1;
if (num1 > 0)
currentmax++;
while (num1 > 0)
{
scanf_s("%d", &num1);
if (num1 < 0)
{
//currentmax--;
break;
}
if (num1 == temp)
{
currentmax++;
temp = num1;
if (currentmax > globalmax)
globalmax = currentmax;
}
else
{
if (currentmax > globalmax)
{
globalmax = currentmax;
countmax = 1;
currentmax = 1;
temp = num1;
}
if (currentmax < globalmax)
{
currentmax = 1;
temp = num1;
}
if (currentmax == globalmax)
countmax++;
}
}
printf("\ncurrent %d\n", currentmax);
printf("\nglobal %d\n", globalmax);
printf("\ncount %d\n", countmax);
}
Sorry for the huge wall of text. Any help would be highly appreciated!