Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. /*Write a C/C++ application that defines two matrices of integer values. The matrices’ dimensions and their elements are read from the standard input. Write the functions that display:
  2. a) the positions of the odd elements in each matrix;
  3. b) the sum of all even elements in both matrices;
  4. c) the sum of the two initial matrices;
  5. Author: Adorjan Istvan
  6. Date:30/11/2017*/
  7. #define _CRT_SECURE_NO_WARNINGS
  8. #include <stdio.h>
  9. #include <conio.h>
  10.  
  11. using namespace std;
  12. int position(int*[]); // function for the position of the odd numbers
  13. int sum(int*[]); // function for the sum of all even numbers
  14. int matrixsum(int*[], int*[]); // function for the sum of two matrices
  15. int print(int*[]); // function to print this two matrix
  16.  
  17. void main()
  18. {
  19. int a[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
  20. int b[3][4] = { 12,13,14,15,16,17,18,19,20,21,22,23 };
  21. int *p, *o;
  22. p = a[0];
  23. o = b[0];
  24. printf("\n Our first matrix looks like: \n");
  25. print(p);
  26. printf("\n Our second matrix looks like: \n");
  27. print(o);
  28. printf("\n Let's find the odd elements int the first matrix: ");
  29. position(p);
  30. printf("\n\n Let's find the odd elements int the second matrix: ");
  31. position(o);
  32. printf("\n\n The sum of the even numbers in both matrices is equal with: %d\n", sum(p) + sum(o));
  33. printf("\n The sum of this two matrices is equal with: \n");
  34. matrixsum(p, o);
  35.  
  36. _getch();
  37. }
  38. int position(int* p)
  39. {
  40. int i, j;
  41.  
  42. for (i = 0; i<3; ++i)
  43. {
  44. for (j = 0; j<4; ++j)
  45. {
  46. if (*(*(p + i) + j) % 2 != 0)//Checking odd elements.
  47. {
  48. printf("\n %d row %d collum, which is: %d", i + 1, j + 1, *(*(p + i) + j));
  49. }
  50. }
  51. }
  52. return 0;
  53. }
  54. int sum(int* p)
  55. {
  56. int sum = 0;
  57. int i, j;
  58. for (i = 0; i<3; ++i)
  59. {
  60. for (j = 0; j<4; ++j)
  61. {
  62. if (*(*(p + i) + j) % 2 == 0)//Checking odd elements.
  63. {
  64. sum += *(*(p + i) + j);
  65. }
  66. }
  67. }
  68. return sum;
  69. }
  70. int matrixsum(int* p, int* o)
  71. {
  72. int i, j;
  73. int sum[3][4];
  74. int* f;
  75. f = sum[0];
  76. for (i = 0; i < 3; i++) {
  77. for (j = 0; j < 4; j++) {
  78. *f = *(*(p + i) + j) + *(*(o + i) + j); // Calculating the sum of the elements.
  79. printf("%d\t", (*(f + i) + j));
  80. }
  81. printf("\n");
  82. }
  83. return 0;
  84. }
  85. int print(int* p[])
  86. {
  87. int i, j;
  88. for (i = 0; i < 3; i++) {
  89. for (j = 0; j < 4; j++) {
  90. printf("%d\t", *(*(p + i) + j));
  91. }
  92. printf("\n");
  93. }
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement