prime number functions- help

My teacher has assigned us to write a function that can tell if a number is prime or not, we already wrote a program that allows a user to enter a number and the program returns all the numbers up to that number that are prime. He says to use that as a basis for this assignment. He also gave us a main () function to use, I'm really lost and could use some help writing this function so if anyone can help I would really appreciate it
What is that code ? How does your other program determine if one of those numbers less than n is prime ?
the main() the teacher gave is this: int main()
{
int counter=0;
int current_number=2;
std::cout << "First 1000 prime numbers" << std::endl;
while(counter < 1000)
{
if(prime_number(current_number)==true)
{
std::cout << current_number << std::endl;
counter++;
}
current_number++;
}
return 0;
}



and my last assignment was this:
#include<iostream>
using namespace std;
int main()
{
int n;
int i;
bool is_prime=true;

cout << "Enter a number greater than two" << endl;
cin >> n;

for(i=2;i<n;i++)
{


for(int j=2;j<i;j++)
{
if(i%j==0)
is_prime=false;

}
if(is_prime==true)
cout<< i << "is prime" << endl;
is_prime=true ;
}



Hi there ! Use code tags pls :D

To be clear, you are to write a program that determines the first 1000 primes by using a function to test candidates for primes ?

You have everything you need here. The problem seems to be just extracting what the function is.

Remember that a function has the following parts:
return type - what comes back (like: void, bool, int)
parameters - what datatype goes in and how many go in - the function needs to know what to expect
prototype - this is like a header that keeps track of the name/return type/parameters
definition - identical to prototype except this has the implementation

So, based on this function call you should be able to extract what each parts of prime_number(current_number) does:
if(prime_number(current_number)==true)
What is it returning - hint: it's being used in a conditional statement against 'true'
Parameters ? Are there 5 parameters? 1 ? What type of parameter is it - a bool, int, string, vector?

With these questions answered you should be able to extract the prototype and then the definition will be just copy/pasting what you have from your last assignment starting from the 'for' loops.
Last edited on
sorry I'm new to the site and didn't know about code tags.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 1 #include<iostream>
  2 using namespace std;
  3 
  4 bool isPrime(int number)
  5 {
  6   int i;
  7   for (i=2; i<number; i++)
  8   { if(number% i==0)
  9     isPrime=false;
 10   }
 11   if (isPrime==true)
 12     cout << "true" << endl;
 13 }
 14  int main()
 15 { int counter=0;
 16   int current_number=2;
 17   cout<< "First 1000 prime numbers" << endl;
 18   while (counter <1000)
 19   { if (prime_number(current_number)==true)
 20     { cout << current_number << endl;
 21       counter++;
 22     }
 23     current_number++; }
 24     return 0;
 25 }
 26 

there's what I have so far. I need it to return true if its prime and false if it isn't. I know my main() is right. It's the top I'm having trouble with. I went through and got rid of what I didn't need and now I'm stuck
OK, that's a good start. Look into your textbook (or google) for how to declare a function. In a 1 file program the order goes:
includes
using namespace std;

function prototypes

main()

function definitions

lines 4 to 13 is the function definition - which should be moved below main() (after line 25). lines 4 to 13 should then be replaced with a single line that is the function prototype (find that and the syntax and why there is one in your book).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  4 bool isPrime(int number)  //function name is 'isPrime', return type is 'bool' parameter is 1 integer
  5 {
  6   int i;
  7   for (i=2; i<number; i++) //for (int i=2; i<number; i++) <--better to keep local variables local
  8   { if(number% i==0)
 //what is 'isPrime' ? it is a function that takes 1 parameter
//it is not a variable - therefore line 9 is impossible
//you want to return the fact that number is not a prime number right?
//so it is just: return false;
//keep in mind that after a 'return' statement, the rest of the function wont be executed
  9     isPrime=false;
 10   }
 11   if (isPrime==true) //same problem with isPrime here...
 12     cout << "true" << endl;
 13 }


1
2
3
//so, return true or return false from isPrime() comes back here and is checked 
//in this 'if' statement to deal with if it is indeed a prime:
if (isPrime(current_number)==true


line 19 calls a function called/whose name is: prime_number(). does this function have a matching definition ?

Last edited on
it is a best Programming Language...
teacher has assigned us to write a function that can tell if a number is prime or not,

This says to me "get a number from the user and say whether it is prime or not" ......i see this as quite different from getting a number like 1500 and spelling out all the prime numbers between it and 2.
Now all you need is a function that will establish if it(the user number) is prime or not.
That function will check that when the user number is "moded" successively with 2 thru sqrt(user number) it does or does not == 0 to be prime.
Since 2 is the only even prime the loop will need to be incremented by i+=2 and start at i=1.
Topic archived. No new replies allowed.