[ create a new paste ] login | about

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

C, pasted on May 5:
/* Bài 211/68/SBT Thầy NTTMK:Tính trung bình cộng các số nguyên tố trong mảng một chiều các số nguyên . 376.cpp */

/* Ở đây chương trình sẽ liệt kê ra tất cả các số nguyên tố có trong mảng,số lượng của chúng và tính trung bình cộng các số nguyên tố . */
/* 

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 dem=0,f,Co,tong=0;
	bool Co=false;
	printf("\nCac so nguyen to co trong mang la:");
	for(int i=0;i<n;i++)
	{
		f=1;
		if(a[i]<2)
			f=0;
		for(int k=2;k<=a[i]/2;k++)
		{
			if(a[i]%k==0)
				f=0;
		}
		if(f==1)
		{
			printf("%4d",a[i]);
			dem++;
			tong+=a[i];
			Co=true;
		}
	}
	if(Co==true)
	{
		printf("\nSo luong cac so nguyen to co trong mang la:%d",dem);
		float trungbinhcong=tong*1.0/dem; // ở đây vì trungbinhcong đang ở dạng số thực (float) mà tổng đang ở dạng số nguyên (int) nên ta nhân thêm 1.0 vào mới đúng được hoặc là ở dòng khai báo tong ta để dạng số thực luôn.
		printf("\nTrung binh cong cac so nguyen to co trong mang la:%f",trungbinhcong);
	}
	else
		printf("\nTrong mang khong ton tai so nguyen to\n");
}
		
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
13
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 51: error: 'for' loop initial declaration used outside C99 mode
Line 56: error: 'for' loop initial declaration used outside C99 mode
Line 66: error: 'true' undeclared (first use in this function)
In function 'main':
Line 80: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: