Advertisement
Yanislav29

Untitled

Nov 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. // ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <ctime>
  7. #include <cmath>
  8. #include <stdlib.h>
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13. srand(time(NULL));
  14. int currentIndex = 0;
  15. int sum = 0;
  16. int rowCounter = 0;
  17. double **darr2 = NULL;
  18. size_t rows, cols;
  19. cout << "Please senter number of rows: ";
  20. cin >> rows;
  21. cout << "Please senter number of cols: ";
  22. cin >> cols;
  23.  
  24. int end = 122, begin = -12;
  25. darr2 = new double*[rows];
  26. for (int i = 0; i < rows; i++)
  27. {
  28. darr2[i] = new double[cols];
  29. }
  30. for (int i = 0; i < rows; i++) {
  31. for (int j = 0; j < cols; j++)
  32. {
  33. darr2[i][j] = (double)rand() / RAND_MAX * (end - begin) + begin;
  34. }
  35.  
  36. }
  37. cout << "Matrix: \n";
  38. for (int i = 0; i < rows; i++)
  39. {
  40. for (int j = 0; j < cols; j++)
  41. {
  42. cout << darr2[i][j] << "\t\t";
  43. }
  44. cout << endl;
  45. }
  46.  
  47. cout << "Matrix main diagonal:" << endl;
  48. for (int i = 0; i < rows; i++){
  49. cout << darr2[i][i] << "\t\t";
  50. }
  51. cout << endl;
  52.  
  53. cout << "Matrix secondary diagonal: \n";
  54. for (int i = 0; i < rows; i++)
  55. {
  56. cout << darr2[i][cols - i - 1] << "\t\t";
  57.  
  58. }
  59. cout << endl;
  60.  
  61. cout << "ZADACHA 2:" << endl;
  62. for (int i = 0; i < rows; i++)
  63. {
  64. for (int j = 0; j < cols; j++)
  65. {
  66. if ((i + j) < rows - 1){
  67. cout << "0" << "\t\t";
  68. }
  69. else {
  70. cout << darr2[i][j] << "\t\t";
  71. }
  72. }
  73. cout << endl;
  74. }
  75.  
  76. cout << endl;
  77. cout << endl;
  78. cout << endl;
  79.  
  80.  
  81.  
  82. int indexj = 0;
  83. cout << "ZADACHA 3:" << endl;
  84. for (int i = 0; i < rows; i++)
  85. {
  86. for (int j = 0; j < cols; j++) {
  87. if (i > j) {
  88.  
  89.  
  90. darr2[i][j]= pow(i + j,i);
  91.  
  92.  
  93.  
  94. }
  95. }
  96.  
  97. }
  98. for (int i = 0; i < rows; i++)
  99. {
  100. for (int j = 0; j < cols; j++) {
  101. if (i > j) {
  102.  
  103.  
  104. cout << darr2[i][j] << "\t"\t";
  105.  
  106.  
  107.  
  108. }
  109. }
  110.  
  111. }
  112.  
  113. cout << endl;
  114. cout << endl;
  115. cout << endl;
  116.  
  117.  
  118. cout << "ZADACHA 4:" << endl;
  119. cout << "Print Matrix as a spiral: \n";
  120.  
  121.  
  122. int i, k = 0, l = 0, m = rows, n = cols;
  123. /*
  124. k - индекс на началния ред
  125. m - индекс на крайния ред
  126. l - индекс на началната колона
  127. n - индекс на крайната колона
  128. i - итератор
  129. */
  130. while (k < m && l < n) {
  131. //печатане на първия от оставащите редове
  132. for (i = l;i < n;i++) {
  133. cout << darr2[k][i] << "\t\t";
  134. }
  135. k++;
  136. //печатане на последната колона от оставащите редове
  137. for (i = k; i < m;i++) {
  138. cout << darr2[i][n - 1] << "\t\t";
  139. }
  140. n--;
  141. if (k < m) {//печатане на последния ред от оставащите редове
  142. for (i = n - 1; i >= l; --i)
  143. {
  144. cout << darr2[m - 1][i] << "\t\t";
  145. }
  146. m--;
  147. }
  148. //печатане на първа колона от оставащите колони
  149. if (l < n) {
  150. for (i = m - 1; i >= k; --i)
  151. {
  152. cout << darr2[i][l] << "\t\t";
  153. }
  154. l++;
  155. }
  156. cout << endl;
  157. }
  158.  
  159.  
  160. for (int i = 0; i < rows; i++)
  161. {
  162. delete[] darr2[i];
  163. }
  164.  
  165. delete[] darr2;
  166. darr2 = NULL;
  167.  
  168.  
  169.  
  170.  
  171.  
  172. return 0;
  173.  
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement