int Prime (int array_[5], int st) {
do
{
int e=1;
for (int i=2; i<st; i++){
if (st%i==0)
{
e=0;
break;
}
}
int m=0;
int k=0;
int array_f[5];
if(e==1){array_f[k]=st;}
k+=1;
st+=1;
m+=st;
}// do
while(k==4);
return m;
}
int main(){
int b=23;
cout<<Prime(b);
return 0;
}
int Prime (int array_[5], int st)
{
do
{ //<---------------------------- Start scope of 'do' body
int e=1;
for (int i=2; i<st; i++)
{
if (st%i==0)
{
e=0;
break;
}
}
int m=0;
int k=0; // <-------------- 'k' is declared in 'do' body
int array_f[5];
if(e==1) { array_f[k]=st; }
k+=1;
st+=1;
m+=st;
}// do //<---------------------- End scope of 'do' body
while(k==4); // but 'k' is used outside of that scope.
return m;
}
Move the declaration of 'k' to outside of the 'do' body.
I recommend that you add any extra code that you require in main() and leave the Prime function alone. Think of it as a tool which does just one job, like a screwdriver or a hammer. Function Prime() does a job, it gives a yes / no answer to tell whether a particular number is prime.
You can then call that function with different values, as required. For example this program (based upon your own code) will print all the prime numbers between 1 and 100:
#include <iostream>
usingnamespace std;
int Prime (int st)
{
if (st<2)
return 0;
int e = 1;
for (int i=2; i<st; i++)
{
if (st%i==0)
{
e = 0;
break;
}
}
return e;
}
int main()
{
for (int b=0; b<100; b++)
{
if (Prime(b))
cout << b << " ";
}
cout << endl;
return 0;
}
As for the question about k out of scope, the variable k is declared within a block enclosed by braces, like this:
1 2 3 4
{
int k = 5;
}
cout << k; // error
When the closing brace is reached, the variable k no longer exists, it cannot be referred to or used at all. The solution is to declare it outside of the block:
There is a for-loop, which executes for each value of b from 0 to 99. Inside the body of the loop, the function Prime() is called with the current value of b. If the number is prime, it is then printed.
So, when b=0, Prime() is not called, when b=1 we call Prime(), and then,up to 100, we do not call Prime().!!
I'm not sure what you mean here.
In the example I posted previously, it read like this:
1 2 3 4 5
for (int b=0; b<100; b++)
{
if (Prime(b))
cout << b << " ";
}
What happens in the for loop...
1. at line 1, b is declared. b is given the initial value of 0
2. the condition (b<100) is tested
3. it is true, so the body of the loop is executed, so yes, we do call Prime(0), etc.
4. after reaching the closing brace at line 5, execution goes back to the start of the loop. b is increased by 1, so b is now 1.
5. the condition (b<100) is tested.
6. ... and so on.
Look at it this way, this is what the body of the loop actually does
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
if (Prime(0))
cout << 0 << " ";
if (Prime(1))
cout << 1 << " ";
if (Prime(2))
cout << 2 << " ";
if (Prime(3))
cout << 3 << " ";
if (Prime(4))
cout << 4 << " ";
...
...
...
if (Prime(97))
cout << 97 << " ";
if (Prime(98))
cout << 98 << " ";
if (Prime(99))
cout << 99 << " ";