So my function works flawlessly up to where it is supposed to print the prime numbers. No matter what numbers i put in, all it outputs is "Primes " nothing else. Im not sure where in this program its not working so any help is appreciated.
/*
File: isprime.cpp
Created by: James Selhorst
Creation Date: 10/22/12
Synopsis:
This program reads in a minimum and maximum integer greater than 1
and returns all primes between the minimum and maximum.
*/
#include <iostream>
#include <cmath>
using namespace std;
// FUNCTION PROTOTYPE FOR read_range
void read_range(int& imin, int& imax);
// FUNCTION PROTOTYPE FOR is_prime
bool is_prime(int& k);
// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
int imin(0), imax(0);
int prime(0);
// Read in range
read_range(imin, imax);
// Print prime numbers
cout << "Primes:";
for (int k = imin; k <= imax; k++)
{
if (is_prime(k))
{
cout << " " << k;
}
}
cout << endl;
return 0;
}
// DEFINE FUNCTION read_range() HERE:
void read_range(int& imax, int& imin)
{
cout<<"Enter minimum and maximum:";
cin>>imin>>imax;
while(imin>imax)
{
cout<<"Error, Minimum must be less then maximum"<<endl;
cout<<"Enter minimum and maximum:";
cin>> imin>>imax;
}
while((imin<2 || imax<2))
{
cout<<"Error, Minimum and maximum must be at least 2"<<endl;
cout<<"Enter minimum and maximum:";
cin>> imin>>imax;
}
}
Ive been working on this program for hours upon hours and this is the last part to finally finish and turn my assignement in super late so please i need someone to point me in the right direction or simply tell me what to change to make it work, im begging at this point.
and if none of them = 0 its true and a prime so i created
What you have said is right, but it doesn't match your code fragment.
You have a loop to check all the numbers, but there is a small thing missing from the logic.
When you get this working, it will be fine up to a limit, because it is a very exhaustive method. Prime numbers become very sparse, so a more efficient method has to be used.
There are lots of examples of algorithms for this on the net.
Can you give me more advice then it needs a counter. I understand if say your min is 9 max is 12 it will start with 9 and pull it into is_prime
i=2, 9%2 !=0
i=3, 9%3=0
i=4 9%4 !-0
this would show false more then once because at points it =0, however when it hits say 11 ,
from 1-10, 11%(1-10) !=0, so it would come out true every time.
should i make a counter saying only it comes out true 1 time then the whole thing is true?
I'm not sure where this code fragment fits into the big picture, is it a function, or a part of something else?
Please use code tags, like this
1 2 3 4 5 6 7 8 9 10 11 12 13
{
int count=0;
for(int i=2; i<=sqrt(k); i++)
{
if (k%i !=0)
count++;
}
if (count==(k-1))
true;
elsefalse;
}
Lines 8 through 12 don't do anything useful. In particular, lines 10 and 12 are pretty much useless. You need to either assign true/false to some variable which you will later test, or if this is meant to be a function, then maybe it needs to return some value?
As for counting the number of times the remainder is non-zero, that's more than is necessary.
Test the opposite condition instead. If the remainder is equal to zero, the number is not prime. At that point you can exit from the for-loop, as any subsequent tests are irrelevant.
// FUNCTION PROTOTYPE FOR read_range
void read_range(int& imin, int& imax);
// FUNCTION PROTOTYPE FOR is_prime
bool is_prime(int& k);
// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
int imin(0), imax(0);
// Read in range
read_range(imin, imax);
// Print prime numbers
cout << "Primes:";
for (int k = imin; k <= imax; k++)
{
if (is_prime(k))
{
cout << " " << k;
}
}
cout << endl;
return 0;
}
// DEFINE FUNCTION read_range() HERE:
void read_range(int& imax, int& imin)
{
cout<<"Enter minimum and maximum:";
cin>>imin>>imax;
while(imin>imax)
{
cout<<"Error, Minimum must be less then maximum"<<endl;
cout<<"Enter minimum and maximum:";
cin>> imin>>imax;
}
while((imin<2 || imax<2))
{
cout<<"Error, Minimum and maximum must be at least 2"<<endl;
cout<<"Enter minimum and maximum:";
cin>> imin>>imax;
}
}
// DEFINE FUNCTION is_prime() HERE:
bool is_prime(int& k)
{
int count=0;
for(int i=2; i<=(k-1); i++)
{
if (k%i !=0)
count++;
}
if (count=(k-2))
true;
}
this is my full code chervil , i cant figure it out, i gave code tags ext, can you please just tell me how to fix this , NO one will just give me the freaking answer and im so tired of looking at this and getting encrypted hints to "figure" it out.
I cant even begin to understand jumper007's code, im in a beginners class using the very basic of coding like cout ext. So when looking at his code and seeing vix and bitset library , the whole thing is useless to me. Can you tell me how to fix the code i have now without encrypted hints, for example, if you see the exact problem... tell me