I'm working on an exercise for pointers. I'm supposed to calculate the primes from <= x. Inside of my FOR loop I get the "expression must have integral or enum type" error. I think it has to do with i being an int and x being a double. I've tried a lot of things and nothing seems to make it work.
Any suggestions?
#include <iostream>
int pi(double x)
{
int * numprimes;
numprimes = new int;
// Declare pointers and allocate memory for all of them
// Compute number of primes <= x and store at integer that
// numprimes points to.
int count = 0;
for (int i = 2; i <= x; i++)
{
if (x % i != 0)
{
count++;
}
}
*numprimes = count;
int ret = *numprimes;
// Deallocate memory for all pointers declared
delete numprimes;
return ret;
}
If you want to try excersizes on pointers try solving simple problems like swapping array elements etc. i don't understand as to why you need the pointer in the above program it is completely unnecessary. Also primes are natural numbers not fractions.