Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. /*Даны матрицы А(3,5), В(5,2),. Для каждой матрицы вычислить суммы строк
  2. Оформить в виде отдельной функции.*/
  3.  
  4. /*new позволяет выделять только одномерные массивы, поэтому для работы
  5. с многомерными массивами необходимо воспринимать их как массив указателей на другие массивы.*/
  6.  
  7. #include "stdafx.h"
  8. #include "iostream"
  9. #include "conio.h"
  10. #include "locale.h"
  11. using namespace std;
  12.  
  13. int *input(int **mass,const int sizeI, int sizeJ) {
  14.  
  15. for (int i = 0; i < sizeI; i++) {
  16. mass[i] = new int[sizeJ];
  17. }
  18.  
  19. for (int i = 0; i < sizeI; i++) {
  20. for (int j = 0; j < sizeJ; j++) {
  21. mass[i][j] = i + j;
  22. }
  23. }
  24. return *mass;
  25. }
  26.  
  27. int *calculation(int **mass, const int sizeI, int sizeJ) {
  28. int *summ = new int[sizeI];
  29. for (int i = 0; i < sizeI; i++) {
  30. for (int j = 0; j < sizeJ; j++) {
  31. summ[i] += mass[i][j];
  32. }
  33. }
  34. return summ;
  35. }
  36.  
  37. int main()
  38. {
  39. //Размеры массивов
  40. const int sizeAI = 3, sizeAJ = 5;
  41. const int sizeBI = 5, sizeBJ = 2;
  42.  
  43. //Создание массивов
  44. int **A = new int *[sizeAI];
  45. int **B = new int *[sizeBI];
  46.  
  47. //Ввод данных в массивы
  48. *A = input(A, sizeAI, sizeAJ);
  49. *B = input(B, sizeBI, sizeBJ);
  50.  
  51. //Массивы для суммы строк
  52. int *summA = new int[sizeAI];
  53. int *summB = new int[sizeBI];
  54.  
  55. //Вычисляем сумму строк
  56. summA = calculation(A, sizeAI, sizeAJ);
  57. summB = calculation(B, sizeBI, sizeBJ);
  58.  
  59. for (int i = 0; i < sizeAI; i++) {
  60. cout << endl;
  61. for (int j = 0; j < sizeAJ; j++) {
  62. cout << A[i][j] << " ";
  63. }
  64. }
  65.  
  66. cout << summA[1];
  67.  
  68.  
  69.  
  70. _getch();
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement