### writes right for 400 but not for 484...

Helo!
Please, what is wrong wiht the code?
If I out a1=400, it writes out 19, a si t should be.
But, if I write 484, then it writes out NOTHING!!!

it should wirte out closest prime number to sqrt(a1).
For sqrt(400)=20, nearest prime is 19, and it writes it out.

But for sqrt(484)= 22, nearest pirme would be 23, and it writes nothing.

Many thanks!

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 #include #include using namespace std; int Ave(int array_f[], int& sm, int c) { int k=0; do { int e=1; for (int i=2;iarray_f[4]){ cout<array_f[j])&&(c
Last edited on
I mean no offense... but your code is very difficult to read because you don't give your variables meaningful names... they're all 1-letter names which are completely undescriptive. You also don't have any comments so it's next to impossible to know what you're trying to do.

Maybe someone else might be able to take the time to decipher this, but I can't spare the time right now. So instead I will direct you towards a guide of how to use a debugger which might help you be able to find the problem for yourself:

http://www.cplusplus.com/forum/beginner/75304/#msg403990

Use the debugger to step through the code line by line... watch the variables and make sure they are updating to be what you expect.
Last edited on
Hello!
cam we have a look just at this segment?

 12345678910111213141516171819 cout<array_f[4]){ cout<array_f[j])&&(c
Right here is the problem:

if--> writes
else--> not!!!!

 1234567 if (c-array_f[j]<=array_f[j+1]-c){ cout<
 if--> writes else--> not!!!!

No... they're both writing. Maybe this code isn't executing when you think it is.

Use the debugger. Set a breakpoint. If the breakpoint trips, you know the line of code is running and you can see what all the variables contain. See that thread I linked before for instruction on how to do that.
Did U try this program in your own compiler??
Topic archived. No new replies allowed.