Created
March 12, 2020 01:58
-
-
Save tranduydat/c4d5f4a3cf46b4ab78534f1028455479 to your computer and use it in GitHub Desktop.
Exercise 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ex 6 | |
#include <stdio.h> | |
void enterArray(int *a, int &n) | |
{ | |
printf("How many elements do you want to put into this array?\nEnter n: "); | |
scanf("%d", &n); | |
for (int i = 0; i < n; i++) | |
{ | |
printf("Enter a[%d]: ", i); | |
scanf("%d", &a[i]); | |
} | |
} | |
void insertP (int *a, int &n, int &p, int &q) | |
{ | |
printf("Nhap vi tri: "); | |
scanf("%d", &p); | |
printf("Nhap gia tri cua a[%d]: ", p); | |
scanf("%d", &q); | |
for (int i = n - 1; i >= p; i--) | |
a[i + 1] = a[i]; | |
a[p] = q; | |
n++; | |
} | |
void del(int *a, int &n, int p) | |
{ | |
for (int i = p; i < n; i++) | |
a[i] = a[i + 1]; | |
n--; | |
} | |
void trunglap(int *a, int &n) | |
{ | |
for (int i = 0; i < n - 1; i++) | |
for (int j = i + 1; j < n; j++) | |
if (a[i] == a[j]) | |
del(a, n, j); | |
} | |
void showArray(int *a, int n) | |
{ | |
for (int i = 0; i < n; i++) | |
printf("a[%d] = %d\n", i, a[i]); | |
} | |
int main() | |
{ | |
int n, p, q; | |
int a[100]; | |
enterArray(a, n); | |
printf("Mang da them: \n"); | |
showArray(a, n); | |
printf("\nMang da xoa: \n"); | |
trunglap(a, n); | |
showArray(a, n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment