[ create a new paste ] login | about

Link: http://codepad.org/PfbyI5r2    [ raw code | output | fork | 3 comments ]

C, pasted on Sep 15:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100

void nhap (int a[], int &n)
{
	do
	{
		printf("\nNhap so phan tu: ");
		scanf("%d", &n);
		if(n <= 0 || n > MAX)
		{
			printf("\nSo phan tu khong hop le. Xin kiem tra lai !");
		}
	}while(n <= 0 || n > MAX);
	for(int i = 0; i < n; i++)
	{
		printf("\nNhap a[%d]: ", i);
		scanf("%d", &a[i]);
	}
}

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

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

	nhap(a, n);
	xuat(a, n);
	printf("\nCac gia tri chan trong mang la: ");
	lietkechan(a, n);

	getch();
	return 0;
}


Output:
1
2
3
4
5
6
Line 17: error: conio.h: No such file or directory
Line 6: error: expected ';', ',' or ')' before '&' token
In function 'xuat':
Line 26: error: 'for' loop initial declaration used outside C99 mode
In function 'lietkechan':
Line 34: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments:
posted by NhanCoder on Aug 6

reply
posted by luongphongnhan on Oct 24
#include <iostream>
#include "math.h"

#define MAX 100

using namespace std;

void nhapMang(int arr[], int &n){
do
{
cout << "Press num of interger: ";
cin >> n;
if (0 >= n || n > 100) {
system("cls");
cout << "Value of n is invalue!! Pls repress!!" << endl;
}
} while (n<=0 || n >100);
for (int i = 0; i < n; i++)
{
cout << "Press element for array: ";
cin >> arr[i];
}
}

void xuatMang(int arr[], int n) {
cout << "Arr imported is: " << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

void timChan(int arr[], int n) {
for (int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
cout << arr[i] << " ";
}
cout << endl;

}

int main() {
int numOfArr;

int Arr[MAX];
nhapMang(Arr, numOfArr);
xuatMang(Arr, numOfArr);
cout << "Nhung phan tu trong mang la chan: " << endl;
timChan(Arr, numOfArr);

system("pause");
return 0;
}
reply
posted by nhathuy8900 on Feb 20
mình lười nhập nên tạo ngẫu nhiên số liệu luôn, mọi người tham khảo
//liet ke cac gia tri chan trong mang 1 chieu.cpp
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void taomang(int a[], int &n);
void xuatmang(int a[], int n);
void lietke(int a[], int n);
int main()
{
int a[100];
int n;
taomang(a,n);
cout<<"------------------------------"<<endl;
xuatmang(a,n);
cout<<"------------------------------"<<endl;
cout<<"cac so chan trong mang la:"<<endl;
lietke(a,n);
}
void taomang(int a[], int &n)
{
srand(time(NULL));
n = rand()%(100-30+1)+30;
cout<<"so luong phan tu cua mang la: "<<n<<endl;
for(int i=1;i<=n;i++)
{
a[i]=rand()%501;
}

}

void xuatmang(int a[], int n)
{
for(int i=1;i<=n;i++)
if(i%8==0)
cout<<a[i]<<endl;
else
cout<<a[i]<<" ";
cout<<endl;

}

void lietke(int a[], int n)
{
int dem=1;
for(int i=1;i<=n;i++)
if(a[i]%2==0)
{
if(dem%8==0)
cout<<a[i]<<endl;
else
cout<<a[i]<<" ";
dem++;
}
cout<<endl;
cout<<"------------------------------"<<endl;
cout<<"tong so chan co trong mang la: "<<dem-1<<endl;

}

reply