Need help using a function in Main

I have created a function to identify if a number is prime or not. I need to then use the return value ( it's a boolian statement ) in my main program. How to I get my main program to recognize my return? Lets say for simplicity, I just want a cout statement saying "is prime" or "is not prime" in my main program not included in the function. I'm already using the "cin" from my main function as the input so all my files are in order. Thanks in advance My function below.
|
|
|
V

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

#include "isPrime_PROTOTYPE.h"

;bool isPrime (int n)
{
	int remainder;

	

	for (int x = 2; x < n; x++)
	
	{
		remainder = n % x;
		cout << x << " " << remainder << endl;

		if (remainder == 0)
			{
				return false;
			}
	}
}
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
27
28
29
30
31
32
#include<iostream>
using namespace std;

bool isPrime (int n)
{
	int remainder;



	for (int x = 2; x < n; x++)

	{
		remainder = n % x;
//		cout << x << " " << remainder << endl;

		if (remainder == 0)

				return true;
        else
        return false;
	}
}
int main()
{
    int a=0;
    cin>>a;
    if(isPrime(a)==0)
        cout<<"A prime. "<<endl;
    else
        cout<<"Not a prime. "<<endl;
return 0;
}
Outside the for-loop in your 'isPrime' function, make sure to include a return true type.

In your main, create a bool variable. Then all you have to do is

mainBool = isPrime(numberYouSend);

Then the bool in your main will have the value of whatever your function determines.


Is this helpful?

EDIT to Chriscpp
In the if-statement,

Messed up a bit here. Look at the next post.
1
2
if(!isPrime(a)) cout << "Not prime\n";
else cout << "Prime" << endl;


Just another way for that.
Last edited on
@sherre02 it is
1
2
3
4
if(!isPrime(a))
        cout<<"A prime. "<<endl;
    else
        cout<<"Not a prime. "<<endl;
haha I knew that! I'm leaving the mistake though
lol ;)
Your prime function is going to run verrrry slow for large numbers. You should check if it is divisible by 2 first then start your for loop at 3 and increment by 2. This will cut the iterations in half. You could also make it so every 3rd increment it will increment an additional 2 which would cut off a third more. Which would make it 1/3 overall iterations compared to the starting. So instead of o(n) it is now o( n /3 ) Another way to cut it even more would be to loop till a sqrt of the value. so now it would be o( ( n / 3 )^ .5 ) ~ o( n / 5 )


here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
if( value % 2 == 0 ) return( false );
int count = 0;
for( int i = 3; i < sqrt(value); i += 2; )
{
    ++count;
    if( count == 3 )
    {
        count = 0;
        i += 2;
    }
    if( value % i == 0 ) return( false );
}
return( true );
I'm glad you understand that upper-limit of divisibility (square root).

Not a lot of people understand that for some reason.
Wow, a lot of responses. I appreciate the help, I really do. This is for a college assignment. I don't think it will be scrutinized just checked for functionality. What I have to do is insert this function into my main, then I have to display each prime number passed my original input up to 160 prime numbers. Not sure if the "if else" statements in "main" will work well for this.
Hmm... Then change the function-return-type to 'int'.

If the number is prime, return the number. If it isn't prime, return '-1'.

Then in main, print the number if it isn't '-1'.
Isn't a boolian expression already in the form of int? True being 1 and False being 0?
That's correct. I was just offering another idea. Do you have things under control now?
Lol, not so much. I keep getting errors,
When it runs through the program I get either a 1 or a 0,
I need to keep the original number so I can use a post increment on it in a loop to keep getting prime numbers. Not sure how to do it. I tried creating another variable then assigning it to the original variable after it runs through the program, but that's not working either. I'm sure I could look this up on google but that would be cheating, I plan on doing this for a living one day so I have to work through it. However, my professor is less than informative...

Also if I give the bool expression the value of true it breaks the loop. If only a false the program will continue with the numbers
Last edited on
It's probably something stupid but I can't seem to get it to reassign nums value to number it just runs through the loop using the original value, unless the original value is not prime, then it increments it by one and uses that value 160 times.


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
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

#include "isPrime_PROTOTYPE.h"

;int main()
{
	int Prime = 0;	
	int Number;
	int Num=0;

	cin >> Number;
	Num = (Num != Number) ?Number:Num ;
	
	
while (Prime < 160)
	{
		Num = (Num != Number) ?Number:Num ;
	
		if(isPrime (Number)==0)
			Num ++;
		

		else(isPrime (Number)!=0); 
			cout << Num << endl;
			Prime ++;
			Num ++;
		
	}
		
	
	
	system("pause");
	return 0;

}
Last edited on
Just a question, why do you start your functions with a ;? It'll compile, but why would you do that?
Because if it's not there it won't compile...
Because if it's not there it won't compile...


You probably forgot a ; inside your isPrime_PROTOTYPE.h file somewhere.
So I decided to heck with the nonsense and put everything on one cpp. Everything now works as it should...HOWEVER....I have now run into a problem with the number two, for some reason I can't seem to get it to work if the user enters in "2" for the Number.


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;

bool Prime (int n)
{
	
	int remainder;

	for (int x = 2; x < n; x++)
		{
			remainder = n % x;

			if ( remainder == 0 )
				{
					return false;
				}
			else if ( n == 2 )
				{
					return false;
				}
			else if ( x == n - 1 && remainder !=0)
				{
					return true;
				}
		}
}


int main()
{
	int Number;
	int Primes=0;
	cin >> Number;

	
	
	
	
while( Primes < 160 )
	{
	if(Prime(Number)==true)
		{
		
			cout << Number << " Is prime." << endl;
			Primes ++;
			Number ++;
		
		}
	
	else if(Prime(Number)==false)
		{
	
			Number++;
		
		}
	
	}
	system("pause");
		return 0;

}
Last edited on
In your for-loop in the function, if you pass '2' to 'n' then go through the loop, you start with 'x = 2'.
 
remainder = n % x;


Right off the bat you get 'remainder' = '0'. According to the first if-statement in the for-loop, if 'remainder = 0', you return false.

That's why t doesn't work for an input of '2'
You probably dont get 2 as a prime because of line 19. It should return true not false. 1 and 2 are primes. Prime numbers do not start at 3.
Topic archived. No new replies allowed.