Function

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include"header.

h"

// 1. nhap mang
void input(int a[], int& n) {
while (true) {
cout << "Nhap so luong phan tu cua mang: ";
cin >> n;
if (n <= 0)
cout << "Vui long nhap n>0.\n";
else
break;
}
for (int i = 0; i < n; i++) {
cout << "Nhap a[" << i << "] = ";
cin >> a[i];
}
}

// 2. chia mang ban dau thanh mang chan va mang le


void segregateOddEven(int a[], int n, int odd[], int& odd_size, int even[], int&
even_size) {
int odd_idx = 0, even_idx = 0;
for (int i = 0; i < n; i++) {
if (a[i] % 2 == 0) {
even[even_idx] = a[i];
even_idx++;
}
else
{
odd[odd_idx] = a[i];
odd_idx++;
}
}
odd_size = odd_idx;
even_size = even_idx;
}

// kiem tra so nguyen to


bool checkPrime(int x) {
if (x < 2)
return false;
else
{
for (int i = 2; i < x; i++) {
if (x % i == 0)
return false;
}
return true;
}
}

// 3. tim tat ca phan tu la so nguyen to va dat vao mang so nguyen to


void createPrimeArr(int a[], int n, int primeArr[], int& p_size) {
int index = 0;
for (int i = 0; i < n; i++) {
if (checkPrime(a[i])) {
primeArr[index] = a[i];
index++;
}
}
p_size = index;
}

// 4. thay the so nguyen to thanh so 0


void replacePrimeWithZero(int a[], int n) {
for (int i = 0; i < n; i++) {
if (checkPrime(a[i])) {
a[i] = 0;
}
}
}

// xoa phan tu
void removeElement(int a[], int& n, int index) {
for (int i = index; i < n - 1; i++) {
a[i] = a[i + 1];
}
n--;
}

// 5. xoa tat ca so nguyen nguyen to


void removePrimeNum(int a[], int& n) {
int tmp = 0;
for (int i = 0; i < n; i++) {
if (checkPrime(a[i])) {
removeElement(a, n, i);
i--;
}
}
}

// them so 0 vao truoc 1 phan tu


void insertZeroBeforeElement(int a[], int& n, int index) {
for (int i = n; i > index; i--) {
a[i] = a[i - 1];
}
a[index] = 0;
n++;
}

// 6. them so 0 vao truoc so nguyen to


void insertZeroBeforePrimeNum(int a[], int& n) {
for (int i = 0; i < n; i++) {
if (checkPrime(a[i])) {
insertZeroBeforeElement(a, n, i);
i = i + 1;
}
}
}

void swap(int& a, int& b)


{
int temp = a;
a = b;
b = temp;
}
// 7. sap xep tang dan
void ascendingSort(int a[], int n) {
// selection sort
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[min_idx])
min_idx = j;
swap(a[min_idx], a[i]);
}
}

// 8. sap xep giam dan


void descendingSort(int a[], int n) {
int i, j, max_idx;
for (i = 0; i < n - 1; i++)
{
max_idx = i;
for (j = i + 1; j < n; j++)
if (a[j] > a[max_idx])
max_idx = j;
swap(a[max_idx], a[i]);
}
}

// 9. Sap xep mang theo kieu so duong giam dan - so am tang dan
void positiveAsc_NegativeDes_Sort(int a[], int n) {
int positive[100];
int p_idx = 0;
int negative[100];
int n_idx = 0;

for (int i = 0; i < n; i++) {


if (a[i] >= 0) {
positive[p_idx] = a[i];
p_idx++;
}
else if (a[i] < 0)
{
negative[n_idx] = a[i];
n_idx++;
}
}

descendingSort(positive, p_idx);
ascendingSort(negative, n_idx);

for (int i = 0; i < n; i++) {


if (i < p_idx)
a[i] = positive[i];
else
{
a[i] = negative[i - p_idx];
}
}
}
// 10. xuat mang
void output(int a[], int n) {
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}

// copy mang
void copyArr(int a[], int n, int tmp[], int& t_size) {
t_size = n;
for (int i = 0; i < n; i++) {
tmp[i] = a[i];
}
}

You might also like