Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. /*
  5. Liczby naturalne a,b s¹ komplementarne je¿eli ich suma jest liczb¹
  6. pierwsz¹. Dana jest tablica typu tab=array[1..max,1..max] of integer;
  7. wype³niona liczbami naturalnymi. Proszê napisaæ procedurê, która zeruje
  8. element nie posiadaj¹ce liczby komplementarnej
  9. */
  10. using namespace std;
  11. const int N = 3;
  12. void print_table(int tab[N][N]);
  13. bool isPrime(int n);
  14. void set_table(int tab[N][N]);
  15.  
  16. int main()
  17. {
  18. int tab[N][N];
  19. set_table(tab);
  20. bool flag = true;
  21. int licznik = 0;
  22. for(int i=0; i<N; i++)
  23. {
  24. for(int j=0; j<N; j++)
  25. {
  26. for(int k=i+1; k<N; k++)
  27. {
  28. for(int m=j+1; m<N; m++)
  29. {
  30. if(!isPrime(tab[i][j] + tab[k][m]))
  31. {
  32. licznik++;
  33. if(licznik == N*N){
  34. tab[i][j] = 0;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41.  
  42. cout<<endl<<endl;
  43. print_table(tab);
  44.  
  45. return 0;
  46. }
  47.  
  48. bool isPrime(int n)
  49. {
  50. if(n < 2)
  51. return false;
  52. for(int i=2; i*i <= n; i++)
  53. {
  54. if(n%i == 0)
  55. return false;
  56. }
  57. return true;
  58. }
  59.  
  60. void set_table(int tab[N][N])
  61. {
  62. srand(time(NULL));
  63. for(int i=0; i<N; i++)
  64. {
  65. for(int j=0; j<N; j++)
  66. {
  67. tab[i][j] = rand() % 50 + 1;
  68. cout<<tab[i][j]<<"\t";
  69. }
  70. cout<<endl<<endl;
  71. }
  72. }
  73.  
  74. void print_table(int tab[N][N])
  75. {
  76. for(int i=0; i<N; i++) // wyswietlanie tablicy
  77. {
  78. for(int j=0; j<N; j++)
  79. {
  80. cout<<tab[i][j]<<"\t";
  81. }
  82. cout<<endl<<endl;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement