[ create a new paste ] login | about

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

C, pasted on Apr 26:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define MAX 100
void NhapMang(int[], int);
void XuatMang(int[], int);
void NhapMangNgauNhien(int[], int);
// n là số lượng phần tử cũa mãng
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 NhapMangNgauNhien(int a[], int n)
{
	srand(time(0));
	for (int i = 0; i < n; i++)
	{
		// [10,99]
		a[i] = 10 + rand() % 90;

	}
}
void XuatMang(int a[], int n)
{
	for (int i = 0; i < n; i++){
		printf("%4d", a[i]);
	}
}
int TraVeSoDao(int x)
{
	int SoDao = 0;
	while (x != 0)
	{
		SoDao = SoDao * 10 + x % 10;
		x /= 10;
	}
	return SoDao; 
}
// Nếu x đối xứng => trả về 1, ngược lại trả về 0
int KiemTraDoiXung(int x)
{
	return x == TraVeSoDao(x);
}
void LietKeDoiXung(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		if (KiemTraDoiXung(a[i]))
		{
			printf("\%4d", a[i]);
		}
	}
}

int KiemTraAmstrong(int x)
{
	int SoLuong = (int)log10((float)x) + 1;
	int Tong = 0; 
	int themang = x;
	while (themang != 0)
	{
		Tong += pow(themang % 10, (float)SoLuong);
		themang /= 10;
	}
	return Tong == x;

}
void LietKeVaTinhTongAmstrong(int a[], int n, int &Tong)
{
	Tong = 0;
	for (int i = 0; i < n; i++)
	{
		if (KiemTraAmstrong(a[i]))
		{
			Tong += a[i];
			printf("%4d", a[i]);
		}
	}
}
int main()
{
	/*int n = 153;
	int Check = KiemTraAmstrong(n);
	if (Check == 1)
	{
		printf("\nLa so Amstrong");
	}
	else
	{
		printf("\nKhong la so Amstrong");
	}*/
	//int Check = KiemTraDoiXung(n);
	//if (Check == 1)
	//{
	//	printf("\nDoi xung");
	//}
	//else
	//{
	//	printf("\nKhong doi xung");
	//}



	{
		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 kiem tra lai !");
			}
		} while (n < 0 || n > MAX);

		int a[MAX];  // Khai baó
		NhapMang(a, n);
		//NhapMangNgauNhien(a, n);
		XuatMang(a, n);

		printf("\nCac so doi xung la");
		LietKeDoiXung(a, n);

		// In ra các số amstrong trong [1, 100000]
		/*for (int i = 1; i < 1000000; i++)
		{
			if (KiemTraAmstrong(i))
			{
				printf("\n%d", i);
			}
		}*/

		int Tong;
		printf("\nCac so Amstrong la: ");
		LietKeVaTinhTongAmstrong(a, n, Tong);

		printf("\nTong cac so Amstrong la: %d", Tong);
		_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
In function 'NhapMang':
Line 13: error: 'for' loop initial declaration used outside C99 mode
In function 'NhapMangNgauNhien':
Line 22: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 31: error: 'for' loop initial declaration used outside C99 mode
In function 'LietKeDoiXung':
Line 52: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 74: error: expected ';', ',' or ')' before '&' token


Create a new paste based on this one


Comments: