[ create a new paste ] login | about

Link: http://codepad.org/YQNwuqen    [ 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 TimMax(int a[][MAX], int dong, int cot)
{
	int max = a[0][0]; // Phần tử đầu tiên của mảng
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			if(a[i][j] > max)
			{
			   max = a[i][j];
			}
		}
	}
	return max;
}
int TimMax_Cach2(int a[][MAX],int dong, int cot)
{
	int Max = a[0][0];

	for(int i = 1; i < dong * cot; i++)
	{
		if(a[i / cot][i % cot] > Max)
		{
			Max = a[i / cot][i % cot];
		}
	}
	return Max;
}
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 Max = TimMax_Cach2(a,dong,cot);
	printf("\nMax = %d",Max);

	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 'TimMax':
Line 52: error: 'for' loop initial declaration used outside C99 mode
Line 54: error: 'for' loop initial declaration used outside C99 mode
In function 'TimMax_Cach2':
Line 68: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: