Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. double m[3][3];
  2.  
  3. for (i=0; i<ROWLENGTH; ++i){
  4. double temp;
  5. temp = m[r2][i];
  6. m[r2][i] = m[r1][i];
  7. m[r1][i] = temp;
  8. }
  9.  
  10. double **m;
  11. m = malloc(sizeof(double*)*NUMROWS);
  12. /* put error checking here */
  13. for (i=0; i<NUMROWS; ++i){
  14. m[i] = malloc(sizeof(double)*ROWLENGTH);
  15. /* error checking again */
  16. }
  17.  
  18. double *temp;
  19. temp = m[r2];
  20. m[r2] = m[r1];
  21. m[r1] = temp;
  22.  
  23. double r[3], q[3] = { 1, 2, 3 };
  24. r = q; /* ERROR */
  25.  
  26. typedef struct { double r[ROWLENGTH] } row;
  27. row m[NUMROWS] = { {1, 2, 3}, {4, 5, 6}, {7, 8 9}};
  28.  
  29. row temp = m[2];
  30. m[2] = m[1];
  31. m[1] = temp;
  32.  
  33. typedef int Row[3];
  34. Row Matrix[3];
  35.  
  36. Row Temp;
  37.  
  38. memcpy(Temp, Matrix[0], sizeof(Row));
  39. memcpy(Matrix[0], Matrix[1], sizeof(Row));
  40. memcpy(Matrix[1], Temp, sizeof(Row));
  41.  
  42. int x[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  43.  
  44. for (int i=0; i<3; i++) {
  45. int temp = x[0][i];
  46. x[0][i] = x[1][i];
  47. x[1][i] = temp;
  48. }
  49.  
  50. typedef struct {int m[3];} Row;
  51. typedef int RowAccess[3];
  52.  
  53. main()
  54. {
  55. Row tmp,row[]={{1,2,3},{4,5,6},{7,8,9}};
  56. RowAccess *rowa=row;
  57. tmp=row[0];
  58. row[0]=row[1];
  59. row[1]=tmp;
  60. /* easy access to matrix here: (is this what you want?) */
  61. rowa[0][0]=0;
  62. rowa[0][1]=1;
  63. ...
  64. return 0;
  65. }
  66.  
  67. auxiliary = a ;
  68. a = b ;
  69. b = auxiliary ;
  70.  
  71. a ^= b ;
  72. b ^= a ;
  73. a ^= b ;
  74.  
  75. int swap (int *a , int *b){
  76. (*a)^=(*b);
  77. (*b)^=(*a);
  78. (*a)^=(*b);
  79. return 0;
  80. }
  81.  
  82. #define ROW_COUNT 5
  83. #define COLUMN_COUNT 5
  84. ....
  85. int a[ROW_COUNT][COLUMN_COUNT];
  86.  
  87. printf("nSwap Row: "); scanf("%d", &swp1) ; // first row index
  88. printf("With Row: "); scanf("%d", &swp2); // second row index
  89.  
  90. for (j = 0 ; j < COLUMN_COUNT ; j++){
  91. swap( &a[swp1][j] , &a[swp2][j] );
  92. }
  93.  
  94. #include "stdio.h"
  95. #include "conio.h"
  96.  
  97. #define ROW_COUNT 5
  98. #define COLUMN_COUNT 5
  99.  
  100.  
  101.  
  102. int swap (int *a , int *b){
  103. (*a)^=(*b);
  104. (*b)^=(*a);
  105. (*a)^=(*b);
  106. return 0;
  107.  
  108. }
  109.  
  110. int main(){
  111. int i, j ;
  112. int swp1, swp2 ;
  113. int a[ROW_COUNT][COLUMN_COUNT];
  114.  
  115. // Create ( ROW_COUNT X COLUMN_COUNT ) random matrix
  116.  
  117. for (i = 0 ; i < ROW_COUNT ; i++ )
  118. for (j = 0 ; j < COLUMN_COUNT ; j++ ) a[i][j] = rand();
  119.  
  120. // Display matrix before row swap
  121.  
  122. for (i = 0 ; i < ROW_COUNT ; i++ ){
  123. for (j = 0 ; j < COLUMN_COUNT ; j++ ) printf("%dt",a[i][j]);
  124. printf("n");
  125. }
  126.  
  127. // Elements to be swapped
  128.  
  129. printf("nSwap Row: "); scanf("%d", &swp1) ; // first row index
  130. printf("With Row: "); scanf("%d", &swp2); // second row index
  131.  
  132. // Swapping right here
  133.  
  134. for (j = 0 ; j < COLUMN_COUNT ; j++){
  135. swap( &a[swp1][j] , &a[swp2][j] );
  136. }
  137.  
  138.  
  139. // Display once again
  140.  
  141. printf("n");
  142. for (i = 0 ; i < ROW_COUNT ; i++ ){
  143. for (j = 0 ; j < COLUMN_COUNT ; j++ ) printf("%dt",a[i][j]);
  144. printf("n");
  145. }
  146.  
  147.  
  148.  
  149. getch();
  150. return 0;
  151. }
  152.  
  153. temprow = row[1];
  154. row[1] = row[0];
  155. row[0] = temprow;
  156.  
  157. #include <algorithm>
  158. int A[][] = {{1,2,3},{4,5,6},{7,8,9}};
  159. swap(A[0],A[2]); //swaps first and last row
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement