简体   繁体   中英

Compare length of two arrays using C++

I want to compare two integer arrays using C++ if their length is not equal then make those arrays equal to each other by adding items to a smaller-length array. Here is my code snippet.

    #include <iostream>
    
    using namespace std;
    
    int main() {
       
        int arrayOne[] = {1,2,3,4,5,6,7};
        int arrayTwo[] = {8,5,6,4,5,6,4};
        int arrayThree[] = {};
        
        int a = sizeof(arrayOne) / sizeof(int); // 7
        int b = sizeof(arrayTwo) / sizeof(int); // 7
        
 if(a != b){
        if(a > b){ // means b is small 
            int diff = a - b ; // = 2 ... 0,1
            for(int k=0; i < k ; k++ ){ 
                arrayTwo[b+k] = b +k;
            }
        }
        else{
            int diff = b - a ; // = 2 ... 0,1
            for(int k=0; i < k ; k++ ){ 
                arrayOne[b+k] = b +k;
            }
        }
    }
        
        // Here I want to compare these two arrays....
        
        for (int i=0; i < a; i++)
        {
          arrayThree[i] = arrayOne[i] + arrayTwo[i];   // aggregate the sums into the first array
        }
    
        for(int i =0; i < a;i++){
            cout<<arrayThree[i]<<endl;
        }
        
        return 0;
    
    }

The main problem I want to sum the item of both arrays so that need to be of the same length.

you cannot increase or decrease size of the array because it is static. You can compare two array by size only. For increasing or decreasing size and adding the element you can use vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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