[ create a new paste ] login | about

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

C, pasted on May 6:
/* Bài 412/99/SBT Thầy NTTMK:Viết hàm xuất các số nguyên tố trong ma trận các số nguyên ra màn hình theo thứ tự tăng dần . 421.cpp */
/* 

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

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

void xuatmatran(int a[MAX][MAX],int n,int m)
{
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			printf("%4d",a[i][j]);
		}
		printf("\n");
	}
}

void hoanvi(int &a,int &b)	// đây là 1 hàm hoán vị giúp tiết kiệm thời gian chạy chương trình hơn những hàm hoán vị trước giờ hay sử dụng.
{
	a=a^b;
	b=a^b;
	a=a^b;
}

void xulydulieu(int a[MAX][MAX],int n,int m)
{
	int b[MAX],i,j,f,dem=0;
	printf("\nCac so nguyen to co trong ma tran la:");
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			f=1;
			if(a[i][j]<2)
				f=0;
			for(int k=2;k<=a[i][j]/2;k++)
			{
				if(a[i][j]%k==0)
					f=0;
			}
			if(f==1)
			{
				printf("%4d",a[i][j]);
				b[dem]=a[i][j];
				dem++;
			}
		}
	}
	printf("\nSo luong cac so nguyen to co trong ma tran la:%d",dem);
	printf("\nCac so nguyen to sau khi sap xep tang dan la:");
	for(i=0;i<dem-1;i++)
	{
		for(j=i+1;j<dem;j++)
		{
			if(b[i]>b[j])
			{
				hoanvi(b[i],b[j]);
			}
		}
	}	
	for(int i=0;i<dem;i++)
	printf("%d ",b[i]);
}

void main()
{
	int a[MAX][MAX],n,m,tieptuc;
	quaylai:nhapmatran(a,n,m);
	printf("\n>>>>>>>>>>>>>>>MA TRAN VUA NHAP LA:<<<<<<<<<<<<<<<<<<<<<\n");
	xuatmatran(a,n,m);
	xulydulieu(a,n,m);
	printf("\nBan co muon tiep tuc chay chuong trinh khong ? Neu co bam phim C,nguoc lai bam bat ky 1 phim nao khac de ket thuc\n");
	tieptuc=getch();
	if(tieptuc=='c'||tieptuc=='C')
	goto quaylai;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Line 17: error: conio.h: No such file or directory
Line 16: error: expected ';', ',' or ')' before '&' token
In function 'xuatmatran':
Line 46: error: 'for' loop initial declaration used outside C99 mode
Line 48: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 56: error: expected ';', ',' or ')' before '&' token
In function 'xulydulieu':
Line 74: error: 'for' loop initial declaration used outside C99 mode
Line 99: error: redeclaration of 'i' with no linkage
Line 65: error: previous declaration of 'i' was here
Line 99: error: 'for' loop initial declaration used outside C99 mode
In function 'main':
Line 104: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: