-1

This piece of code which works fine it tells you to enter a number, then it puts the number in a for loop and it checks if it's dividable by i, if true it prints not prime if not prints prime.

#include <iostream>
using namespace std;

int main() {
  int x;
  cin >> x;

  bool f = true;
  for (int i = 2; i < x; i++) {
    f = false;
    if (i % x == 0)
      f = true;
    if (f)
      cout << "not primary";
    else
      cout << "primary";
  }
}

but when i convert it to an array like so:

#include <iostream>
using namespace std;

int main() {
  cout << "the number of array:" << endl;
  int n;
  cin >> n;

  cout << "enter them = \n";
  int *p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];

  bool f = true;
  for (int i = 0; i < n; i++)
    for (int j = 2; j < p[i]; j++) {
      f = false;
      if (p[i] % j == 0)
        f = true;
      if (f)
        cout << "This is not a primary number!\n";
      else
        cout << "this is a primary number!\n";
    }

  delete p;
}

it stores just the first number and i get that but how to increment it lets say n =3 so p[3] = {4,6,7}; my question is how tell the compiler in the j condition if (p[0] % j) then(p[1] %j) it seems that it just takes p[0]

Stargateur
  • 24,473
  • 8
  • 65
  • 91

1 Answers1

0

This will work much better

#include <iostream>
using namespace std;

int main() {
  cout << "the number of array:" << endl;
  int n;
  cin >> n;

  cout << "enter them = \n";
  int *p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];

  for (int i = 0; i < n; i++) {
    bool f = false; // we set f to false for each number
    for (int j = 2; j < p[i]; j++) {
      if (p[i] % j == 0) {
        f = true;
        break; // we break the loop if it's a prime number
      }
    }
    if (f)
      cout << p[i] << " is not a primary number!\n";
    else
      cout << p[i] << " is a primary number!\n";
  }

  delete[] p; // Here you forget brackets [], when you use new[] you must use delete[].
}

Doc for delete operator.

I let some problem like int. You should not use signed number for iteration or stock a size. Because you are a beginner, I don't want to confuse you. So I let it.

Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • Signed numbers are the **correct** choice to represent a size, because their arithmetics work in the expected way. Unsigned numbers do not prevent errors caused by negative values, they just make them harder to find because you silently get a huge positive value instead. Please see http://stackoverflow.com/questions/10168079/why-is-size-t-unsigned for a discussion on that topic. And while the `delete[]` at the end is well-intentioned, it would be better to teach `std::vector`. You should never use `new[]`. – Christian Hackl Dec 03 '16 at 10:57
  • @ChristianHackl I really don't agree. C++ can be a system language. You can use new[]. I agree vector would be a good choice, but the OP is obviously a beginner. I think it's better to keep the answer as close as what he try. I don't agree for signed number, the post that you have link offer two answer. I'm for the second – Stargateur Dec 03 '16 at 11:10
  • All high-level facilities of the language, like `std::vector` or `std::unique_ptr`, are implemented in terms of placement new or `new`. You do not need `new[]`, system programming or not. And while you may disagree with the answer about `unsigned`, know that you then also disagree with some of the world's most renowned C++ experts, including Bjarne Stroustrup, Herb Sutter and Scott Meyers. It's certainly ironic that your answer gives the impression that using `int` for sizes is a beginner's error. Most experienced C++ programmers would probably see it the other way round. – Christian Hackl Dec 03 '16 at 11:21
  • thanks a lot it saved me a lot of unnecessary headache. – Arey M Salih Dec 03 '16 at 15:36
  • @ChristianHackl I found, [this](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html) article. "On the other hand, if the variable is defined to wrap around on overflow, then the compiler must assume that the loop is possibly infinite (which happens if N is INT_MAX) - which then disables these important loop optimizations. This particularly affects 64-bit platforms since so much code uses "int" as induction variables." – Stargateur Dec 24 '16 at 12:29