Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Даны матрицы А(3,5), В(5,2),. Для каждой матрицы вычислить суммы строк
- Оформить в виде отдельной функции.*/
- /*new позволяет выделять только одномерные массивы, поэтому для работы
- с многомерными массивами необходимо воспринимать их как массив указателей на другие массивы.*/
- #include "stdafx.h"
- #include "iostream"
- #include "conio.h"
- #include "locale.h"
- using namespace std;
- int *input(int **mass,const int sizeI, int sizeJ) {
- for (int i = 0; i < sizeI; i++) {
- mass[i] = new int[sizeJ];
- }
- for (int i = 0; i < sizeI; i++) {
- for (int j = 0; j < sizeJ; j++) {
- mass[i][j] = i + j;
- }
- }
- return *mass;
- }
- int *calculation(int **mass, const int sizeI, int sizeJ) {
- int *summ = new int[sizeI];
- for (int i = 0; i < sizeI; i++) {
- for (int j = 0; j < sizeJ; j++) {
- summ[i] += mass[i][j];
- }
- }
- return summ;
- }
- int main()
- {
- //Размеры массивов
- const int sizeAI = 3, sizeAJ = 5;
- const int sizeBI = 5, sizeBJ = 2;
- //Создание массивов
- int **A = new int *[sizeAI];
- int **B = new int *[sizeBI];
- //Ввод данных в массивы
- *A = input(A, sizeAI, sizeAJ);
- *B = input(B, sizeBI, sizeBJ);
- //Массивы для суммы строк
- int *summA = new int[sizeAI];
- int *summB = new int[sizeBI];
- //Вычисляем сумму строк
- summA = calculation(A, sizeAI, sizeAJ);
- summB = calculation(B, sizeBI, sizeBJ);
- for (int i = 0; i < sizeAI; i++) {
- cout << endl;
- for (int j = 0; j < sizeAJ; j++) {
- cout << A[i][j] << " ";
- }
- }
- cout << summA[1];
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement