Advertisement
81gl-ntd

Mảng 2 chiều

Dec 9th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #define Mrow 100
  6. #define Mcol 100
  7. using namespace std;
  8. int nhap(int a[3][3])
  9. {
  10. int i,j;
  11. for (i=0;i<3;i++)
  12. for (j=0;j<3;j++)
  13. cin>>a[i][j];
  14. getch();
  15. }
  16. int hienthi(int a[3][3])
  17. {
  18. int i,j;
  19. for (i=0;i<3;i++) {
  20. for (j=0;j<3;j++)
  21. cout<<a[i][j]<<" ";
  22. cout<<endl; }
  23.  
  24. }
  25. int ngaunhien(int a[3][3])
  26. {
  27. int i,j;
  28. srand((unsigned)time(NULL));
  29. for (i=0;i<3;i++){
  30. for (j=0;j<3;j++)
  31. a[i][j]=rand()%100;
  32. }
  33. }
  34. int hienthingaunhien(int a[3][3])
  35. {
  36. int i,j;
  37. for (i=0;i<3;i++) {
  38. for (j=0;j<3;j++)
  39. cout<<a[i][j]<<" ";
  40. cout<<endl;
  41. }
  42. }
  43.  
  44. int nhapvoigiatribatky(int a[Mrow][Mcol],int& m,int& n) //có dấu & để khi truy xuất ở dưới nó sẽ truyền ngược trở lại vào hàm int nhap() nếu không sẽ bị lặp vô hạn.
  45. {
  46. int i,j;
  47. cout<<"Nhap kich thuoc dong & cot:"<<endl;
  48. cin>>m;
  49. cin>>n;
  50. for (i=0;i<m;i++)
  51. {
  52. for (j=0;j<n;j++)
  53. cin>>a[i][j];
  54. }
  55. getch();
  56. }
  57. int hienthivoigiatribatky(int a[Mrow][Mcol],int& m,int& n)
  58. {
  59. int i,j;
  60. for (i=0;i<m;i++)
  61. {
  62. for (j=0;j<n;j++)
  63. cout<<a[i][j]<<" ";
  64. cout<<endl;
  65. }
  66.  
  67. }
  68. int chepdong(int a[Mrow][Mcol],int m,int n)
  69. //chép dòng k-->h
  70. {
  71. int i,j,h,k;
  72. cout<<"chon dong nguon & dong chep de len:"<<endl;
  73. cin>>k;
  74. cin>>h;
  75. if (k<1 || k>m || h<1 || h>m)
  76. {
  77. return(0);
  78. }
  79. for (i=0;i<n;i++)
  80. {
  81. a[h-1][i]=a[k-1][i];
  82. }
  83. for (i=0;i<m;i++)
  84. {
  85. for (j=0;j<n;j++)
  86. cout<<a[i][j]<<" ";
  87. cout<<endl;
  88. }
  89.  
  90. }
  91.  
  92. int chepcot(int a[Mrow][Mcol],int m,int n)
  93. //chép cột k-->h
  94. {
  95. int i,j,h,k;
  96. cout<<"chon cot nguon & cot chep de len:"<<endl;
  97. cin>>k;
  98. cin>>h;
  99. if (k<1 || k>m || h<1 || h>m)
  100. {
  101. return(0);
  102. }
  103. for (i=0;i<m;i++)
  104. {
  105. a[i][h-1]=a[i][k-1];
  106. }
  107. for (i=0;i<m;i++)
  108. {
  109. for (j=0;j<n;j++)
  110. cout<<a[i][j]<<" ";
  111. cout<<endl;
  112. }
  113. }
  114.  
  115. int hoandoidong(int a[Mrow][Mcol],int m,int n)
  116. {
  117. int i,j,h,k,temp;
  118. cout<<"chon 2 dong can hoan doi"<<endl;
  119. cin>>h;
  120. cin>>k;
  121. if (k==h) return(0);
  122. for (i=0;i<n;i++)
  123. {
  124. temp=a[k-1][i];
  125. a[k-1][i]=a[h-1][i];
  126. a[h-1][i]=temp;
  127. }
  128. for (i=0;i<m;i++)
  129. {
  130. for (j=0;j<n;j++)
  131. cout<<a[i][j]<<" ";
  132. cout<<endl;
  133. }
  134. }
  135. int hoandoicot(int a[Mrow][Mcol],int m,int n)
  136. {
  137. int i,j,h,k,temp;
  138. cout<<"chon 2 cot can hoan doi"<<endl;
  139. cin>>h;
  140. cin>>k;
  141. if (k==h) return(0);
  142. for (i=0;i<m;i++)
  143. {
  144. temp=a[i][k-1];
  145. a[i][k-1]=a[i][h-1];
  146. a[i][h-1]=temp;
  147. }
  148. for (i=0;i<m;i++)
  149. {
  150. for (j=0;j<n;j++)
  151. cout<<a[i][j]<<" ";
  152. cout<<endl;
  153. }
  154. }
  155. int loaibodong(int a[Mrow][Mcol],int& m,int n)
  156. {
  157. int i,j,k;
  158. cout<<"nhap dong can loai bo"<<endl;
  159. cin>>k;
  160. if (k<0 || k>m) return(0);
  161. for(i=k-1;i<m-1;i++)
  162. {
  163. for(j=0;j<n;j++)
  164. a[i][j]=a[i+1][j];
  165. }
  166. m--;
  167. for (i=0;i<m;i++)
  168. {
  169. for (j=0;j<n;j++)
  170. cout<<a[i][j]<<" ";
  171. cout<<endl;
  172. }
  173. }
  174. int loaibocot(int a[Mrow][Mcol],int m,int& n)
  175. {
  176. int i,j,k;
  177. cout<<"nhap cot can loai bo"<<endl;
  178. cin>>k;
  179. if (k<0 || k>n) return(0);
  180. for(i=k-1;i<n-1;i++)
  181. {
  182. for(j=0;j<m;j++)
  183. a[i][j]=a[i][j+1];
  184. }
  185. n--;
  186. for (i=0;i<m;i++)
  187. {
  188. for (j=0;j<n;j++)
  189. cout<<a[i][j]<<" ";
  190. cout<<endl;
  191. }
  192. }
  193.  
  194.  
  195. int main()
  196. {
  197. int a[Mrow][Mcol],m,n;
  198. nhapvoigiatribatky(a,m,n);
  199. hienthivoigiatribatky(a,m,n);
  200. //ngaunhien(a);
  201. //hienthingaunhien;
  202. //chepdong(a,m,n);
  203. //chepcot(a,m,n);
  204. //hoandoidong(a,m,n);
  205. //hoandoicot(a,m,n);
  206. //loaibodong(a,m,n);
  207. //loaibocot(a,m,n);
  208. getch();
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement