有一个十位数的数组,统计出正数、负数和零的个数

 

#include<iostream>
using namespace std;
int main()
{
int a=0,b=0,c=0,d[10],i;
for(i=0;i<=9;i++)
{
cin>>d[i];
if(d[i]>0)a++;
if(d[i]<0)b++;
else c++;
}
cout<<a<<endl;//正数的个数
cout<<b<<endl;//负数的个数
cout<<c<<endl;//零的个数
return 0;
}

 

int main()
{
    int a[10] = {10,20,0,30,-10,-20,-34,14,9,21};
    int zs = 0;
    int fs = 0;
    int ls = 0;
    for(int i=0; i<10; i++)
    {
        if(a[i] > 0)
            zs++;
        else if(a[i] < 0)
            fs++;
        else
            ls++;
    }
    cout<<"正数有"<<zs<<"个"<<endl;
    cout<<"负数有"<<fs<<"个"<<endl;
    cout<<"零有"<<ls<<"个"<<endl;
}

 

#include <iostream>
using namespace std;
int max(const int &a,const int &b);
int main()
{
    cout<<max(3,4)<<endl;
    return 0;
}
int max(const int &a, const int &b)
{
    return a>b?a:b;
}

第一题

#include <stdio.h>
#include<iostream>
using namespace std;

int main()
{
	int zheng = 0;
	int fu = 0;
	int ling = 0;
	int a[10] = {1,2,-9,0,5,-4,-7,6,0,5};
	for (int i = 0;i<sizeof(a) / sizeof(a[0]);i++){
		if(a[i]<0){
			fu++;
		}else if(a[i]>0){
			zheng++;
		}else{
			ling++;
		}
	}
	cout<<zheng<<endl;
	cout<<fu<<endl;
	cout<<ling<<endl;
	return 0;
}

 

(1)和(2)分别写了两个函数,代码如下,如有帮助,请采纳一下,谢谢。

#include <stdio.h>
//比较a和b的大小,返回大值
double Max(double a,double b)
{
	return a>b?a:b;
}
//统计数组a中正数、负数和零的个数
void NmbCount(double a[],int n, int* zs,int* fs,int*zero)
{
	int i = 0;
	*zs = 0;
	*fs = 0;
	*zero = 0;
	for (; i < n; i++)
	{
		if(a[i] > 0)
			(*zs)++;
		else if(a[i] == 0)
			(*zero)++;
		else
			(*fs)++;
	}
}

int main()
{
	double a,b;
	int zs,fs,zero,i;
	double arr[10] = {1,2,-1,-2,-3,0,0,3,2,0};

	printf("请输入两个数:");
	scanf("%lf %lf",&a,&b);
	printf("较大的数是:%g\n",Max(a,b));

	printf("题目(2)数组为:");
	for (i = 0; i < 10; i++)
	{
		printf("%g ",arr[i]);
	}

	NmbCount(arr,10,&zs,&fs,&zero);
	printf("\n整数:%d个,负数%d个,零%d个\n",zs,fs,zero);
	return 0;
}

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632

用for循环遍历就可以。

#include"stdio.h"
int main()
{
int a[10];
int countp=0, countn=0, count0=0;
for(int i=0,i<10,i++)
{
scanf("%d", &a[i]);
if(a[i]>0)
{countp++;}
else if(a[i]<0)
{countn++;}
else
{count0++;}
}
printf("正数:%d 负数:%d 0:%d",countp,countn,count0);
return 0;
}