[ create a new paste ] login | about

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

C, pasted on May 6:
/* Bài 266/75/SBT Thầy NTTMK:Hãy thêm một phần tử có giá trị x vào mảng tại vị trí k . 273.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 nhapmang(int a[MAX],int &n,int &k,int &x)
{
	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]);
	}
	do{
		printf("\nNhap vao vi tri can them:k=");
		scanf("%d",&k);
		if(k<0||k>n-1)
			printf("\nVi tri can them khong hop le!Xin vui long nhap lai!\n");
		else
			break;
	}while(k<0||k>n-1);
	printf("Nhap vao gia tri x can them:x=");
	scanf("%d",&x);
}


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

void themgiatri(int a[MAX], int &n, int x, int k)
{
	for (int i=n; i>k; i--)
	{
		a[i] = a[i-1];
	}
	a[k] = x;
	n++;
} 

void main()
{
	int a[MAX],n,x,k,tieptuc;
	quaylai:nhapmang(a,n,k,x);
	printf("\n>>>>>>>>>>>>MANG VUA NHAP LA:<<<<<<<<<<<<<\n");
	xuatmang(a,n);
	printf("\n>>>>>>>>>>>>>MANG VUA THEM LA:<<<<<<<<<<<<<<<<<\n");
	themgiatri(a,n,x,k);
	xuatmang(a,n);
	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
Line 17: error: conio.h: No such file or directory
Line 16: error: expected ';', ',' or ')' before '&' token
In function 'xuatmang':
Line 46: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 53: error: expected ';', ',' or ')' before '&' token
In function 'main':
Line 64: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: