[ create a new paste ] login | about

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

C, pasted on May 19:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
void NhapMang(int[], int);
void XuatMang(int[], int);
int KiemTraNguyenTo(int);
int KiemTraMangNguyenTo(int[], int n);
void NhapMang(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("\nNhap vao 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]);
	}
}
int TimKiemPhanTuCuoi(int a[], int n, int x)
{
	int ViTri = -1;
	for (int i = 0; i < n; i++)
	{
		if (a[i] == x)
		{
			ViTri = i;
		}
	}
	return ViTri;
}
int KiemTraNguyenTo(int n)
{
	if (n < 2)
	{
		return 0;
	}
	else if (n > 2)
	{
		if (n % 2 == 0)
		{
			return 0;
		}
	}
	for (int i = 3; i <= sqrt((double)n); i += 2)
	{
		if (n % i == 0)
		{
			return 0;
		}
	}
	return 1;
}
int KiemTraMangNguyenTo(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		if (KiemTraNguyenTo(a[i]) == 0)
		{
			return 0;
		}
	}
	return 1;
}
int ViTriNguyenTo(int a[], int n)
{
	int ViTri1 = 0;
	for (int i = 0; i < n; i++)
	{
		if (KiemTraNguyenTo(a[i]) == 1)
			 ViTri1 = i;
	}
	return ViTri1;
}
int TimSoNhoNhat(int a[], int n)
{
	int min = a[0];
	for (int i = 0; i < n; i++)
	{
		if (a[i] < min)
		{
			min = a[i];
		}
	}
	return min;
}
// tìm giá trị đương đầu
int DuongDau(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		if (a[i] > 0)
		{
			return a[i];
		}
	}
	return -1;
}
//Hàm tìm giá trị dương nhỏ nhất.
int DuongNhoNhat(int a[], int n)
{
	int lc = DuongDau(a, n);
	if (lc == -1)
	{
		return -1;
	}
	for (int i = 0; i < n; i++)
	{
		if (a[i] > 0 && a[i] < lc)
		{
			lc = a[i];
		}
	}
	return lc;
}
int main()
{

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

		if (n < 0 || n > MAX)
		{
			printf("\nGia tri nhap vao khong hop le. Xin vui long kiem tra lai !");
		}
	} while (n < 0 || n > MAX);

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

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

	// Cau a
	int ViTri = TimKiemPhanTuCuoi(a, n, x);
	if (ViTri != -1)
	{
		printf("\n%d xuat hien tai vi tri cuoi cung cua mang o vi tri: %d", x,ViTri);
	}
	else
	{
		printf("\n%d khong xuat hien trong mang",x);
	}
	
	// Cau b

	int ViTri1 = ViTriNguyenTo(a, n);
	int CheckNguyenTo = KiemTraMangNguyenTo(a, n);
	if (CheckNguyenTo == 0 && ViTri1 != 1)
	{
		printf("\nTon tai it nhat 1 so nguyen to trong mang");
		printf("\nVi tri so nguyen to dau tien trong mang la %d", ViTri1);
	}
	else
	{
		printf("\nKhong co so nguyen to trong mang");
	}
	
	

	// Cau c

	int min = TimSoNhoNhat(a, n);
	printf("\nPhan tu nho nhat trong mang la %d ", min);


	// Cau d
	int MinDuong = DuongNhoNhat(a, n);
	printf("\nSo duong nho nhat trong mang la %d", MinDuong);

	_getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Line 17: error: conio.h: No such file or directory
In function 'NhapMang':
Line 11: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 19: error: 'for' loop initial declaration used outside C99 mode
In function 'TimKiemPhanTuCuoi':
Line 27: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraNguyenTo':
Line 49: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraMangNguyenTo':
Line 60: error: 'for' loop initial declaration used outside C99 mode
In function 'ViTriNguyenTo':
Line 72: error: 'for' loop initial declaration used outside C99 mode
In function 'TimSoNhoNhat':
Line 82: error: 'for' loop initial declaration used outside C99 mode
In function 'DuongDau':
Line 94: error: 'for' loop initial declaration used outside C99 mode
In function 'DuongNhoNhat':
Line 111: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: