Advertisement
Guest User

Untitled

a guest
May 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<time.h>
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. int random2digit(int max,int min);
  8.  
  9. void printarr(int [5][5],int row,int col);
  10.  
  11. int main()
  12. {
  13. srand(time(NULL));
  14. int A[5][5], row, col;
  15. do{
  16. cout << "Masukkan jumlah baris(1-5) ";
  17. cin >> row;
  18. if(row > 5 || row < 1)
  19. {
  20. cout << "Error!" << endl;
  21. }
  22. }while(row > 5 || row < 1);
  23. do{
  24. cout << "Masukkan jumlah kolom(1-5) ";
  25. cin >> col;
  26. if(col > 5 || col < 1)
  27. {
  28. cout << "Error!" << endl;
  29. }
  30. }while(col > 5 || col < 1);
  31.  
  32. for(int i = 0; i < row; i++)
  33. {
  34. for(int j = 0; j < col; j++)
  35. {
  36. if(i%2 == 0)
  37. {
  38. A[i][j] = 2 * random2digit(49,0) + 1;
  39. }
  40. else
  41. {
  42. A[i][j] = 2 * random2digit(49,0);
  43. }
  44. }
  45. }
  46. printarr(A, row, col);
  47.  
  48. return 0;
  49. }
  50.  
  51. int random2digit(int max,int min)
  52. {
  53. int x;
  54. x = rand()%(max - min) + min;
  55. return (x);
  56. }
  57.  
  58. void printarr(int A[5][5], int row,int col)
  59. {
  60. for(int i = 0; i < row; i++)
  61. {
  62. for(int j = 0; j < col; j++)
  63. {
  64. cout << A[i][j] << " ";
  65. }
  66. cout << endl;
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement