Ir para conteúdo
Fórum Script Brasil
  • 0

transformação de matriz em vetor


Thales R. Queiroz

Pergunta

gostaria de tranformar essa matriz em vetor e não estou conseguindo.

#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

main(){

    int vetorS[12 * 4];
    int i= 0;
    int j=0;
    float S[12][4] = {
        {5, 5, 5, 5},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0}
       
    };
    int indice = 0;
    int x=0, y=0;
    
for( x = 0; x < i; x++ )
{
    for( y = 0; y < j; y++ )
    {
        vetorS[indice] = S[x][y];
        indice = indice + 1;
    }
}
cout<<vetorS<<endl;
}

Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

  • 0

A forma mais ou menos correta de se fazer seria essa:
 

#include <iostream>
using namespace std;

main() {

    unsigned const int NLINHAS = 12;
    unsigned const int NCOLUNAS = 4;
    unsigned const int TAM = NLINHAS * NCOLUNAS;

    int vetorS[TAM];
    int S[NLINHAS][NCOLUNAS] = {
        {5, 5, 5, 5},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0}

    };

    unsigned int x, y;
    int indice=0;

    for( x = 0; x < NLINHAS; x++ ) {
        for( y = 0; y < NCOLUNAS; y++, indice++) {
            vetorS[indice] = S[x][y];
        }
    }

    //mostrar vetor
    for( x = 0; x < TAM; x++ ) {
        cout<<vetorS[x];
    }

}

Mas se consideramos que toda matriz em realidade é um vetor podemos fazer isso:
 

#include <iostream>
using namespace std;

main() {

    unsigned const int NLINHAS = 12;
    unsigned const int NCOLUNAS = 4;
    unsigned const int TAM = NLINHAS * NCOLUNAS;

    int vetorS[TAM];
    int S[NLINHAS][NCOLUNAS] = {
        {5, 5, 5, 5},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0}

    };

    unsigned int x;

    //mostrar vetor
    for( x = 0; x < TAM; x++ ) {
        vetorS[x] = S[0][x];
        cout<<vetorS[x];
    }

}

E fica muito mais fácil não é.

 

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,9k
×
×
  • Criar Novo...