Answer the question
In order to leave comments, you need to log in
What overflows the array?
Good day! The compiler throws an error
Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted.
#include <iostream>
#include <time.h>
using namespace std;
#define N 9
#define M 6
void matrixsort(unsigned int *arr) {
int temp;
for (int i = 0; i < M - 1; i++) {
for (int j = 0; j < N - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}
void showmatrix(unsigned int arr[N][M], string s) {
string str = "Matrica " + s + " sortirovki:";
cout << endl << str << endl << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main() {
unsigned int arr0[N];
unsigned int arr[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
arr[i][j] = rand() % 50;
}
}
showmatrix(arr, "do");
int line = 0;
while (line < N) {
for (int j = line; j < line + 1; j++) {
for (int i = 0; i < N; i++) {
arr0[i] = arr[i][j];
}
arr0[M] = line;
matrixsort(arr0);
for (int i = 0; i < N; i++) {
arr[i][j] = arr0[i];
}
}
line++;
}
showmatrix(arr, "posle");
return 0;
}
Answer the question
In order to leave comments, you need to log in
void matrixsort(unsigned int *arr) { int temp; for (int i = 0; i < M - 1; i++) {
for (int i = 0; i < N - 1; i++)
because this is sorting, not matrix traversal.
while (line < N) { for (int j = line; j < line + 1; j++) {
for (int j = 0; j < M; j++) {
arr0[M] = line;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question