#### Archived

This topic is now archived and is closed to further replies.

# Need help with some little error

This topic is 6124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

#include #include int number_is_prime(int &number_in); void fill_array(void); int compare_array[40]; int input_number; int input_array[40]; void main() { char pause; fill_array(); cout<<"\n"; int x=0; do { cout<< input_array[x]; if (compare_array[x]==0) cout<< " is not prime."; else cout<<" is prime./n"; x=x++; }while(x!=40); // pauses the output so i can see it on my pos compiler cout<< "enter any number to end"; cin>> pause; } // asks for number and finds out if it is odd, if it is even it adds one to make it odd // then fills an array with odd numbers void fill_array(void) { int half_input_number; cout<< "enter a number between 1 and 100: "; cin >> input_number; half_input_number=input_number/2; if(input_number==half_input_number*2) { input_number++; } for(int loop2=0;loop2<=39;loop2++) { compare_array[loop2]= number_is_prime(input_number); input_array[loop2]=input_number; input_number=input_number+2; }; } //======================================== //comparing function //======================================== int number_is_prime(int &number_in) { int ref_array[46]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199}; for(int loop3=0; loop3<=45; loop3++) { if (number_in == loop3) return 1; else return 0; } }

##### Share on other sites
for some reason it just doesn't work I have played with this thing for hours and I am about to pass out. Please help

here was the assigment:

fill an array with odd numbers (40 of them) where you pick the first one (1 to 100) and if it is even it will add one to make it odd and compare them to an array of prime numbers returning a 0 if it is not prime and a 1 if it is, then it is supposed to display the list numbers and wether or not they are prime.

Edited by - LegendNH on December 18, 2001 5:33:27 PM

##### Share on other sites
The problem is in number_is_prime():

for(int loop3=0; loop3<=45; loop3++)
{
if (number_in == loop3)
return 1;
else
return 0;
}

This for loop only checks one number, because it returns whether number_in == loop3 or not. What you want is this:

for(int loop3=0; loop3<=45; loop3++)
{
if (number_in == loop3)
return 1;
}

return 0;

This way, it only returns false if it gets all of the way through the loop without finding a match.

FragLegs

BTW - You can test a number for evenness with this:

if (my_number % 2 == 0) // then it is even, else it is odd

It is faster than the way that you do it in the above code, and it is the common way to test for even/odd (meaning it will be easier for other programmers to understand).

##### Share on other sites
thank you very much, my friend asked me for help and I could not figure out what was wrong, your help was greatly appriciated. Also he does not know about remainders yet and he said he didn''t want to change it because his teacher never taught him that.

These forums are very friendly and so much more mature then hardforum was. They are just a bunch of children who wanted to make their post count as high as possible.

Well thanks again

##### Share on other sites
if i''m not mistaken, shouldn''t the for loop read:

for(int loop3=0; loop3<=45; loop3++)
{
if (number_in == ref_array[ loop3 ])
return 1;
}

so that you test the prime numbers in the array rather than comparing the input number to the numbers 0 through 45.

===========================
The price of intelligence is stupidity

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 36
• ### Forum Statistics

• Total Topics
632634
• Total Posts
3007551
• ### Who's Online (See full list)

There are no registered users currently online

×