Primes & arrays! Mistake?

NEED TO USE GIVEN OR ELSE FUNCTIONS...

Given: Interval of integers [m,n]. Need to use function that recognises primes, need to print all those primenumber pairs that have difference of 2 (m-n=2). I need to use function that recognises primes (as I said before).


The problem:
a,b print strange numbers. When I declare a=0 and b=0 it prints not a primes but 0 and 0.


Need help... :(


Program:

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
    
#include <iostream>
using namespace std;

const int P=23;
const int K=40;
int pirmskaitli(int[],int k, int f, int n);

int main()
{
int atkartot;
    int n;
    int arrr[P]; // array of number
    int a;
    int b;

    do
    {   do
        {
        cout << "INT NUMBER OF ELEMENTS " << endl;
        cin >> n;
        if (n<2 || n>23 ) cout << "INT NUMBER, THAT IS IN INTERVA; (2;23)!" << P << endl;
        }
        while (n<2 || n>23);
        cout << "INT ALL ELEMENTS: " << endl;
        for (int i=0; i<n; i++)
        {cin >> arrr[i] ;}

        if (pirmskaitli(arrr, n, a, b)==5) cout << "------" << endl;
        else cout << "FOUND PRIMES: " << a << " and " << b << endl;



        cout << endl << endl << "reply (1) end (0)?" << endl;
        cin >> reply;}
        while (reply==1); 
    return 0;
    }



int pirmskaitli (int arrr[], int n, int a, int b)
{
        int arrr2[P]; // array of primes
        int pair[K]; // pair of numbers
        int pairx;


arrr[0];
 for (int i=0;i<n;i++)
{
if ((arrr[i]-1)%6==0 || (arrr[i]+1)%6==0 || arrr[i]<4 || arrr[i]==5 || arrr[i]==7) a=i;
}

return a;
}


in pirmskaili function you passed the value of the integers which means that when the function completed the value of the variable will return to as it was before look at this and compare:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
void function(int *x,int *y){  //look i passed parameters...so i dont need to return any value
	*x=*x+1;
	*y=*y+1;}
void main(){
	int a=4,b=3;
	function(&a,&b);
	cout<<"a value= "<<a<<endl<<"b value = "<<b;
	cin>>a;}     //copy it and compile and look what happened to a and b 
when you returned from pirmskaitli function of the array and a,n,b never changed because without parameters the computer understand that you to use temporarily and you want the values back ....... use parameters to avoid this
Topic archived. No new replies allowed.