The function that checks that the elements of the line of the matrix - simple numbers in order of decommissioning - are not working correctly



  • He wrote a function that checks that the elements of the row of the matrix are simple numbers in order of loss (availability and not simple numbers are allowed, most importantly, that all simple decommissioning numbers go).

    However, the function is not correct. Why don't you check for an increase in the counter. Why?

    #include <stdio.h>
    #include <malloc.h>
    

    int **a;

    int simple(int n)
    {
    int i;
    for(i=2;i<= n/2;i++)
    if( (n%i)==0 )
    {
    return 0; //не простое число
    }
    return 1; // простое число
    }

    int downsimple(int k, int m)
    {
    int maxdigit, count;
    count = 0;
    for(int j=0; j++; j<m)
    {
    maxdigit= a[k][j];

        for(int i=0; i&lt;m; i++)
        {
            if (1 == simple(a[k][i]))
            {
                count++;
                if(maxdigit &lt; a[k][i])
                {
                    return 0; //не соотвествует условию
                }
            }
        }
    }
    printf("Count %d\n", count);
    if(count &gt; 0) { printf("\vse ok\n");return 1;} //в порядке убывания
    else {return 0;}
    

    }

    int main()
    {
    int n,m,i,j;

    printf("The NUMBER of N:\n");
    scanf("%d", &amp;n);
    
    
    printf("The NUMBER of M:\n");
    scanf("%d", &amp;m);
    a = (int**)malloc(n*sizeof(int*));
    for(i=0; i&lt;n; i++)
    {
        a[i] = (int*)malloc(m*sizeof(int));
            for(j=0; j&lt;m; j++)
            {
                printf("a[%d][%d] = ", i, j);
                scanf("%d", &amp;a[i][j]);
            }
     }
    
    free(a);
    printf("\n\nMemory free!\n");
    return 0;
    

    }



  • Functions downsimple External cycle:

    for(int j=0; j++; j<m)
    

    Usual seal (for(int j=0; j<m; j++) must be.

    maxdigit It's just for the simple numbers to count in the outside cycle, isn't it?

    Well, the internal cycle probably isn't. 0 We have to go through. j+1

    In general, the algorithm sees something like that:

    for (int i=0; i<m; i++) {
       if (is_simple(a[k][i]))
          for (j=i+1; i<m; j++)
             if (is_simple(a[k][j]) && a[k][j] >= a[k][i])
                return 0
    }
    return 1;
    

    P.S. Yes, and see j - external cycle index, and i - internal, somehow weird)

    Delivery with one line passage (with no cycles applied):

    int current;
    // находим первое простое число
    for (current=0; current<m && !is_simple(a[k][current]); current++)
        ;
    for (int i=current+1; i<m; i++)
        if (is_simple(a[k][i])) // не простое число игнорируем
            if (a[k][i] > a[k][current])
                return 0; // следующее больше текущего - ошибка
            else
                current = i; // изменяем текущее
    return 1; // вернет 1 если простых чисел нет вовсе.
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2