[ create a new paste ] login | about

Link: http://codepad.org/jNCeH6Yi    [ raw code | output | fork | 3 comments ]

C, pasted on Oct 18:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100

void nhap (int a[], int &n)
{
	do
	{
		printf("\nNhap so phan tu: ");
		scanf("%d", &n);
		if(n <= 0 || n > MAX)
		{
			printf("\nSo phan tu khong hop le. Xin kiem tra lai !");
		}
	}while(n <= 0 || n > MAX);
	for(int i = 0; i < n; i++)
	{
		printf("\nNhap a[%d]: ", i);
		scanf("%d", &a[i]);
	}
}

void xuat(int a[], int n)
{
	for(int i = 0; i < n; i++)
	{
		printf("%4d", a[i]);
	}
}

void HoanVi(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

void SapXepTangDan(int a[], int n)
{
	for(int i = 0; i < n; i++)
	{
		for(int j = i + 1; j < n; j++)
		{
			if(a[i] > a[j])
			{
				HoanVi(a[i], a[j]);
			}
		}
	}
}
/*
Cho chạy từ cuối về đầu, nếu thấy số nào lớn hơn x thì cho nó dịch về sau 1 vị trí.
*/
void ChenXVaoMangTang(int a[], int &n, int x)
{
	int i, j;
	for(i = 0; i < n; i++)
	{
		if(x < a[i])
		{
			int temp = x;
			for(j = n; j > i; j--)
			{
				a[j] = a[j - 1];
			}
			a[i] = temp;
			break;
		}
	}
	n++;
}
int main()
{
	int n;
	int a[MAX];

	nhap(a, n);
	xuat(a, n);
	SapXepTangDan(a, n);
	int PhanTuThem;


	printf("\nNhap vao phan tu can them: ");
	scanf("%d", &PhanTuThem);

	ChenXVaoMangTang(a, n, PhanTuThem);
	printf("\nMang sau khi them:\n");
	xuat(a, 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
Line 6: error: expected ';', ',' or ')' before '&' token
In function 'xuat':
Line 26: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 32: error: expected ';', ',' or ')' before '&' token
In function 'SapXepTangDan':
Line 41: error: 'for' loop initial declaration used outside C99 mode
Line 43: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 55: error: expected ';', ',' or ')' before '&' token


Create a new paste based on this one


Comments:
posted by karimhoang on Jul 26
Cái thuật toán thêm phần tử thiếu trường hợp x lớn hơn tất cả giá trị trong mảng rồi ạ
reply
posted by karimhoang on Jul 26
Thuật toán thêm giá trị x bị thiếu trường hợp giá trị x lớn nhất trong mảng rồi ạ
reply
posted by huybean98 on Dec 22
void ChenXVaoMangTang(int a[], int &n, int x)
{
int i, j;
for(i = 0; i < n; i++)
{
if(x < a[i])
{
int temp = x;
for(j = n; j > i; j--)
{
a[j] = a[j - 1];
}
a[i] = temp;
break;
}
} a[n]=x;
n++;
}
thuat toan cho luon truong hop x lon hon tat cac cacc phan tu trong mang
reply