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.

please, where is the mistake?
Many thanks!

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
62
63
64
65
66
67
 #include<iostream>

#include<math.h>
using namespace std;
int Ave(int array_f[], int& sm, int c)
{
 int k=0;

 do {
    int e=1;
    for (int i=2;i<sm;i++)
    {
        if (sm%i==0)
        {
           e=0;break;
        }
    }
    if (e==1) {
       array_f[k]=sm; /*cout<<k<<" "<<sm<<" "<<array_f[k]<<endl;*/k=k+1;
    }
    sm=sm+1;
 } while(k<5);

 /*int r;
 int m=0;
 for (int i=0;i<k; i++) {
     m=array_f[i]+m;
 }
 r=m/k;
*/
 cout<<array_f[0]<<" "<<array_f[1]<<" "<<array_f[2]<<" "<<array_f[3]<<" "<<array_f[4]<<endl;

 if (c<array_f[0]){
    cout<<array_f[0]<<endl;
 }
 if (c>array_f[4]){
    cout<<array_f[4]<<endl;
 }
 for (int j=0;j<k;j++) {
     if ((c>array_f[j])&&(c<array_f[j+1])) {
        if (c-array_f[j]<=array_f[j+1]-c){
           cout<<array_f[j]<<" "<<"@"<<endl;
           }
         else {
           cout<<array_f[j+1];
         }
        
     }
 }

return r;
}              //function 

int main()
{
int a1=484;


int a=(int)sqrt(a1);
int x=14;
int array_m[5];
int g;
g=Ave(array_m,x,a);
cout<<g;
system("PAUSE");
return 0;
}
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 cout<<array_f[0]<<" "<<array_f[1]<<" "<<array_f[2]<<" "<<array_f[3]<<" "<<array_f[4]<<endl;

 if (c<array_f[0]){
    cout<<array_f[0]<<endl;
 }
 if (c>array_f[4]){
    cout<<array_f[4]<<endl;
 }
 for (int j=0;j<k;j++) {
     if ((c>array_f[j])&&(c<array_f[j+1])) {
        if (c-array_f[j]<=array_f[j+1]-c){
           cout<<array_f[j]<<" "<<"@"<<endl;
           }
         else {
           cout<<array_f[j+1];
         }
        
     }
 }
Right here is the problem:

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


1
2
3
4
5
6
7
 if (c-array_f[j]<=array_f[j+1]-c){
           cout<<array_f[j]<<" "<<"@"<<endl;
           }
         else {
           cout<<array_f[j+1];
         }
        
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.