#include<stdio.h>
#include<conio.h>
#define max 100
#include<iostream>
using namespace std;
#include<stdlib.h>
// day la co che mang tinh
/*int nhapmang(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"nhap vao phan tu ["<<i<<"]=";cin>>a[i];
}
}
int xuatmang(int a[100],int n)
{
for(int i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
}
int tinhtong(int a[],int n)
{
int tong=0;
for(int i=0;i<n;i++)
{
tong+=a[i];
}
return tong;
}
int main()
{
int a[100],n;
do
{
cout<<"nhap n";cin>>n;
if(n<0||n>100)
{
cout<<"ban da nhap sai xin kiem tra lai !\n";
}
}while(n<0||n>100);
nhapmang(a,n);
cout<<endl;
xuatmang(a,n);
cout<<endl;
cout<<tinhtong(a,n);
getch();
return 0;
}*/
// theo co che mang dong
int nhapmang(int *a,int n)
{
for(int i=0;i<n;i++)
{
cout<<"nhap vao phan tu ["<<i<<"]=";cin>>a[i];
}
}
int xuatmang(int *a,int n)
{
for(int i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
}
int tinhtong(int *a,int n)
{
int tong=0;
for(int i=0;i<n;i++)
{
tong+=a[i];
}
return tong;
}
int main()
{
int n;
do
{
cout<<"nhap n";cin>>n;
if(n<0||n>100)
{
cout<<"ban da nhap sai xin kiem tra lai !\n";
}
}while(n<0||n>100);
// theo co che malloc
//int *a=(int *)malloc(n*sizeof(int *));
// theo co che calloc
//int *a=(int *)calloc(n,sizeof(int *));
// theo co che realloc
//int *a=(int *)realloc(0,sizeof(int *));
// theo co che new
//int *a=new int ;//cap phat 1 con tro
int *a= new int[n];//cap phat toan bo con tro
//nhapmang(a,n);
cout<<endl;
xuatmang(a,n);
cout<<endl;
cout<<tinhtong(a,n);
// free(a);
delete a;// giai phong o dau tien trong mang troi
delete[] a;// giai phong nguyen cai mang
getch();
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.