Ah ok... Any suggestions on what it should be? Im not sure how to calculate it at all.
This is a discussion on Not declared in this scope within the C++ Programming forums, part of the General Programming Boards category; Ah ok... Any suggestions on what it should be? Im not sure how to calculate it at all....
Ah ok... Any suggestions on what it should be? Im not sure how to calculate it at all.
Since 2 is a special case, you could check first if the number is two (if so, it's a prime so return whatever says "this is a prime"). For all other numbers, try dividing by 2, then use the loop.
I do believe your loop is wrong too - check your logic [I take it you understand the PRINCIPLE of checking if a number is prime?].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I don't really get the principle of prime numbers sorry.
Ok so, a prime number is always uneven, unless it's 2.
First, check if the number is 2. If it is, return true.
Second, check if the number is even. If it is, return false.
Lastly, if it's an uneven number, I assume something like this would work:
Imagine n is the number you want to check.
Start a cycle with i = 2 for example.
Check if i*i < n. If it is, check if n%i equals 0. If it does, return false.
Increment 1 to i.
If your number survives through this cycle, n is a prime number.
Sorry guys I realise I must be painful to try and help so thanks for trying.
This is my latest try, I think I am getting closer at least.
Code:bool isPrime(int number) { int count, s; bool isprime = true; /* Every even number is not prime */ if (number == 2) return true; if (number % 2 == 0) return false; /* check every odd number up to the square root of the number */ s = sqrt(number); for (count=3; count<=s; count+=2); { if (number % count == 0) isprime = false; } return isprime; }
Looks good. To improve performance a bit, you could add a break to the "isPrime = false;" statement, as if you find out that the number isn't a prime, you know the result then and there.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks Mat, I'll have a look into that.
Just one last issue (though I seem to be full of them) I am trying to submit and I am getting an error "ERROR - Unable to find pointers being used in getNumber function"
The program looks to work otherwise so I was just wondering where the pointer should be and why I should need it?
Code:#include <iostream> #include <math.h> using namespace std; bool isPrime(int); void getNumber(int&); int main() { int number; getNumber(number); if (isPrime(number)) cout << "\n" << number << "is a prime number\n"; else cout << "\n" << number << "is not a prime number\n"; return 0; } void getNumber(int &number) { cout << "Please enter a positive number "; cin >> number; if (!cin.good()) { printf("Invalid number entered\n"); exit(1); } } bool isPrime(int number) { int count; double s; bool isprime = true; /* Every even number is not prime */ if (number == 2) return true; if (number % 2 == 0) return false; /* check every odd number up to the square root of the number */ s = sqrt(number); for (count=3; count<=s; count+=2); { if (number % count == 0) return false; } return isprime; }
I do not know what the problem is with that code. It looks fine to me.
What tool are you getting the error from?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
It is the submission system for my teacher. So he doesn't have to look at it if things don't pass the tests.
Apparently you need to use pointers to implement getNumber(), but I cannot see why unless this is C instead of C++.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
So I'm really lost. I'm trying to convert my code so that it passes the pointer in getNumber test but I just can't seem to work it out. Any help would be appreciated...
g++ -lm prime.cpp
prime.cpp: In function 'int main()':
prime.cpp:13: error: invalid conversion from 'int' to 'int*'
prime.cpp:13: error: initializing argument 1 of 'void getNumber(int*)'
prime.cpp:15: error: invalid conversion from 'int' to 'int*'
prime.cpp:15: error: initializing argument 1 of 'bool isPrime(int*)'
prime.cpp: In function 'bool isPrime(int*)':
prime.cpp:41: error: ISO C++ forbids comparison between pointer and integer
prime.cpp:42: error: invalid operands of types 'int*' and 'int' to binary 'operator%'
prime.cpp:45: error: cannot convert 'int*' to 'double' for argument '1' to 'double sqrt(double)'
prime.cpp:48: error: invalid operands of types 'int*' and 'int' to binary 'operator%'
Code:#include <iostream> #include <math.h> using namespace std; bool isPrime(int *number); void getNumber(int *number); int main() { int *number; getNumber(*number); if (isPrime(*number)) cout << "\n" << number << "is a prime number\n"; else cout << "\n" << number << "is not a prime number\n"; return 0; } void getNumber(int *number) { cout << "Please enter a positive number "; cin >> *number; if (!cin.good()) { printf("Invalid number entered\n"); exit(1); } } bool isPrime(int *number) { int count; double s; bool isprime = true; /* Every even number is not prime */ if (number == 2) return true; if (number % 2 == 0) return false; /* check every odd number up to the square root of the number */ s = sqrt(number); for (count=3; count<=s; count+=2); { if (number % count == 0) return false; } return isprime; }
Pointer Errors:
The first error is in the way your passing the numbers.
You've defined:
which essentially means your declaring a pointer to a number.Code:bool isPrime(int *number);
The value you pass would have to be the address of that number, not the number itself (the value of a pointer is an address).
What your doing is passing the value of number by de-referencing it ( * ) which retrieves the value stored at the address.
If you want to keep the parameter as int * number than you would have to call the function usingwhich actually means pass the value stored in the pointer number (address of the actual number)Code:isPrime(number)
and assign that to the pointer number in your function.
A more feasible answer would be passing by reference.
change:toCode:isPrime(int * number )This will allow you to just use number as if it were an int, so you can remove all the de-referencing within the functions. ie line 3 in getNumber() would be just number instead of *numberCode:isPrime(int & number)
This applies to your getNumber() function as well.
Tutorials on Pointers:
http://www.cplusplus.com/doc/tutorial/pointers.html : very complete but can be daunting
http://www.cprogramming.com/tutorial/lesson6.html : much simpler explanation but doesn't explain the & (address of) operator
TypeCasting:
The other error is the type-cast from double to int.
sqrt returns a double and your storing that data into an int.
a whole tutorial on type-casting is available here: http://www.cplusplus.com/doc/tutorial/typecasting.htmlCode:bool isPrime(int &number) { int count; int s; bool isprime = true; /* Every even number is not prime */ if (number == 2) return true; if (number % 2 == 0) return false; /* check every odd number up to the square root of the number */ s = static_cast<int>( sqrt(number) ); // this line could also be s = (int)(sqrt(number)) for (count=3; count<=s; count+=2); { if (number % count == 0) return false; } return isprime; }
Edit:
Another note about the passing parameters, as laserlight said, why are you passing the pointers? generally with small(er) datatypes such as int,double,float,char you can just pass them. This is useful because if you call isPrime(7) will result in an error when using pointers because 7 does not have an address. (not a variable just a value)
Although it is good practice to pass things by reference or by using pointers when using functions, generally this is only necessary for larger objects such as entire classes, structs, or arrays (that includes strings)
Last edited by balulu; 03-12-2009 at 06:49 AM.
hmm... looking at the error message from your teacher's tool, it appears that you only need to use a pointer in the getNumber() function. This is easy, because the change from using a reference to using a pointer parameter is trivial, since a reference can be seen as a const pointer without pointer syntax. As such:
The rest of your program would remain the same, except that instead of calling getNumber() as:Code:void getNumber(int* const number) { cout << "Please enter a positive number "; cin >> *number; if (!cin.good()) { printf("Invalid number entered\n"); exit(1); } }
You would call it by passing the address of number:Code:getNumber(number);
Code:getNumber(&number);
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Shouldn't I call getNumber as getNumber(*number)?
In function 'int main()':
prime.cpp:13: error: cannot convert 'int**' to 'int*' for argument '1' to 'void getNumber(int*)'
Code:#include <iostream> #include <math.h> using namespace std; bool isPrime(int &number); void getNumber(int* const number); int main() { int *number; getNumber(&number); if (isPrime(*number)) cout << "\n" << number << "is a prime number\n"; else cout << "\n" << number << "is not a prime number\n"; return 0; } void getNumber(int* const number) { cout << "Please enter a positive number "; cin >> *number; if (!cin.good()) { printf("Invalid number entered\n"); exit(1); } } bool isPrime(int &number) { int count; double s; bool isprime = true; /* Every even number is not prime */ if (number == 2) return true; if (number % 2 == 0) return false; /* check every odd number up to the square root of the number */ s = sqrt(number); for (count=3; count<=s; count+=2); { if (number % count == 0) return false; } return isprime; }
should not be int *number.Code:int *number; getNumber(&number);
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.