#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int **CreateMatrix (int rows, int cols);
void FillMatrix (int **matrix, int rows, int cols);
void OutputMatrix (int **matrix, int rows, int cols);
void SortMatrix (int **matrix, int rows, int cols);
void DellMatrix(int **matrix, int rows);
int main(){
srand(time(NULL));
int rows = 9, cols = 9;
int **matrix = CreateMatrix(rows, cols);
FillMatrix(matrix, rows, cols);
cout << "Цикл до сортування: " << endl;
OutputMatrix(matrix, rows, cols);
cout << endl;
SortMatrix(matrix, rows, cols);
cout << "Цикл після сортування: " << endl;
OutputMatrix(matrix, rows, cols);
DellMatrix(matrix, rows);
system("pause");
return 0;
}
int **CreateMatrix(int rows, int cols){
int **matrix = new int *[rows];
for (int i = 0; i < rows; i )
{
matrix[i] = new int [cols];
}
return matrix;
}
void FillMatrix(int **matrix, int rows, int cols){
int N = 19;
for (int i = 0; i < rows; i ) {
for (int j = 0; j < cols; j ) {
matrix[i][j] = N;
}
}
for (int i = rows - 1; i > rows / 2 - 1; i--)
{
for (int k = cols - 1 - i; k < 1 i; k )
{
matrix[i][k] = rand() % 100;
}
}
}
void OutputMatrix(int **matrix, int rows, int cols){
for (int i = 0; i < rows; i )
{
for (int j = 0; j < cols; j )
{
cout << setw(5) << matrix[i][j];
}
cout << endl;
}
}
void SortMatrix (int **matrix, int rows, int cols){ //selection sort
int max, maxi;
for (int k = rows - 1; k > rows / 2 - 1; k--)
{
for (int i = cols - 1 - k; i < 1 k; i )
{
max = matrix[k][i];
maxi = i;
for (int j = i 1; j <= k; j )
{
if (matrix[k][j] > max)
{
max = matrix[k][j];
maxi = j;
}
}
matrix[k][maxi] = matrix[k][i];
matrix[k][i] = max;
}
}
}
void DellMatrix(int **matrix, int rows){
for (int i = 0; i < rows; i )
{
delete matrix[i];
}
delete[] matrix;
}
Цикл до сортування:
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 64 19 19 19 19
19 19 19 64 47 94 19 19 19
19 19 59 5 54 70 4 19 19
19 56 21 49 45 17 87 61 19
48 3 5 33 40 70 84 46 68
Цикл після сортування:
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 64 19 19 19 19
19 19 19 94 64 47 19 19 19
19 19 70 59 54 5 4 19 19
19 87 61 56 49 45 21 17 19
84 70 68 48 46 40 33 5 3
Hi all, Task: The elements in the shaded area are generated randomly. All others = N. Arrange the elements in the shaded area in descending order. I did a sort in rows, but I need all elements in general to be sorted in descending order, i.e. at the top the biggest elements, below the smallest. I can not figure out how to re-do it, hope for your help! Thank you.
CodePudding user response:
I would do it like that:
int NumberOfValues(int cols)
{
int count = 0;
for (int i = cols; i > 0; i -=2)
count = i;
return count;
}
void FillMatrix(int **matrix, int rows, int cols){
int N = 19;
for (int i = 0; i < rows; i ) {
for (int j = 0; j < cols; j ) {
matrix[i][j] = N;
}
}
std::vector<int> random_numbers(NumberOfValues(cols));
for (auto &i : random_numbers)
i = rand() % 100;
std::sort(begin(random_numbers), end(random_numbers));
int index = 0;
for (int i = rows - 1; i > rows / 2 - 1; i--)
{
for (int k = cols - 1 - i; k < 1 i; k )
{
matrix[i][k] = random_numbers[index ];
}
}
}
make an array (a std::vector
is perfect for that) with the correct number of values in the pyramid (for 9 cols you have 9 values in the last row, 7 above that, 5 above the 7, 3 above the 5 and 1 above the 3, so 9 7 5 3 1), fill it with random numbers, sort it and then use these values instead of random numbers.
In your del
function you forgot to use delete[] matrix[i];
instead of delete matrix[i];
. A new[]
needs a delete[]
.
Running example: https://godbolt.org/z/o34vncYor
CodePudding user response:
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int **CreateMatrix (int rows, int cols);
void FillMatrix (int **matrix, int rows, int cols);
void OutputMatrix (int **matrix, int rows, int cols);
void SortMatrix (int **matrix, int rows, int cols);
void DellMatrix(int **matrix, int rows);
int main(){
srand(time(NULL));
int rows = 9, cols = 9;
int **matrix = CreateMatrix(rows, cols);
FillMatrix(matrix, rows, cols);
cout << "Цикл до сортування: " << endl;
OutputMatrix(matrix, rows, cols);
cout << endl;
cout << "Цикл після сортування: " << endl;
OutputMatrix(matrix, rows, cols);
DellMatrix(matrix, rows);
system("pause");
return 0;
}
int **CreateMatrix(int rows, int cols){
int **matrix = new int *[rows];
for (int i = 0; i < rows; i )
{
matrix[i] = new int [cols];
}
return matrix;
}
void FillMatrix(int **matrix, int rows, int cols){
int N = 19, size = 25;
int matrix2[size];
int tmp;
for (int l = 0; l < size; l ) {
matrix2[l] = rand() % 100;
}
for(int i = 0; i < size; i )
{
int pos = i;
tmp = matrix2[i];
for(int j = i 1; j < size; j )
{
if (matrix2[j] > tmp)
{
pos = j;
tmp = matrix2[j];
}
}
matrix2[pos] = matrix2[i];
matrix2[i] = tmp;
}
for (int p = 0; p < size; p ) {
cout << matrix2[p] << " ";
}
cout << endl;
for (int i = 0; i < rows; i ) {
for (int j = 0; j < cols; j ) {
matrix[i][j] = N;
}
}
for (int i = rows - 1; i > rows / 2 - 1; i--)
{
for (int k = cols - 1 - i; k < 1 i; k )
{
for (int j = 0; j < rows; j ) {
matrix[i][k] = matrix2[j];
}
}
}
}
void OutputMatrix(int **matrix, int rows, int cols){
for (int i = 0; i < rows; i )
{
for (int j = 0; j < cols; j )
{
cout << setw(5) << matrix[i][j];
}
cout << endl;
}
}
void DellMatrix(int **matrix, int rows){
for (int i = 0; i < rows; i )
{
delete matrix[i];
}
delete[] matrix;
}
98 97 92 92 89 89 81 78 76 74 69 31 31 29 26 22 21 17 17 9 6 4 1 0 0
Цикл до сортування:
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 76 19 19 19 19
19 19 19 76 76 76 19 19 19
19 19 76 76 76 76 76 19 19
19 76 76 76 76 76 76 76 19
76 76 76 76 76 76 76 76 76
Цикл після сортування:
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 19
19 19 19 19 76 19 19 19 19
19 19 19 76 76 76 19 19 19
19 19 76 76 76 76 76 19 19
19 76 76 76 76 76 76 76 19
76 76 76 76 76 76 76 76 76
This is another option, but it does not work correctly