统计一个数组中正数和负数的个数

 2023-09-05 阅读 37 评论 0

摘要:1.遍历数组,将正数重新放在一个数组中,负数放在另一个数组中 1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int a[5],i,j=0,k=0; 6 int s1[5],s2[5]; 7 cout<<"Please enter 5 numbers"<<endl; 8 for(i

1.遍历数组,将正数重新放在一个数组中,负数放在另一个数组中

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int a[5],i,j=0,k=0;
 6     int s1[5],s2[5];
 7     cout<<"Please enter 5 numbers"<<endl;
 8     for(i=0;i<5;i++)
 9         cin>>a[i];
10     for(i=0;i<5;i++)
11     { 
12         if(a[i]>0)
13         s1[j++]=a[i];     
14         else if(a[i]<0)
15          s2[k++]=a[i];
16     }
17     cout<<"The positive numbers are:"<<endl;
18     for(i=0;i<j;i++)
19     cout<<s1[i]<<" ";
20     cout<<endl;
21     cout<<"The negative numbers are:"<<endl;
22     for(i=0;i<k;i++)   
23     cout<<s2[i]<<" ";
24     cout<<endl;
25     system("pause");
26     return 0;
27 }

2.显示结果

转载于:https://www.cnblogs.com/dongyanxia1000/p/4905411.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/1344.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息