First smaller prime to sqrt(a1)-PLEASE, PANICK!!!

Hello!
We have to input a number.
Then count 5 prime numbers <= it.
Then we make array_f made of these prime numbers.

Then we input another number, and program should cout smaller next "neighbour" among these array's elements.

Why is this program not writing 11 (because sqrt(81) =9 and it is smaller then 11- the array_f[0]???

p.s. I am writing in codepad so instead of cin I have to initialise variables at once.!!!

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream> 
#include<math.h>
using namespace std;

int Prox(int array_f[], int& sm, int a)
{

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;
}  

               // do
while(k<5);

if (array_f[k-1<a])
{
cout<<array_f[k-1];
}


if(array_f[0]>a)
{
cout<<array_f[0];
}


int j;
for (j=0;j<k;j++)
{

if(array_f[j]-a<array_f[j+1]-a)
{
cout<<array_f[j];
}
else
{
cout<<array_f[j+1];
}

}
return array_f[j];


}              //function


int main(){

int x=10;
int array_m[5];

int a1=81;
int a=(int)sqrt(a1);

cout<<Prox(array_m,x,a);
return 0;
}



Last edited on
array_f[j] as returning value just stinks to me...wondering why...
Hello!
Why is result 19 and not 11 here: (the same tast, fixd some things:)

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
if(array_f[0]>a)
{
t=array_f[0];
}


int j;
for (j=0;j<k;j++)
{

if(array_f[j-1]-a<=array_f[j]-a)
{
t=array_f[j-1];
}
else
{
t=array_f[j];
}

}
return t;


}              //function


int main(){

int x=10;
int array_m[5];

int a1=4;
int a=(int)sqrt(a1);
cout<<"a= "<<a<<endl;

cout<<Prox(array_m,x,a);
return 0;
}
Topic archived. No new replies allowed.