[ create a new paste ] login | about

Link: http://codepad.org/MbGx4k76    [ 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");
	}
}
void HoanVi(int &a, int &b)
{
	a = a - b;  // a = a - b
	b = a + b;  // b = a
	a = b - a;  // a = b
}
// Sắp xếp mảng 1 chiều tăng dần
void SapTangDan(int a[], int n)
{
	for(int i = 0; i < n - 1; i++)
	{
		for(int j = i + 1; j < n; j++)
		{
			if(a[i] > a[j])
			{
				HoanVi(a[i],a[j]);
			}
		}
	}
}

// Dùng mảng phụ
void SapXepCach1(int a[][MAX], int dong, int cot)
{
	// Bước 1: đổ mảng 2 chiều sang mảng 1 chiều
	int b[MAX]; // Mảng 1 chiều
	int idx = 0;
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			b[idx++] = a[i][j];
		}
	}
	// Bước 2: Sắp tăng dần
	SapTangDan(b,idx); // idx là số phần tử của mảng 1 chiều
	// Bước 3: Đổ dữ liệu từ mảng 1 chiều sau khi đã sắp xếp vào  lại mảng 2 chiều

	idx = 0; // reset lại

	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			a[i][j] = b[idx++];
		}
	}
}
// Không dùng mảng phụ
void SapXepCach2(int a[][MAX],int dong, int cot)
{
	int n = dong * cot;
	for(int i = 0; i < n - 1; i++)
	{
		for(int j = i + 1; j < n; j++)
		{
			if(a[i / cot][i % cot] > a[j / cot][j % cot])
			{
				HoanVi(a[i / cot][i % cot],a[j / cot][j % cot]);
			}
		}
	}
}
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);

	printf("\nSap xep mang 2 chieu tang dan cach 1\n");
	SapXepCach1(a,dong,cot);
	XuatMang(a,dong,cot);

	printf("\nSap xep mang 2 chieu tang dan cach 2\n");
	SapXepCach2(a,dong,cot);
	XuatMang(a,dong,cot);

	getch();
	return 0;
}


Output:
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
t.c: At top level:
Line 48: error: expected ';', ',' or ')' before '&' token
In function 'SapTangDan':
Line 57: error: 'for' loop initial declaration used outside C99 mode
Line 59: error: 'for' loop initial declaration used outside C99 mode
In function 'SapXepCach1':
Line 75: error: 'for' loop initial declaration used outside C99 mode
Line 77: error: 'for' loop initial declaration used outside C99 mode
Line 88: error: redefinition of 'i'
Line 75: error: previous definition of 'i' was here
Line 88: error: 'for' loop initial declaration used outside C99 mode
Line 90: error: 'for' loop initial declaration used outside C99 mode
In function 'SapXepCach2':
Line 100: error: 'for' loop initial declaration used outside C99 mode
Line 102: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: