danicdb
danicdb

Reputation: 1

Issue creating a vector

I´m a beginner in C++ (using C:B and eclipse) and I'm trying to work out the basics of vectors but I'm struggling to move ahead and would like to ask for some help.

Basically, I have to create 2 vectors (vet1 type int and vet2 type long) and join them in sequence to create a third vector type short (vet3). Afterwards, create a matrix 4x4 (mat1) from vet3 element by element.

My issue comes creating vet3, the program stops there. It may be something really easy but I couldn't figure it out.

If I forgot something or need some more info, please tell me. PS: Sorry about my english, by the way :)

using namespace std;

int main() {

vector<int> vet1; //Cria vector, tipo int, com nome vet1
vector<long> vet2; //Cria vector, tipo long, com nome vet2
vector<short> vet3; //Cria vector, tipo short, com nome vet3
vector<int> mat1[4][4]; //Cria matriz 4x4, tipo  int, com nome mat1
int data, i, j, n = 0;

for (i = 0; i < 8; i++) //Laço para preencher o vet1 de dados
{
    cout << "\nDigite um numero para a posicao " << vet1.size() << " do vetor 1: " << endl;
    cin >> data;
    vet1.push_back(data); //função que coloca o valor digitado no final do vetor vet1
}
for (i = 0; i < 8; i++)
{
    cout << "\nDigite um numero para a posicao " << vet2.size() << " do vetor 2: " << endl;
    cin >> data;
    vet2.push_back(data); //função que coloca o valor digitado no final do vetor vet21
}
for (i = 0; i < vet1.size(); i++) //Laço para imprimir vet1
{
    cout << "\t[" << vet1[i] << "]";
}
for (i = 0; i < vet2.size(); i++) //Laço para imprimir vet2
{
    cout << "\t[" << vet2[i] << "]";
}
for (i = 0; i < 16; i++) //Laço que intercala vet1 e vet2 sequencialmente para criar vet3 com 16 posições
{
    vet3[i+n] = vet1[i];
    vet3[i+n+1] = vet2[i];
    n++;
}
for (i = 0; i < vet3.size(); i++)
{
    cout << "\t[" << vet3[i] << "]";
}

n = 0;
for (i = 0; i < 4 ; i++) //Laço para inserir todos os elementos do vet3 numa matriz quadrada 4x4
{
    for (j = 0; j < 4; j++)
    {
        mat1[i][j] = vet3[n];
        n++;
    }
}
return 0;
}

Upvotes: 0

Views: 101

Answers (4)

JHBonarius
JHBonarius

Reputation: 11291

I will just write my comments in my example code:

#include <vector>
#include <iostream>
#include <algorithm>
#include <array>

// never use using namespace std!
// makes the result unecesary large
using std::vector;
using std::cout;
using std::cin;
using std::array;

int main()
{
    int userInput;
    vector<int> vet1;
    vet1.reserve(8); // initialize the vector size
    for (int i = 0; i < 8; i++)
    {
        cout << "Digite um numero para a posicao " << i << " do vetor 1: \n"; // dont use endl...
        cin >> userInput;
        vet1.push_back(userInput);
    }

    vector<long> vet2;
    vet2.reserve(8);
    for (int i = 0; i < 8; i++)
    {
        cout << "Digite um numero para a posicao " << i << " do vetor 2: \n";
        cin >> userInput;
        vet2.push_back(userInput);
    }

    // generate a lambda function that prints a vector element
    auto printElement = [](auto element)
    {
        cout << "\t[" << element << "]";
    };

    //Laço para imprimir vet1
    std::for_each(vet1.cbegin(), vet1.cend(), printElement);

    //Laço para imprimir vet2
    std::for_each(vet2.cbegin(), vet2.cend(), printElement);

    vector<short> vet3(16); //initialize size
    // now fill vector 3 with the elements from vector 1 interleaved with vector 2
    for (int i = 0, i2 = 0; i<8; i++, i2 += 2)
    {
        vet3[i2] = vet1[i];
        vet3[i2 + 1] = vet2[i];
    }


    // mat1 is actually not a matrix, as that is not a know type
    // instead this is a jagged-array: an array of arrays.
    // I will use the array data type for this
    array<array<int, 4>, 4> mat1;
    // as array has a fixed size, its elements are initialized
    // using the default constructor (of int in this case)
    // so I will copy your for construct
    int n = 0;
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            mat1[i][j] = vet3[n++];
}

Upvotes: 0

Fogmoon
Fogmoon

Reputation: 569

  • Why you use different integer type for your vector? From your code, you should handle similar integer type, so just use same integer type.
  • From your code, vet3 should reserve size 16 by vector<int> vet3(16);
  • Your matrix vector statement is not right, should be vector<vector<int> > mat1(4, vector<int>(4));

  • Your intercalating vet1 and vet2 method is wrong, should be like this:

    for (i = 0; i < 8; i++) //Laço que intercala vet1 e vet2 sequencialmente para criar vet3 com 16 posições
    {
        vet3[i*2] = vet1[i];    // even index
        vet3[i*2+1] = vet2[i];  // odd index
    }
    

The complete code like this:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> vet1; //Cria vector, tipo int, com nome vet1
    vector<int> vet2; //Cria vector, tipo long, com nome vet2
    vector<int> vet3(16); //Cria vector, tipo short, com nome vet3
    vector<vector<int> > mat1(4, vector<int>(4)); //Cria matriz 4x4, tipo  int, com nome mat1

    int data, i, j, n = 0;

    for (i = 0; i < 8; i++) //Laço para preencher o vet1 de dados
    {
        cout << "\nDigite um numero para a posicao " << vet1.size() << " do vetor 1: " << endl;
        cin >> data;
        vet1.push_back(data); //função que coloca o valor digitado no final do vetor vet1
    }

    for (i = 0; i < 8; i++)
    {
        cout << "\nDigite um numero para a posicao " << vet2.size() << " do vetor 2: " << endl;
        cin >> data;
        vet2.push_back(data); //função que coloca o valor digitado no final do vetor vet21
    }

    for (i = 0; i < vet1.size(); i++) //Laço para imprimir vet1
    {
        cout << "\t[" << vet1[i] << "]";
    }
    for (i = 0; i < vet2.size(); i++) //Laço para imprimir vet2
    {
        cout << "\t[" << vet2[i] << "]";
    }
    for (i = 0; i < 8; i++) //Laço que intercala vet1 e vet2 sequencialmente para criar vet3 com 16 posições
    {
        vet3[i*2] = vet1[i];
        vet3[i*2+1] = vet2[i];
    }
    for (i = 0; i < vet3.size(); i++)
    {
        cout << "\t[" << vet3[i] << "]";
    }

    n = 0;
    for (i = 0; i < 4 ; i++) //Laço para inserir todos os elementos do vet3 numa matriz quadrada 4x4
    {
        for (j = 0; j < 4; j++)
        {
            mat1[i][j] = vet3[n];
            n++;
        }
    }
    return 0;
}

Hope this help!

Upvotes: 0

pmaxim98
pmaxim98

Reputation: 236

You could just emplace the items from the vectors one by one:

    for (const auto & elem : vet1)
        vet3.emplace_back(elem);
    for (const auto & elem : vet2)
        vet3.emplace_back(elem);

You need C++11 (I think, not sure atm since it's been a while) to compile with the code of block above...

There are still some problems with your code: It doesn't make much sense to combine elements of two vectors with different types (in your case int with long => short). This prevents you from using standard functions like std::copy() which would've came handy in this situation (or not, since I'm pretty sure the point of the exercise was to do this manually, element by element). The matrix needn't a "vector" before its alias ("int" was what you had to declare it with).

Upvotes: 0

aschepler
aschepler

Reputation: 72483

vet3 is empty, so vet3[i+n] is invalid. You need to resize it or use push_back.

Upvotes: 1

Related Questions