Printing out prime from array

closed account (ETA9216C)
I am trying to print out prime from array. I have viewed other sources that are similar and tried to use as reference, but only to confuse myself.This is what i have so far.

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

void get_output(int&Max,int array[])
{
    	//array[i]-i; no idea what his does
        
    for(i=1;i<=10;i++) // allow 10 outputs on a line.
    {
        for(int i=0;i<Max;i++)
        {
            prime=true;
            if(Max%i==0)
            {
                prime =false;
             else
                 return true;

                 

            }
                    
        }
            
    }
		  cout<<endl;
}
Last edited on
closed account (ETA9216C)
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
void get_output(int&Max,int array[])
{
    bool prime;

	//array[i]-i; no idea what his does

    for(int i=1;i<=10;i++)
    {
        for(int i=0;i<Max;i++)
        {
            prime=true;
            if(Max%i==0)
            {
                prime =false;
            }
            if(Max%i!=0)
            {
                return true;
            }

        }

    }
		  cout<<endl;
}



 void printout(int&Max,int array[])
 {
    cout<<array[Max]<<" = "<<prime;



 }



update of code. From looking at more references, i came up with this so far. I don't know how am i going to print out the prime.
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
bool isPrime (int Max)
{
	bool answer = true;                       
	
	for (int i = 2; i <= sqrt(Max); ++i )
		if (Max %  i == 0)
		{
			answer = false; 	
			break;			
		}
	return answer;
}
Last edited on
closed account (ETA9216C)
it may have worked but there is nothing on output console.
Would it have been better if i did a structure?

this is entire code:

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
int main(int argc, char* argv[])
{

	int Max;
	int array[Max];



	max_number(Max);
	get_output(Max,array);
	printout(Max,array);

	system("pause");
	return 0;
}

void max_number(int&Max)
{

	   cout<<"Enter a MAX number ";
	   cin>>Max;

}

int get_output(int&Max,int array[])
{
bool isPrime (int Max);

	bool answer = true;

	for (int i = 2; i <= sqrt(Max); ++i )
		if (Max %  i == 0)
		{
			answer = false;
			break;
		}
	return answer;
}



 void printout(int&Max,int array[])
 {
    int anwser;
    cout<<array[Max]<<" = "<<anwser;



 }
Last edited on
You can integrate that function in your code as follows:

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
# include <iostream>
// and stuff of # include...

// other functions...

bool isPrime (int Max);

	bool answer = true;

	for (int i = 2; i <= sqrt(Max); ++i )
		if (Max %  i == 0)
		{
			answer = false;
			break;
		}
	return answer;
}

int main()
{
 
//  your code that gives Max
    .....................
        if(isPrime (Max))
            cout << "Prime: " << Max << '\n';

//  the rest of your code......

    return 0;
}
Last edited on
Topic archived. No new replies allowed.