Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions HomeWork4/4Hw1exKobzystyiMaksym
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>

using namespace std;

int main() {
int *arr;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

обьявляйте этот указатель после того как проверили введенный размер пользователя

int size;

cout << "n = ";
cin >> size;

if (size <= 0) {

cerr << "Invalid size" << endl;
return 1;
}

arr = new int[size];

for (int i = 0; i < size; i++) {
cout << "arr[" << i << "] = ";
cin >> arr[i];
}

int temp;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вносите эту переменную в область видимости цикла, а не держите ее снаружи, просто необходимо стараться ограничивать область видимости переменной


for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// change # elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

там если мне память не ошибает необходимо использовать понятие компаратора


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;

delete[] arr; // delete memory

int value;
cin >> value;
return 0;
}