[ create a new paste ] login | about

Link: http://codepad.org/pel7tHwB    [ raw code | output | fork | 2 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]);
	}
}

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

	nhap(a, n);
	xuat(a, n);
	printf("\nCac vi tri chua gia tri am la: ");
	lietkecacvitriam(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 'lietkecacvitriam':
Line 34: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


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

#define MAX 100

using namespace std;

void nhapMang(float 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(float arr[], int n) {
cout << "Arr imported is: " << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

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

}

int main() {
int numOfArr;

float Arr[MAX];
nhapMang(Arr, numOfArr);
xuatMang(Arr, numOfArr);
cout << "Nhung phan tu trong mang be hon 0: " << endl;
timSoAm(Arr, numOfArr);

system("pause");
return 0;
}
reply
posted by lehoangan01 on Feb 28
// Bài 133: Viết hàm liệt kê các vị trí mà giá trị tại đó là giá trị âm trong mảng 1 chiều các số thực

#include <iostream>
#include <math.h>
#define Max 100
using namespace std;

void get_the_array(int a[], int n){
do{
if (n <= 0 || n > Max){
cout << " invalid valued return";
}
}while (n <= 0 || n > Max);
for(int i = 0; i <= n; i++){
printf("input the value of a[%d]: ",i);
scanf("%i", &a[i]);
}
}

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

int main(){
int n;
printf("\nNhap so phan tu: ");
scanf("%d", &n);
int a[n];
get_the_array(a, n);
find_negative_number(a, n);

return 0;
}

reply