Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*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:
- a) the positions of the odd elements in each matrix;
- b) the sum of all even elements in both matrices;
- c) the sum of the two initial matrices;
- Author: Adorjan Istvan
- Date:30/11/2017*/
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <conio.h>
- using namespace std;
- int position(int*[]); // function for the position of the odd numbers
- int sum(int*[]); // function for the sum of all even numbers
- int matrixsum(int*[], int*[]); // function for the sum of two matrices
- int print(int*[]); // function to print this two matrix
- void main()
- {
- int a[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
- int b[3][4] = { 12,13,14,15,16,17,18,19,20,21,22,23 };
- int *p, *o;
- p = a[0];
- o = b[0];
- printf("\n Our first matrix looks like: \n");
- print(p);
- printf("\n Our second matrix looks like: \n");
- print(o);
- printf("\n Let's find the odd elements int the first matrix: ");
- position(p);
- printf("\n\n Let's find the odd elements int the second matrix: ");
- position(o);
- printf("\n\n The sum of the even numbers in both matrices is equal with: %d\n", sum(p) + sum(o));
- printf("\n The sum of this two matrices is equal with: \n");
- matrixsum(p, o);
- _getch();
- }
- int position(int* p)
- {
- int i, j;
- for (i = 0; i<3; ++i)
- {
- for (j = 0; j<4; ++j)
- {
- if (*(*(p + i) + j) % 2 != 0)//Checking odd elements.
- {
- printf("\n %d row %d collum, which is: %d", i + 1, j + 1, *(*(p + i) + j));
- }
- }
- }
- return 0;
- }
- int sum(int* p)
- {
- int sum = 0;
- int i, j;
- for (i = 0; i<3; ++i)
- {
- for (j = 0; j<4; ++j)
- {
- if (*(*(p + i) + j) % 2 == 0)//Checking odd elements.
- {
- sum += *(*(p + i) + j);
- }
- }
- }
- return sum;
- }
- int matrixsum(int* p, int* o)
- {
- int i, j;
- int sum[3][4];
- int* f;
- f = sum[0];
- for (i = 0; i < 3; i++) {
- for (j = 0; j < 4; j++) {
- *f = *(*(p + i) + j) + *(*(o + i) + j); // Calculating the sum of the elements.
- printf("%d\t", (*(f + i) + j));
- }
- printf("\n");
- }
- return 0;
- }
- int print(int* p[])
- {
- int i, j;
- for (i = 0; i < 3; i++) {
- for (j = 0; j < 4; j++) {
- printf("%d\t", *(*(p + i) + j));
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement