[ create a new paste ] login | about

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

C, pasted on May 19:
#include<stdio.h>
#include<conio.h>
#include<math.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]);

	}
}

void TaoMang(int a[], int n, int b[], int x)
{
	for (int i = 0; i < n; i++)
	{
		b[i] = abs(x - a[i]);
	}
}

int TimMax(int b[], int n)
{
	int Max = b[0];
	for (int i = 1; i < n; i++)
	{
		if (b[i] > Max)
		{
			Max = b[i];
		}
	}
	return Max;
}

void XuatKetQua(int a[], int b[], int n)
{
	printf("\nGia tri trong mang xa gia tri x nhat la: ");
	int Max = TimMax(b, n);
	for (int i = 0; i < n; i++)
	{
		if (b[i] == Max)
		{
			printf("%4d", a[i]); // Đối chiếu qua mảng a
		}
	}
}
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);

	int x;
	printf("\nNhap vao gia tri x: ");
	scanf_s("%d", &x);

	int b[MAX];
	TaoMang(a, n, b, x);
	printf("\nKhoang cach tu x = %d den cac phan tu trong mang la: ",x);
	XuatMang(b, n);
	XuatKetQua(a, b, n);
	

	

	_getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
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 'TaoMang':
Line 24: error: 'for' loop initial declaration used outside C99 mode
In function 'TimMax':
Line 33: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatKetQua':
Line 47: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: