Fienden
Hero
Kort sagt: Varför fungerar inte det här? Jag försöker lagra alla icke-primtal i notPrimes och alla primtal i Primes, sedan mata ut primes. Men, programmet låser sig efter användaren skrivit in ett nummer.
<div class="ubbcode-block"><div class="ubbcode-header">Code:</div><div class="ubbcode-body ubbcode-pre" ><pre>#include <iostream>
#include <vector>
using namespace std;
bool checkForNoPrime(int, vector<int>);
int main()
{
int max = 0;
int prime = 0;
vector<int> notPrimes;
vector<int> primes;
cout << "Enter a number, and I will tell you all prime numbers between 2 and that number." << endl;
cin >> max;
for (int c=2; c<max; c++)
{
if (checkForNoPrime(c, notPrimes))
{
for (int notPrime = c; notPrime <= max; notPrime*2)
{
notPrimes.push_back(notPrime);
}
primes.push_back(c);
}
}
cout << endl;
for (int c=0; c<primes.size(); c++)
{
cout << primes[c] << " ";
}
}
bool checkForNoPrime(int number, vector<int> check)
{
bool rBool = true;
for (int c=0; c < check.size() && rBool == true; c++)
{
if (number==check[c])
{
rBool = false;
}
}
return rBool;
}</pre></div></div>
<div class="ubbcode-block"><div class="ubbcode-header">Code:</div><div class="ubbcode-body ubbcode-pre" ><pre>#include <iostream>
#include <vector>
using namespace std;
bool checkForNoPrime(int, vector<int>);
int main()
{
int max = 0;
int prime = 0;
vector<int> notPrimes;
vector<int> primes;
cout << "Enter a number, and I will tell you all prime numbers between 2 and that number." << endl;
cin >> max;
for (int c=2; c<max; c++)
{
if (checkForNoPrime(c, notPrimes))
{
for (int notPrime = c; notPrime <= max; notPrime*2)
{
notPrimes.push_back(notPrime);
}
primes.push_back(c);
}
}
cout << endl;
for (int c=0; c<primes.size(); c++)
{
cout << primes[c] << " ";
}
}
bool checkForNoPrime(int number, vector<int> check)
{
bool rBool = true;
for (int c=0; c < check.size() && rBool == true; c++)
{
if (number==check[c])
{
rBool = false;
}
}
return rBool;
}</pre></div></div>