[ create a new paste ] login | about

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

C, pasted on May 5:
/* Bài 170/63/SBT Thầy NTTMK:Cho mảng một chiều các số nguyên.Hãy viết hàm tìm số nguyên tố nhỏ nhất lớn hơn mọi giá trị có trong mảng. 143.cpp */

/* Ý TƯỞNG: Ta tìm ra số lớn nhất trong mảng,tuần tự cho số lớn nhất + 1 rồi xét nguyên tố,nếu xét đúng là nguyên tố thì break ra khỏi vòng lặp => số đầu tiên thỏa => chính là số nguyên tố nhỏ nhất nhưng bảo đảm lớn hơn tất cả mọi giá trị trong mảng (vì nó đã lớn hơn giá trị lớn nhất trong mảng). */
/* 

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>
#include<limits.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 max=INT_MIN,Co,f,i,Co1;
	bool Co=false;
	bool Co1=false;
	for(i=0;i<n;i++)
	{
		if(a[i]>max)
		{
			max=a[i];
			Co=true;
		}
	}
	if(Co==true)
	{
		for(i=max+1;i<30000;i++) // => 30000 là xấp xĩ giá trị lớn nhất của kiểu int
		{
			f=1;
			if(i<2)
				f=0;
			for(int k=2;k<=i/2;k++)
			{
				if(i%k==0)
					f=0;
			}
			if(f==1)
			{
				Co1=true;
				break;
			}
		}
		if(Co1==true)
			printf("\nSo nguyen to nho nhat lon hon moi gia tri co trong mang la:%d",i);
	}
}

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 20: error: expected ';', ',' or ')' before '&' token
In function 'xuatmang':
Line 40: error: 'for' loop initial declaration used outside C99 mode
In function 'xulydulieu':
Line 50: error: 'false' undeclared (first use in this function)
Line 50: error: (Each undeclared identifier is reported only once
Line 50: error: for each function it appears in.)
Line 57: error: 'true' undeclared (first use in this function)
Line 67: error: 'for' loop initial declaration used outside C99 mode
In function 'main':
Line 84: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: