Pass a dynamic fix by function in C ++

0

Can you help me with the following exercise?

The idea is to have 3 functions with 3 vectors, vector 1 enters even numbers, vector 2 enters odd numbers and vector 3 multiplies each value of vector 1 with vector 2. In the end, the 3 vectors are displayed. The problem is that for vector 3 I do not have as reference vectors 1 and 2, and therefore does not perform multiplication. This is what I have so far:

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;
using std::setw;

vectorpar (vector<int>numerospares) {
    int size1;

    for (int i = 1; i <= 20; i++) {
        if (i % 2 ==0)
        numerospares.push_back(i);
    }
    size1 = numerospares.size();

    for(int j = 0; j< size1; j++) {
        cout << setw(3) << numerospares[j] << endl;
    }
}

vectorimpar (vector<int> numerosimpares) {
    int size2;

    for (int i = 1; i <= 20; i++) {
        if (i % 2 !=0)
        numerosimpares.push_back(i);
    }
    size2 = numerosimpares.size();

    for(int j = 0; j< size2; j++) {
        cout << setw(3) << numerosimpares[j] << endl;
    }
}

parximpar (vector<int> paresximpares) {
    vector <int>numerospares;
    vector <int>numerosimpares;

    //int size3 = numerospares.size();

    for (int i = 0; i < 20; i++) {
        vectorpar (numerospares)[i] * vectorimpar (numerosimpares)[i];
    }
}

int main() {
  vector <int>numerospares;
  vector <int>numerosimpares;
  vector <int>paresximpares;

  cout<<"El arreglo de pares es:"<<endl;
  vectorpar (numerospares);

  cout<<"El arreglo de impares es:"<<endl;
  vectorimpar (numerosimpares);

  cout<<"El producto de los arreglos es:"<<endl;
  vectorparximpar (paresximpares);

  return 0;
}
    
asked by Daniela R 12.11.2018 в 06:00
source

3 answers

1

Apart from the fact that you lack the return type of the functions (since they do not return anything, they must be void ) you must pass the vectors by reference:

void vectorpar (vector<int> &numerospares);

void vectorimpar (vector<int> &numerosimpares);

void parximpar (vector<int> &paresximpares);

As you are going through copy the vectors, the data stays inside the function, so that the function can modify a vector that is defined outside of it (and in this way the data can exit > of it), you must receive it by reference.

Also, your multiplication function is incorrect; you are multiplying vectors that you define in the function itself (which will be empty), not the vectors that you filled out. I propose that the function parximpar return a vector and receive the vectors that you want to multiply as a constant reference:

vector<int> parximpar (const vector<int> &numerospares, const vector<int> &numerosimpares){

    vector<int> resultado(20);

    for (int i = 0; i < 20; i++) {
        resultado = numerospares[i] * numerosimpares[i];
    }

    return resultado;
}
    
answered by 12.11.2018 в 07:56
0

Why do not you pass the vectors by parameter as you do at the beginning something like this, vectorparximpar (numerospares, numerosimpares, paresximpares); for the call of the function in the main, and in the function you must modify the input of parameters like this, parximpar (vector paresximpares, vector numerospares, vector numerosimpares) and eliminate the creation of the two vectors within the parximpar function since they are defining in the parameters of the function. Any other questions with pleasure, greetings.

    
answered by 12.11.2018 в 06:54
0
  

vector 1 enters num pairs

Since the function is the one that generates the information, it is that it returns it via return :

vector<int> vectorpar()
{
    vector<int> numerospares;

    for (int i = 1; i <= 20; i++){
        if (i % 2 ==0)
            numerospares.push_back(i);
    }

    size_t size1 = numerospares.size();

    for(size_t j = 0; j< size1; j++){
     cout << setw(3)<<numerospares[j]<<endl;
    }

    return numerospares;
}

And something equivalent for odd numbers.

  

vector 3 multiplies each item of vector 1 with vector 2

The paresximpares function, as I understand it, should receive the two previous vectors and generate a new vector with the results:

vector<int> parximpar (vector<int> const& pares, vector<int> const& impares)
{
  vector<int> resultado;

  for (size_t i = 0; i < pares.size(); i++)
   resultado.push_back(pares[i] * impares[i]);

  return resultado;
}

Finally I would extract the code that prints the vectors to an independent function ... I personally hate repeating code. The idea is that the vectors are printed from the main not from the generating functions:

vector<int> vectorpar()
{
    vector<int> numerospares;

    for (int i = 1; i <= 20; i++){
        if (i % 2 ==0)
            numerospares.push_back(i);
    }

    return numerospares;
}

void ImprimeVector(vector<int> const& vector)
{
  for( int n : vector )
    cout << setw(3) << n << '\n';
}

And with all this the main is now more coherent because it clearly understands what it does and there are no surprises within any function.

int main() {
  vector<int> numerospares = vectorpar();
  vector<int> numerosimpares = vectorimpar();

  cout<<"El arreglo de pares es:"<<endl;
  ImprimeVector(numerospares);

  cout<<"El arreglo de impares es:"<<endl;
  ImprimeVector(numerosimpares);

  vector<int> resultado = parximpar(pares,impares);

  cout<<"El producto de los arreglos es:"<<endl;
  ImprimeVector(resultado);

  return 0;
}
    
answered by 12.11.2018 в 10:45