0

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!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Given your input, is the correct answer is "maximum length - 4 occured - 3" or "maximum length - 4 occured - 2" ? It's the "same number" part that i'm afraid of. Do you have to detect that out of the 3 longest sequence, 2 of them are about the same number ? – Tom's Mar 20 '18 at 16:32
  • the occured part refers only to how many times the longest sequence appeared, so whether the sequence is 2 2 2 2 or 4 4 4 4 they're both the longest and they'll both be counted , i hope this cleared your question – Ultraviolence Mar 20 '18 at 16:35
  • why in your example `occured - 3`? – Iłya Bursov Mar 20 '18 at 16:37
  • @IlyaBursov 97 appears 4 times, then 681 appears 4 times, then 97 appears another 4 times. That’s three occurrences of four in a row. – Jonathan Leffler Mar 20 '18 at 16:40
  • i think i didn't explain myself properly. it's "occured - 3" because the longest sequence (4 consecutive same number) uccured 3 times, so while 97 97 97 97 and 681 681 681 aren't the same, they're both the longest sequence and both will be counted, sorry for wording myself badly – Ultraviolence Mar 20 '18 at 16:41
  • If one of the answers solved your problem, you should [accept an answer](https://stackoverflow.com/help/accepted-answer), not put "solved" in the title. – dbush Mar 20 '18 at 17:37
  • @Ultraviolence You are margek as the best the very bad code. – Vlad from Moscow Mar 21 '18 at 11:31

3 Answers3

0

Here is fixed version of your function with some comments:

int num = 0, curr = -1, currCnt = 0, maxCnt = 0, maxCntCnt = 0;
printf("Please enter positive integers: ");
while (num >= 0) {
    if (scanf("%d", &num) != 1) 
        break;
    if (num <= 0)
        break;
    if (num == curr) {
        currCnt++; // new number is continuation of sequence, increase counter
        if (currCnt == maxCnt) {
            maxCntCnt++; // we're reached previously detected max, increase counter
        }
        if (currCnt > maxCnt) {
            maxCnt = currCnt; // we're reached new max, reset counter
            maxCntCnt = 1;
        }
    } else {
        curr = num; // start of new sequence detected, reset counter
        currCnt = 1;
    }
}

printf("\nmaximum length - %d\n", maxCnt);
printf("occured - %d\n", maxCntCnt);
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

We beginners should help each other.:)

For starters take into account that the function shall be declared (when the declaration is also a function definition) like

void MaxLength( void )
                ^^^^^
{
    //...
}

Your function is too complicated.

Here is a solution that I can suggest.

#include <stdio.h>

void MaxLength( void )
{
    size_t max_length = 0, max_count = 0;
    int prev_number = 0;

    size_t n = 0;
    _Bool valid_input;

    printf( "Please enter positive integers: " );

    do
    {
        int number = 0;

        valid_input = scanf( "%d", &number ) == 1 && number > 0;

        if ( !valid_input || number != prev_number )
        {
            if ( max_length < n )
            {
                max_length = n;
                max_count  = 1;
            }
            else if ( valid_input && max_length == n )
            {
                max_count++;
            }

            prev_number = number;
            n = 1;
        }
        else
        {
            ++n;
        }

    } while ( valid_input );

    printf( "\nmaximum length - %zu\n", max_length );
    printf( "occured - %zu\n", max_count ); 
}

int main(void) 
{
    MaxLength();

    return 0;
}

The program output is

Please enter positive integers: 19 19 97 97 97 97 681 681 681 681 97 36 36 36 97 97 97 97 36 -19

maximum length - 4
occured - 3
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

since everybody give the answer, here's mine :

void MaxLength(void)
{
    int inputNumber;
    int previousNumber = -1;
    int currentLen     = 0;
    int maxLen         = 0;
    int nbMaxLen       = 0; 

    printf("Please enter positive integers: ");
    while (scanf("%d", &inputNumber) == 1 && inputNumber > 0) {
        ++currentLen;
        if (inputNumber != previousNumber) {
            if (currentLen == maxLen) {
                ++nbMaxLen;
            } else if (currentLen > maxLen) {
                maxLen = currentLen;
                nbMaxLen = 1;
            }
            currentLen = 1;
        }
        previousNumber = inputNumber;
    }

    if (currentLen == maxLen) {
        ++nbMaxLen;
    } else if (currentLen > maxLen) {
        maxLen = currentLen;
        nbMaxLen = 1;
    }

    printf("maxLen   = %d\n", maxLen);
    printf("nbMaxLen = %d\n", nbMaxLen);
}

Your problem come from there are minor error across the while loop, like the fact that if currentmax == globalmax, then temp1 will not take num1 value, and I don't think this is what is supposed to do.

Globally, we have the same algorithm, but do not hesitate to ask questions if you found something suspicious.

Tom's
  • 2,448
  • 10
  • 22
  • thanks alot for helping, more ideas are always welcome! – Ultraviolence Mar 20 '18 at 17:07
  • @Ultraviolence The function in this post is wrong.:) – Vlad from Moscow Mar 20 '18 at 17:37
  • For what input ? I tested it with your example and some other, and it seemed to work fine (even when no input is given). – Tom's Mar 21 '18 at 08:07
  • @Tom's The code is invalid. It does not process the case when the user presses Ctrl + Z or Crel + d. In this case the last sequence will not be counted. – Vlad from Moscow Mar 21 '18 at 11:31
  • ??? Can you stop trolling please ? Either I really missed something, or it's embarrassing to see a 134k rep guy saying thing like that. First, your function doesn't manage ctrl z / ctrl d. Second, YOUR functon is bugged. If I give "-4" rigth away, it say that the maximum len is 0 and it has occured 1 time. How can something that doesn't exist can have occured one time ???? Finally, managing ctrl z / ctrl d while doing scanf will require more step than you think : it will be OS dependant and a big headache for nothing. The user have to press "enter" to input a number. ctrl d is not "enter" ... – Tom's Mar 21 '18 at 12:44