[ create a new paste ] login | about

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

C, pasted on May 5:
/* Bài 202/67/SBT Thầy NTTMK:Tính tổng các giá trị có chữ số đầu tiên là chữ số lẻ trong mảng một chiều các số nguyên . 367.cpp */

/* Ở đây chương trình liệt kê ra tất cả các giá trị trong mảng có chữ số đầu tiên là chữ số lẻ,đếm số lượng và tính tổng các giá trị đó */
/* 

Tác giả: Nguyễn Việt Nam Sơn
Trung tâm đào tạo tin học - Thiết kế phần mềm - Sơn Đẹp Trai: www.SonDepTrai.com

Nguồn source code này Tôi viết vào năm 2012 lúc mới bắt đầu học lập trình C/C++ nên một số cách sẽ không được tối ưu - Bạn chỉ nên dùng trên tinh thần tham khảo thôi nhé.
Mong giúp đỡ được Bạn trên con đường Học Lập Trình.
TẤT CẢ VÌ SỰ THÀNH CÔNG CỦA BẠN

*/
#include<stdio.h>
#include<conio.h>
#define MAX 100
#define bool

void nhapmang (int a[MAX],int &n)
{
	do{
	printf("Nhap vao so phan tu cua mang:n=");
	scanf("%d",&n);
	if(n<1||n>MAX)
	printf("So ban nhap vao khong hop le!Xin vui long nhap lai!\n");
	else
	break;
	}while(n<1||n>MAX);
	for(int i=0;i<n;i++)
	{
		printf("Nhap vao a[%d]=",i);
		scanf("%d",&a[i]);
	}
}

void xuatmang(int a[MAX],int n)
{
	printf("\n>>>>>>>>>>>>>MANG VUA NHAP LA:<<<<<<<<<<<<<<<\n");
	for(int i=0;i<n;i++)
	{
		printf("%4d",a[i]);
	}
	printf("\n");
}

void xulydulieu(int a[MAX],int n)
{
	int Co;
	bool Co=false;
	int dem,demb=0,demc=0,demd=0,deme=0,demf=0;
	int tong,tongb=0,tongc=0,tongd=0,tonge=0,tongf=0;
	printf("Cac gia tri trong mang co chu so dau tien la chu so le la:");
	for(int i=0;i<n;i++)
	{
		if(a[i]>=-9&&a[i]<=9)
		{
			int b=a[i]%10;
			if(b%2!=0)
			{
				printf("%4d",a[i]);
				demb++;
				tongb+=a[i];
				Co=true;
			}
		}
		if(a[i]>=-99&&a[i]<=-10||a[i]>=10&&a[i]<=99)
		{

			int c=a[i]/10;
			if(c%2!=0)
			{
				printf("%4d",a[i]);
				demc++;
				tongc+=a[i];
				Co=true;
			}
		}
		if(a[i]>=-999&&a[i]<=-100||a[i]>=100&&a[i]<=999)
		{
			int d=a[i]/100;
			if(d%2!=0)
			{
				printf("%4d",a[i]);
				demd++;
				tongd+=a[i];
				Co=true;
			}
		}
		if(a[i]>=-9999&&a[i]<=-1000||a[i]>=1000&&a[i]<=9999)
		{
			int e=a[i]/1000;
			if(e%2!=0)
			{
				printf("%4d",a[i]);
				deme++;
				tonge+=a[i];
				Co=true;
			}
		}
		if(a[i]>=-99999&&a[i]<=-10000||a[i]>=10000&&a[i]<=99999)
		{
			int f=a[i]/10000;
			if(f%2!=0)
			{
				printf("%4d",a[i]);
				demf++;
				tongf+=a[i];
				Co=true;
			}
		}
	}
	dem=demb+demc+demd+deme+demf;
	tong=tongb+tongc+tongd+tonge+tongf;
	if(Co==true)
	{
		printf("\nTrong mang co ton tai %d gia tri thoa yeu cau de bai",dem);
		printf("\nTong cac gia tri co chu so dau tien la chu so le la:%d",tong);
	}
	else
	printf("\nKhong co gia tri nao trong mang thoa man yeu cau de bai");
}
		
void main()
{
	int a[MAX],n,NAMSON;
	quaylai:nhapmang(a,n);
	xuatmang(a,n);
	xulydulieu(a,n);
	printf("\nBan co muon tiep tuc chay chuong trinh khong ? Neu co bam phim C,nguoc lai bam bat ky phim nao khac de ket thuc\n");
	NAMSON=getch();
	if(NAMSON=='c'||NAMSON=='C')
	goto quaylai;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
Line 17: error: conio.h: No such file or directory
Line 19: error: expected ';', ',' or ')' before '&' token
In function 'xuatmang':
Line 39: error: 'for' loop initial declaration used outside C99 mode
In function 'xulydulieu':
Line 49: error: 'false' undeclared (first use in this function)
Line 49: error: (Each undeclared identifier is reported only once
Line 49: error: for each function it appears in.)
Line 53: error: 'for' loop initial declaration used outside C99 mode
Line 63: error: 'true' undeclared (first use in this function)
In function 'main':
Line 124: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: