Set prime number into array and print it out

Let user enter the size of matrix: (for example 3) and program will show like this:
2 3 5
7 11 13
17 21 23

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
#include <iostream>
using namespace std;
bool prime(int m);
int main()
{
	int j,i,s,n=0,oke=0;
	cout<<"What demansion: ";
	cin>>s;
	int a[s][s];
 	for (i=0;i<s;i++)
	 	{
	 		for(j=0;j<s;j++)
	 		{
		 		while (oke<=s*s)
	 			{
			 		n++;
			 		if(prime(n)==true)
			 		{
		 				a[i][j]=n;
		 				oke++;
		 			}
		 			break;
			 	}
		 	}
	 	}
	system("pause");
	return 0;
}
			

 bool prime(int m)
    {
         int i=1,count=0;
         while (i<m)
         {
               if(m%i==0)
               {
                         count++;
               }
               i++;
         }
         if (count==1)
                     return true;

         else 
                  return false;
    }
Last edited on
The way you create an array at line 9, the size needs to be known at compile time. If you're going to let the user enter the size, you'll need to dynamically allocate the array with new and deallocate the memory later with a corresponding delete.

http://www.cplusplus.com/doc/tutorial/dynamic/
Thanks for reading my topic and show me my correct :) but i not understand at all. I think i didn't get the main key to solve this problem. Can you correct me one more time? It's great if we have some code.
Topic archived. No new replies allowed.