[ create a new paste ] login | about

Link: http://codepad.org/Hvog1kny    [ raw code | output | fork ]

C, pasted on Apr 26:
#include<stdio.h>
#include<conio.h>
#define MAX 100

void NhapMang(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("\nNhap vao phan tu a[%d] = ", i);
		scanf_s("%d", &a[i]);
	}
}
void XuatMang(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%4d", a[i]);
	}
}
/*
 Ý tưởng: Đứng từ 1 vị trí, xét lùi lại các vị trí trước đó, nếu phát hiện bị trùng 
 thì lập tức cập nhật cờ hiệu và dừng quá trình xét lại. Đến cuối cùng kiểm tra
 cờ hiệu để biết có bị trùng hay không
*/
void LietKeVaDemPhanBiet(int a[], int n)
{
	// Biến đếm số lượng phân biệt
	int dem = 1;  // Mặc định có sẵn a[0]
	printf("\%4d", a[0]);
	for (int i = 1; i < n; i++)
	{
		int Check = 1;
		for (int j = i - 1; j >= 0; j--) 
		{
			if (a[i] == a[j])
			{
				Check = 0;
				break;
			}
		}
		if (Check == 1) // Nếu không bị trùng
		{
			dem++;
			printf("\%4d", a[i]);
		}
    }
	printf("\nCo %d gia tri phan biet", dem);
}
int main()
{
	int n;

	do
	{
		printf("\nNhap vao so luong phan tu cua mang: ");
		scanf_s("%d", &n);

		if (n < 0 || n > MAX)
		{
			printf("\nSo luong phan tu khong hop le. Xin kiem tra lai !");
		}
	} while (n < 0 || n > MAX);

	int a[MAX];
	NhapMang(a, n);
	XuatMang(a, n);

	printf("\nCac gia tri phan biet trong mang la:");
	LietKeVaDemPhanBiet(a, n);

	_getch();
	return 0;

}


Output:
1
2
3
4
5
6
7
8
Line 17: error: conio.h: No such file or directory
In function 'NhapMang':
Line 7: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 15: error: 'for' loop initial declaration used outside C99 mode
In function 'LietKeVaDemPhanBiet':
Line 30: error: 'for' loop initial declaration used outside C99 mode
Line 33: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: