[ create a new paste ] login | about

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

C, pasted on Jun 8:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100

// Hợp lệ: Trả về 1, không hợp lệ: Trả về 0
int KiemTraSoNguyen(char *x)
{
	int Check = 1; // Khởi tạo
	fflush(stdin);

	// Kiểm tra chuỗi hợp lệ
	int length = strlen(x);
	
	for(int i = 0; i < length; i++)
	{
		if(x[i] < '0' || x[i] > '9')
		{
			Check = 0;
			break;
		}
	}
	return Check;
}
void NhapMang(int a[][MAX], int dong, int cot)
{
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			printf("\nNhap vao a[%d][%d] = ", i, j);
			scanf("%d", &a[i][j]);
		}
	}
}
void XuatMang(int a[][MAX], int dong, int cot)
{
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			printf("\%4d", a[i][j]);
		}
		printf("\n\n");
	}
}
int TimKiem(int a[][MAX], int dong, int cot, int x)
{
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			if(a[i][j] == x)
			{
				return 1;
			}
		}
	}
	return 0;
}
int TimKiem_Cach2(int a[][MAX], int dong, int cot, int x)
{
	for(int i = 0; i < dong * cot; i++)
	{
		if(a[i / cot][i % cot] == x)
		{
			return 1;
		}
	}
	return 0;
}
int main()
{
	// int a[2][3] = {{1,2,3}, {4, 5, 6}};
  //  XuatMang(a,2,3);

	int a[MAX][MAX], dong, cot;

	// Nhập số dòng
	char sodong[30];
	int CheckDong;
	do{
	fflush(stdin);
	printf("\nNhap vao so dong: ");
	gets(sodong);

	 CheckDong = KiemTraSoNguyen(sodong);

	if(CheckDong == 0)
	{
		printf("\nKieu du lieu khong hop le. Xin kiem tra lai !");
	}
	else
	{
		dong = atoi(sodong);
		if(dong < 1 || dong > MAX)
		{
			CheckDong = 0;
			printf("\nSo dong khong hop le. Xin kiem tra lai !");
		}
	}
	}while(CheckDong == 0);
	// Nhập số cột
	do
	{
		printf("\nNhap vao so cot: ");
		scanf("%d", &cot);

		if(cot < 1 || cot > MAX)
		{
			printf("\nSo cot khong hop le. Xin kiem tra lai !");
		}
	}while(cot < 1 || cot > MAX);

	NhapMang(a,dong,cot);
	XuatMang(a,dong,cot);

	int x;
	printf("\nNhap vao phan tu can tim kiem: ");
	scanf("%d", &x);

	int Check = TimKiem(a,dong,cot,x);
	if(Check == 1)
	{
		printf("\nCo ton tai");
	}
	else
	{
		printf("\nKhong ton tai");
	}


	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Line 17: error: conio.h: No such file or directory
In function 'KiemTraSoNguyen':
Line 16: error: 'for' loop initial declaration used outside C99 mode
In function 'NhapMang':
Line 28: error: 'for' loop initial declaration used outside C99 mode
Line 30: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 39: error: 'for' loop initial declaration used outside C99 mode
Line 41: error: 'for' loop initial declaration used outside C99 mode
In function 'TimKiem':
Line 50: error: 'for' loop initial declaration used outside C99 mode
Line 52: error: 'for' loop initial declaration used outside C99 mode
In function 'TimKiem_Cach2':
Line 64: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: