[ create a new paste ] login | about

Link: http://codepad.org/FJ0bSxDM    [ 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 (float 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("%f", &a[i]);
	}
}

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

float tongam(float a[], int n)
{
	float s = 0;
	for(int i = 0; i < n; i++)
	{
		if(a[i] < 0)
		{
			s = s + a[i];
		}
	}
	return s;
}
int main()
{
	int n;
	float a[MAX];

	nhap(a, n);
	xuat(a, n);

	float tong = tongam(a, n);
	printf("\nTong cac so am trong mang la %.3f", tong);
	
	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 'tongam':
Line 35: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments:
posted by Dgls on Feb 24
#include<iostream>
using namespace std;
void Nhap(int n, float arr[]){
cout<<"Nhap gia tri cua cac phan tu trong mang: "<<endl;
for (int i=0; i<n; i++){
cout<<"a["<<i<<"]=";
cin>>arr[i];
}
}
float Tong_am(int n, float arr[]){
float tong=0;
for (int i=0; i<n; i++){
if (arr[i]<0) tong+=arr[i];
}
return tong;
}
main(){
int n;
cout<<"Nhap so phan tu cua mang: "; cin>>n;
float a[n];
Nhap(n,a);
cout<<"Tong cac gia tri am: "<<Tong_am(n,a)<<endl;
}
reply
posted by cuong.luucb8921 on Sep 7
#include<stdio.h>
int i;

void nhap(int a[], int n)
{
for(i=0;i<n;i++)
{
printf("Nhap vao phan tu a[%d] : ",i);
scanf("%d", &a[i]);
}
}

int tong(int a[], int n)
{
int sum=0;
for(i=0;i<n;i++)
{
if(a[i]<0)
{
sum = sum + a[i];
}
}
return sum;
}
int main(){
int a[1000];
int n;
printf("Nhap vao n : ");
scanf("%d", &n);
nhap(a,n);
printf("Tong cac gia tri am la : %d", tong(a,n));
return 0;
}

reply
posted by luongphongnhan on Oct 25
#include <iostream>
using namespace std;
#define MAX 100
void nhapmang(float *arr, int &n) {
do
{
cout << "Moi nhap so phan tu cua mang: ";
cin >> n;
if (0 >= n > 100) {
cout << "Xin moi nhap lai, so phan tu cua mang khong hop le!";
}
} while (0 >= n > 100);
for (int i = 0; i < n; i++)
{
cout << "Moi nhap phan tu thu " << i << "cua mang: ";
cin >> arr[i];
}
}
void xuatmang(float *arr, int n) {
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

float tinhTong(float *arr, int n) {
int tong = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] <0)
{
tong += arr[i];
}
}
return tong;
}

int main() {
int num;
float *Arr= new float[MAX];
nhapmang(Arr, num);
xuatmang(Arr, num);
cout << "Tong cac phan tu nho hon 0 trong mang la: " << tinhTong(Arr, num) << endl;
system("pause");
return 0;
}
reply