fork download
  1. // OpenMP Implementation
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <omp.h>
  6. #include <time.h>
  7.  
  8. #define ROWS 1000
  9. #define COLUMNS 1000
  10.  
  11. // Function to check if a string is a palindrome
  12. int is_palindrome(char *str, int len) {
  13. for (int i = 0; i < len / 2; i++) {
  14. if (str[i] != str[len - i - 1]) {
  15. return 0;
  16. }
  17. }
  18. return 1;
  19. }
  20.  
  21. int main() {
  22. char matrix[ROWS][COLUMNS];
  23. int n = 3; // Change this to the palindrome length you want to search for
  24. int total_palindromes = 0;
  25.  
  26. // Generate the matrix with random characters
  27. srand(time(NULL));
  28. for (int i = 0; i < ROWS; i++) {
  29. for (int j = 0; j < COLUMNS; j++) {
  30. matrix[i][j] = (rand() % 26) + 'A';
  31. }
  32. }
  33.  
  34. for (int threads = 1; threads <= 8; threads++) {
  35. omp_set_num_threads(threads);
  36. int palindrome_count = 0;
  37. double start_time = omp_get_wtime();
  38.  
  39. #pragma omp parallel for reduction(+:palindrome_count)
  40. for (int i = 0; i < ROWS; i++) {
  41. for (int j = 0; j < COLUMNS; j++) {
  42. char temp[n + 1];
  43.  
  44. // Check right-to-left
  45. if (j >= n - 1) {
  46. for (int k = 0; k < n; k++) {
  47. temp[k] = matrix[i][j - k];
  48. }
  49. temp[n] = '\0';
  50. if (is_palindrome(temp, n)) {
  51. palindrome_count++;
  52. }
  53. }
  54.  
  55. // Check up-to-down
  56. if (i <= ROWS - n) {
  57. for (int k = 0; k < n; k++) {
  58. temp[k] = matrix[i + k][j];
  59. }
  60. temp[n] = '\0';
  61. if (is_palindrome(temp, n)) {
  62. palindrome_count++;
  63. }
  64. }
  65.  
  66. // Check diagonally up-to-down
  67. if (i <= ROWS - n && j >= n - 1) {
  68. for (int k = 0; k < n; k++) {
  69. temp[k] = matrix[i + k][j - k];
  70. }
  71. temp[n] = '\0';
  72. if (is_palindrome(temp, n)) {
  73. palindrome_count++;
  74. }
  75. }
  76. }
  77. }
  78.  
  79. double end_time = omp_get_wtime();
  80. total_palindromes = palindrome_count;
  81. printf("%d palindromes of size %d found in %.6f s using %d threads.\n",
  82. total_palindromes, n, end_time - start_time, threads);
  83. }
  84.  
  85. return 0;
  86. }
  87.  
Success #stdin #stdout #stderr 0.3s 40280KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted