Count the number of equal elements in two-dimensional arrays & traverse the two-dimensional matrix

Source: Internet
Author: User

Count the number of equal elements in two arrays (one-dimensional linear method is required)

  1. IntArray_count (Int* Array1,Int* Array2)
  2. {
  3. IntI = 0, j = 0, Count = 0;
  4. While(I <num1 & J <num2)
  5. {
  6. If(Array1 [I] <array2 [J])
  7. I ++;
  8. Else If(Array1 [I]> array2 [J])
  9. J ++;
  10. Else
  11. {
  12. I ++;
  13. J ++;
  14. Count ++;
  15. }
  16. }
  17. ReturnCount;
  18. }

Search and traverse the output two-dimensional array as shown in.

  1. # Include <stdio. h>
  2. Int m_print (INT ** data, int N)
  3. {
  4. Int I = 0, j = 0;
  5. // Indicates the exploration sign in the lower left. If the value is 0, the exploration is performed in the upper right corner.
  6. Int downleft = 1;
  7. // Count is used for loop exit
  8. Int num = 0;
  9. // Number of array elements
  10. Int loop = N * n-1;
  11. // Parity mark of n, where 0 is an even number and 1 is an odd number
  12. Int even = n % 2;
  13. // Print the first element
  14. Printf ("% d", * (int *) (data) + I * n + j ));
  15. While (1)
  16. {
  17. // Located at the upper boundary. (J! = N-1 |! Even) is used to process the special situations of the upper-right elements based on the parity of N.
  18. If (I = 0 & (J! = N-1 |! Even ))
  19. {
  20. // Explore to the right
  21. J ++;
  22. // Output Element
  23. Printf ("% d", * (int *) (data) + I * n + j ));
  24. // Exit the loop after the output is complete.
  25. If (++ num> = loop) break;
  26. // Set it to lower left to explore
  27. Downleft = 1;
  28. }
  29. // At the bottom boundary
  30. If (I = n-1)
  31. {
  32. // Explore to the right
  33. J ++;
  34. // Output Element
  35. Printf ("% d", * (int *) (data) + I * n + j ));
  36. If (++ num> = loop) break;
  37. // Set it to exploration in the upper right corner
  38. Downleft = 0;
  39. }
  40. // At the left boundary
  41. If (j = 0)
  42. {
  43. // Explore down
  44. I ++;
  45. // Output Element
  46. Printf ("% d", * (int *) (data) + I * n + j ));
  47. If (++ num> = loop) break;
  48. // Set it to exploration in the upper right corner
  49. Downleft = 0;
  50. }
  51. // On the right boundary
  52. If (j = n-1 & (I! = 0 | even ))
  53. {
  54. // Explore down
  55. I ++;
  56. // Output Element
  57. Printf ("% d", * (int *) (data) + I * n + j ));
  58. // Exit the loop after the output is complete.
  59. If (++ num> = loop) break;
  60. // Set it to lower left to explore
  61. Downleft = 1;
  62. }
  63. // Explore at the bottom left
  64. If (downleft)
  65. {
  66. I ++;
  67. J --;
  68. }
  69. // Explore in the upper-right corner
  70. Else
  71. {
  72. I --;
  73. J ++;
  74. }
  75. // Output Element
  76. Printf ("% d", * (int *) (data) + I * n + j ));
  77. // Exit the loop after the output is complete.
  78. If (++ num> = loop) break;
  79. }
  80. Return 0;
  81. }
  82. Int data2 [2] [2] =
  83. {
  84. {1, 2 },
  85. {3, 4}
  86. };
  87. Int data3 [3] [3] =
  88. {
  89. {1, 2, 6 },
  90. {3, 5, 7 },
  91. {4, 8, 9}
  92. };
  93. Int data4 [4] [4] =
  94. {
  95. {1, 2, 6, 7 },
  96. {3, 5, 8, 13 },
  97. {4, 9, 12, 14 },
  98. {10, 11, 15, 16}
  99. };
  100. Int data5 [5] [5] =
  101. {
  102. {1, 2, 6, 7, 15 },
  103. {3, 5, 8, 14,16 },
  104. {4, 9, 13, 17, 22 },
  105. {10, 12, 18, 21, 23 },
  106. {11,19, 20,24, 25}
  107. };
  108. Int data6 [6] [6] =
  109. {
  110. {1, 2, 6, 7, 15, 16 },
  111. {3, 5, 8, 14,17, 26 },
  112. {4, 9, 13, 18, 25, 27 },
  113. },
  114. },
  115. {, 31}
  116. };
  117. Int main ()
  118. {
  119. M_print (INT **) data2, 2 );
  120. Printf ("/N ");
  121. M_print (INT **) data3, 3 );
  122. Printf ("/N ");
  123. M_print (INT **) data4, 4 );
  124. Printf ("/N ");
  125. M_print (INT **) data5, 5 );
  126. Printf ("/N ");
  127. M_print (INT **) data6, 6 );
  128. Printf ("/N ");
  129. Return 0;
  130. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.