How to add elements from two array lists with if C++

Asked

Viewed 114 times

-1

Well, I’ve been stuck on this show for a long time. I am inciante and the purpose of the question is to make operations between each element of two lists if the rest is 1, 2 and so on. Example: 2 list with elements, 4 9 7 and 1 2 3, you will add 4+1 if the division 4/4 is equal to zero.

#include <iostream>
using namespace std;

int main() {
  int n, i, j, s =0, resto = 0;
  

  cin >> n;

  int vet[n], resultado[n];
  
  for (i=0; i<n; i++){

    cin >> vet[i];
  }

  for (j=0; j<n; j++){

    cin >> vet[j];
  }

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

    resto = vet[i] % 4;
    for (j=0; j<n; j++){ 
        if (resto == 0){
          resultado[i] = vet[i]+vet[j];
        }
        if (resto == 1){
          resultado[i] = vet[i]-vet[j];
        }
        if (resto == 2){
          resultado[i] = vet[i]*vet[j];
        }
        if (resto == 3){
          resultado[i] = vet[i]/vet[j];
        }
        if (resto == 4){
          resultado[i] = vet[i]^vet[j];
        }
    }
    cout << resultado[i] << ' ';
     
  }
}
  • When the division 4/4 is equal to zero? in the code if (resto == 0){ why add vet[i] with vet[j] since the only way to resto be equal to zero is if vet[i] for zero?

1 answer

0

Hi, I advise you to always learn and use the modern C++ sub-languages:

Example: corlu

use Std::copy_n to read the entries

copy_n(istream_iterator<int>(cin), n, std::back_inserter(v1)); // le 'n' números em v1

use Std::Transform to scroll through and change the array

transform( v1.begin(), v1.end(), v2.begin(), ... );

use Std::vector to 'auto' whenever possible

 vector<int> v1,v2;

use both to express functions

 auto x = [](int a, int b) { return a + b; }

And please post the important part of the code here ( as I did above ) and the full code in the explorer or corilu Compiler.

Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.