简体   繁体   English

C ++比较2个数组中的整数?

[英]C++ comparing integers in 2 arrays?

So my task is to make 3 arrays. 所以我的任务是制作3个数组。 User has to put integers in 2 of the arrays, then I must compare the integers in each position and put the higher integer in a third array, that has to be displayed on the screen. 用户必须将整数放入2个数组中,然后我必须比较每个位置的整数并将较高的整数放入必须在屏幕上显示的第三个数组中。

Here is my program: 这是我的程序:

int main() {    
    int n; // number of elements
    cin >> n;

    int* array  = new int[n];
    int* array2 = new int[n];
    int* array3 = new int[n];

    cout << "First array: " << "\n";
    for (int i = 0; i < n; i++) {
        cin >> array[i];
    }

    cout << "Second array:" << "\n";
    for (int i = 0; i < n; i++) {
        cin >> array2[i];
    }

    for (int i = 0; i < n; i++) {
        if (array[i] > array2[i]) {
            array[i] = array3[i];
            continue;
        } else if (array[i] <= array2[i]) {
            array2[i] = array3[i];
            continue;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << array3[i] << "\n ";
    }
    delete[] array;
    delete[] array2;
    delete[] array3;
}

So, if my first array is 1 2 3 4 and second array is 5 6 7 8 - it displays some massive negative numbers as output ( -842150421 ). 因此,如果我的第一个数组是1 2 3 4 ,第二个数组是5 6 7 8它显示一些大量的负数作为输出( -842150421 )。

Where is my mistake? 我的错误在哪里?

You are showing your array3 but you haven't added any data to it, you must assign data to your array3 I suppuose that after the comparation you do between array and array2 you get the bigger data into the array3. 您正在显示array3但尚未向其中添加任何数据,您必须将数据分配给array3。我认为在对array和array2进行比较之后,您会获得更大的数据到array3中。

int main(){

int n; //number of elements
cin >> n;
int* array = new int[n];
int* array2 = new int[n];
int* array3 = new int[n];
cout << "First array: " << "\n";
for (int i = 0; i < n; i++)
    cin >> array[i];
cout << "Second array:" << "\n";
for (int i = 0; i < n; i++)
    cin >> array2[i];

for (int i = 0; i < n; i++) {

    if (array[i] > array2[i]) {
        array3[i] = array[i];
        continue;
    }
    else if (array[i] <= array2[i]) {
        array3[i] = array2[i];
        continue;
    }
}
for (int i = 0; i < n; i++)
    cout << array3[i] << "\n ";
delete[] array;
delete[] array2;
delete[] array3;
}

I haven't modified your code but: 我尚未修改您的代码,但:

  • I think you could remove that continue; 我认为您可以删除该continue; as you have an else clause the code will not enter in the other condition. 由于您具有else子句,因此代码不会在其他条件下输入。
  • I would remove the second comparation because if it's not bigger there is the case that is equal or less so you could leave just the else without any if. 我将删除第二个比较,因为如果不比较大,则等于或小于等于,因此您可以不加任何其他条件而保留其他条件。

    if (array[i] > array2[i]) { array3[i] = array[i]; 如果(array [i]> array2 [i]){array3 [i] = array [i]; } else{ array3[i] = array2[i]; } else {array3 [i] = array2 [i]; } }

int maxima(int i , int j)
{

      if(j > i)
           return j;
   return i;
}
int main()
{
   int n; //number of elements
cin >> n;
int* array = new int[n];
int* array2 = new int[n];
int* array3 = new int[n];
cout << "First array: " << "\n";
for (int i = 0; i < n; i++)
    cin >> array[i];
cout << "Second array:" << "\n";
for (int i = 0; i < n; i++)
    cin >> array2[i];
   for(int i = 0; i < n; i++)
          array3[i] = maxima(array[i], array2[i]);
delete[] array;
delete[] array2;
delete[] array3;
    return 0;
}

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM