How to account for the number of primes in a matrix?

Asked

Viewed 477 times

-1

I’m inserting random numbers into a matriz[10][5], where within it I count the quantity only of prime numbers.

But I’m having difficulty extracting with the counter only the prime numbers.

int divisores = 0;
int qtd = 0;

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            for (int c=1; c<=matriz[i][j]; c++)
            {
                if (matriz[i][j] % c == 0)
                {
                    divisores++;
                }
            }
        }

        if (divisores == 2)
        {
            qtd++;
        }
    }

    cout << qtd << endl;
  • Welcome to Sopt. Dude, what’s your question?

  • Thank you. .

  • You shouldn’t define divisores as 0 for each new value checked? In fact, why not create a function of its own to check if a number is prime?

  • I’m having trouble doing this. And my intention is not to use functions, if you can help me without using function thank you.

  • Note that you need to reset the divisor variable before testing each element of the matrix. This is before the loop: for (int c=1; c<=matrix[i][j]; c++). Your if test (splitters == 2) must also be inside the loop of the variable j and not after such a loop.

3 answers

2

Before developing any algorithm you need analyse the situation, with this we need to know first following:

What are prime numbers?

A prime number is a natural number greater than 1, whose only positive divisors are 1 and itself. All others are called composite numbers.

What characterizes a prime number ?

Aggregating the knowledge of the above question, the unique even prime number is 2, all the others are odd. So with this we know of a general form that the primes are not all odd numbers and by exception the only even number 2.

   #include <iostream>
#include <stdlib.h>

using namespace std;
#define LINHA 10
#define COLUNA 5

int main() {
    int matriz[LINHA][COLUNA];
    int number = 0;

    // ADICIONA NÚMEROS ALEATÓRIOS
    for(int l = 0; l < LINHA; l++) {
        for(int c = 0; c < COLUNA; c++) {
            matriz[l][c] = (rand() % 1000);
        }
    }

    //PERCORRE TODA A MATRIZ BIDIMENSIONAL, PROCURANDO OS NÚMEROS PRIMOS
     for(int l = 0; l < LINHA; l++) {
        for(int c = 0; c < COLUNA; c++) {
            number = matriz[l][c];

            // UTILIZANDO DIVISÃO SUCESSIVA DE 2 ATÉ O NÚMERO ENCONTRADO NA MATRIZ
            // SEGUINDO O CONCEITO DE NÚMEROS PRIMOS, ONDE:
            // 1)Um número primo é um número natural maior do que 1
            // 2) Únicos divisores positivos são 1 e ele mesmo
             for(int p = 2; p <= number; p++) {
                if(number%p == 1)
                    cout << matriz[l][c] << ": EH PRIMO E SE ENCONTRA NA LINHA: " << l << " e na COLUNA: " << c << endl;
            }
        }
    }

    return 0;
}

And the OUTPUT will be: inserir a descrição da imagem aqui

NOTE:

Just need correct the repetitions of OUTPUT terminal.

  • Your test to check if a number is prime ( if(number == 2 || restDivision == 1)) is wrong. Take for example the number 9 that will be erroneously identified as prime. Note that with "even single prime number is 2, all others are odd" you cannot conclude that all odd numbers are prime.

  • @anonimo thanks for the feedback! It’s fixed.

0

Correcting your check if the number is prime.

int divisores = 0;
int qtd = 0;
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 5; j++) {
        divisores = 0;
        for (int c=2; c<=matriz[i][j]/2 && divisores==0; c++) {
                if (matriz[i][j] % c == 0) {
                    divisores++;
                }
        }
        if (divisores == 0) {
            qtd++;
        }
    }
}
cout << qtd << endl;

0

I would like to thank you all for your support !

Follow code solved.

include include include

using namespace Std; defines LINE 10 defines COLUMN 5

int main() { int matrix[ROW][COLUMN]; int number = 0; srand(time(0));

// ADICIONA NÚMEROS ALEATÓRIOS
for (int l = 0; l < LINHA; l++) {
    for (int c = 0; c < COLUNA; c++) {
        matriz[l][c] = rand() % 9 + 5;
    }
}
int qtd = 0;
//PERCORRE TODA A MATRIZ BIDIMENSIONAL, PROCURANDO OS NÚMEROS PRIMOS
for (int l = 0; l < LINHA; l++) 
{
    for (int c = 0; c < COLUNA; c++)
    {
        number = matriz[l][c];
        cout << matriz[l][c] << "   ";
    }
        // UTILIZANDO DIVISÃO SUCESSIVA DE 2 ATÉ O NÚMERO ENCONTRADO NA MATRIZ
        // SEGUINDO O CONCEITO DE NÚMEROS PRIMOS, ONDE:
        // 1)Um número primo é um número natural maior do que 1
        // 2) Únicos divisores positivos são 1 e ele mesmo
    for (int p = 2; p <= number; p++)
    {
        if (number % p == 1)
        {
            qtd++;
        }
    }
}
cout << endl;
cout << qtd << endl;

system("pause");

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.