Search code examples
arrayscrecursion

Bacteria population glass coverage


#include <stdio.h>
#include <stdlib.h>

int kl = 1;
int bakterie(int n,int arr[n], int poziom, int j)
{
    int suma = 0;
    if (arr[j] == 0)
    {
        suma = suma;
    }
    if (arr[j] == 1)
    {
        suma += poziom;
    }
    if (arr[j] == 2)
    {
        for (int m = 1;m<=4;m++)
        {
            suma+=bakterie(n,arr,poziom/4,kl);
            printf("%d",suma);
            printf(",");
            printf("%d",kl);
            printf("\n");
            kl++;
        }
    }
    return suma;
}
int main() {
    int i=0,last_2=0,poziom=1;
    int arr[99999];
    char temp;
    do {
        scanf("%d%c", &arr[i], &temp);
        if (arr[i] == 2)
        {
            if(last_2<=4)
            {
                poziom = poziom*4;
            }
            last_2 = 0;
        }
        last_2++;
        i++;
    } while(temp != '\n');

    int n0 = bakterie(i,arr,poziom,0);
    int n1 = n0;
    int n3 = poziom;
    int n2 = n3;
    while(n1!=n2)
    {
        if(n1 > n2)
            n1 -= n2;
        else
            n2 -= n1;
    }
    printf("%d",n0/n1);
    printf("/");
    printf("%d",n3/n1);

    return 0;


  return 0;
}

THE TASK:

On a square glass there live bacteria, the way they arrange themselves is dependent on user input:

2 means the bacteria have split the square into 4 smaller squares and the next 4 user input integers are describing how they split (you can have a recursive situation where the user input is 2 2 2 ...)

1 means the bacteria occupy the entire square

0 means there are no bacteria on this square

(You may assume that the user input is correct (it doesn't end early or contain numbers larger than 2))

You must calculate how much of the glass the bacteria cover. The output must be in the form of an irreducible fraction.

EXAMPLES:

Input: 0

Output: 0/1

Input: 2 1 0 1 1

Output: 3/4

Input: 2 0 2 0 1 1 0 2 0 1 0 1 1

Output: 1/2

I've been writing this code for quite some time and I feel very close to finishing the task. I'm having issues with this section:

int kl = 1;
int bakterie(int n,int arr[n], int poziom, int j)
{
    int suma = 0;
    if (arr[j] == 0)
    {
        suma = suma;
    }
    if (arr[j] == 1)
    {
        suma += poziom;
    }
    if (arr[j] == 2)
    {
        for (int m = 1;m<=4;m++)
        {
            suma+=bakterie(n,arr,poziom/4,kl);
            printf("%d",suma);
            printf(",");
            printf("%d",kl);
            printf("\n");
            kl++;
        }
    }
    return suma;
}

At first, I made kl a local variable in the function bakterie and it worked for inputs that would require at most two layers of "recursive depth" so to speak. But I quickly realized that it would mess it up if there was more. So I tried making it a global variable but sadly the program aborts after printing out the first line with suma and kl (I put it there for debugging purposes). I really don't understand why, I hope making a post here will clear things up. Thanks in advance.


Solution

  • This whole thing is mystifying but making kl = 0 instead of 1 and adding kl++ before the suma+=... made it work. I still don't understand any of this.

    This is the final edited code:

    #include <stdio.h>
    #include <stdlib.h>
    int kl = 0;
    int bakterie(int n,int arr[n], int poziom, int j)
    {
        int suma = 0;
        if (arr[j] == 0)
        {
            suma = suma;
        }
        if (arr[j] == 1)
        {
            suma += poziom;
        }
        if (arr[j] == 2)
        {
            for (int m = 1;m<=4;m++)
            {
                kl++;
                suma+=bakterie(n,arr,poziom/4,kl);
            }
        }
        return suma;
    }
    int main() {
        int i=0,last_2=0,poziom=1;
        int arr[99999];
        char temp;
        do {
            scanf("%d%c", &arr[i], &temp);
            if (arr[i] == 2)
            {
                if(last_2<=4)
                {
                    poziom = poziom*4;
                }
                last_2 = 0;
            }
            last_2++;
            i++;
        } while(temp != '\n');
    
        int n0 = bakterie(i,arr,poziom,0);
        if (n0 == 0)
        {
            printf("%d",0);
            printf("/");
            printf("%d",1);
            return 0;
        }
        int n1 = n0;
        int n3 = poziom;
        int n2 = n3;
        while(n1!=n2)
        {
            if(n1 > n2)
                n1 -= n2;
            else
                n2 -= n1;
        }
        printf("%d",n0/n1);
        printf("/");
        printf("%d",n3/n1);
    
        return 0;
    
    
      return 0;
    }