比较数组C ++ [英] Comparing arrays C++
本文介绍了比较数组C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < iostream >
使用 命名空间标准
int main(){
int size = 5 跨度>;
int count = 0 ;
int big [size],list [size] = { 5 , 0 , 4 , 2 , 0 跨度>};
int list2 [size] = { 1 , 2 , 3 , 4 , 5 跨度>};
for ( int i = 0 ; i< size; i ++){
if (list [i]> = list2 [i])
{
list [i] = big [i];
count ++;
}
else if (list [i]< = list2 [i ])
list2 [i] = big [i];}
for ( int y = 0 ; y< size; y ++){
cout<<大[y]<< ;
}
return 0 ;
}
这里错了什么......它杀了我
我尝试了什么:
i得到这样的东西
1 0 4255129 0 0
解决方案
Quote:for(int i = 0; i< size; i ++){>
if(list [i]> = list2 [i])
{
list [i] = big [i];
count ++;
}
else if(list [i]< = list2 [i])
list2 [i] = big [i];}
以上代码存在缺陷。它应该是
for ( int i = 0 ; i< size; i ++)
{
if (list [i]> = list2 [i])
{
big [i] = list [i];
count ++;
}
else
{
big [i] = list2 [i];
}
}
或者更简洁
for ( int i = 0 ; i< size; i ++)
big [i] = list [i]> = list2 [i]? count ++,list [i]:list2 [i];
hey guys i want to compare two arrays and get 3rd array containing the bigger element in the two of them
#include <iostream>
using namespace std;
int main(){
int size =5;
int count=0;
int big[size],list[size]={5,0,4,2,0};
int list2[size]={1,2,3,4,5};
for(int i=0;i<size;i++){
if(list[i]>=list2[i])
{
list[i]=big[i];
count++;
}
else if (list[i]<=list2[i])
list2[i]=big[i];}
for (int y=0;y <size;y++){
cout << big[y] << " ";
}
return 0;
}
what wnt wrong over here... it's killing me
What I have tried:
i get somthing like this
1 0 4255129 0 0
解决方案
Quote:for(int i=0;i<size;i++){>
if(list[i]>=list2[i])
{
list[i]=big[i];
count++;
}
else if (list[i]<=list2[i])
list2[i]=big[i];}
The above code is flawed. It should be
for (int i = 0; i < size; i++) { if(list[i] >= list2[i]) { big[i] = list[i]; count++; } else { big[i] = list2[i]; } }
or, more pithily
for (int i = 0; i < size; i++) big[i] = list[i] >= list2[i] ? count++, list[i] : list2[i];
这篇关于比较数组C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文