I'll try to keep this short. So my task is to find the last prime number in an array, if there's any. But right now my program assigns any number it wants from the array to the lastPrimeNumber variable. Any ideas?
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL));
int numbers[10] = {0};
int numbersCount = 10;
bool areEven = true;
cout << "Numbers: ";
for (int i = 0; i < numbersCount; i )
{
numbers[i] = rand() % 10 1;
cout << numbers[i] << " ";
}
for (int i = 0; i < numbersCount; i )
{
if (numbers[i] % 2 != 0)
{
cout << "\nNot all numbers are even" << endl;
areEven = false;
break;
}
if (areEven)
{
cout << "\nAll numbers are even" << endl;
break;
}
}
// Looking for the last prime number in an array
int lastPrimeNumber = 0;
bool isPrime = true;
for (int i = 0; i < numbersCount; i )
{
for (int j = 2; j < numbers[i]/2; j )
{
if (numbers[i] % j == 0 || numbers[i] < 2)
{
isPrime = false;
}
}
if(isPrime)
{
lastPrimeNumber = numbers[i];
}
}
cout << endl << lastPrimeNumber;
return 0;
}
Thanks in advance.
CodePudding user response:
Simple fix, change
bool isPrime = true;
for (int i = 0; i < numbersCount; i )
{
to
for (int i = 0; i < numbersCount; i )
{
bool isPrime = true;
In your version once you have set isPrime
to false you never set it back to true again.
CodePudding user response:
For starters this for loop
for (int i = 0; i < numbersCount; i )
{
if (numbers[i] % 2 != 0)
{
cout << "\nNot all numbers are even" << endl;
areEven = false;
break;
}
if (areEven)
{
cout << "\nAll numbers are even" << endl;
break;
}
}
contains a logical error. If the first element of the array is an even value then the program outputs
cout << "\nAll numbers are even" << endl;
that in general is wrong.
Instead you should write for example
for (int i = 0; areEven && i < numbersCount; i )
{
areEven = numbers[i] % 2 == 0;
}
if ( areEven )
{
cout << "\nAll numbers are even" << endl;
}
else
{
cout << "\nNot all numbers are even" << endl;
}
Within these for loops
int lastPrimeNumber = 0;
bool isPrime = true;
for (int i = 0; i < numbersCount; i )
{
for (int j = 2; j < numbers[i]/2; j )
{
if (numbers[i] % j == 0 || numbers[i] < 2)
{
isPrime = false;
}
}
if(isPrime)
{
lastPrimeNumber = numbers[i];
}
}
you do not reset the variable isPrime
before the inner for loop.
And the condition in the for loop is incorrect
for (int j = 2; j < numbers[i]/2; j )
Due to the condition the number 4
can be considered as a prime number.
And moreover this condition
numbers[i] < 2
in this if statement
if (numbers[i] % j == 0 || numbers[i] < 2)
can never evaluate to true.
At least you should write
int lastPrimeNumber = 0;
for (int i = 0; i < numbersCount; i )
{
bool isPrime = not ( numbers[i] < 2 );
for (int j = 2; isPrime && j <= numbers[i]/2; j )
{
if (numbers[i] % j == 0)
{
isPrime = false;
}
}
if(isPrime)
{
lastPrimeNumber = numbers[i];
}
}
A better approach is the following.
int lastPrimeNumber = 0;
for (int i = 0; i < numbersCount; i )
{
bool isPrime = numbers[i] % 2 == 0 ? numbers[i] == 2 : numbers[i] != 1;
for ( int j = 3; isPrime && j <= numbers[i] / j; j = 2 )
{
if (numbers[i] % j == 0)
{
isPrime = false;
}
}
if(isPrime)
{
lastPrimeNumber = numbers[i];
}
}