Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
Copy 2d array to another 2d array

I'm trying to create a program in C that copies numbers from one 2d array to another, but specifically. (see image)

Code:

int main(){
    int numbers[6][8] = {
        {1,1,1,1,1,1,1,1},//0
        {2,2,2,2,2,2,2,2},//1
        {3,3,3,3,3,3,3,3},//2
        {4,4,4,4,4,4,4,4},//3
        {5,5,5,5,5,5,5,5},//4
        {6,6,6,6,6,6,6,6} //5
    };
    int output[2*8][3]; 
    
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 2*8; j++){
            output[j][i] = numbers[i][j];         
        }
    }  

    for(int j = 0; j < 2*8; j++){
        for(int i = 0; i < 3; i++){
            printf("%d ", output[j][i]);
        }
        printf("\n");
    }

 }

My output:

1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3

2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4

But it's bad.

The output should look like this:

1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3

4 5 6
4 5 6
4 5 6
4 5 6
4 5 6
4 5 6
4 5 6
4 5 6

Problem is that in my output in second part I have number from rows 1, 2 and 3 (from numbers array). But it was supposed to be the 3, 4 and 5 row.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You have to get your indexes right :-)

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 16; j++) {
        output[j][i] = numbers[j / 8 * 3 + i][j % 8];
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

In base of your code, i split in two cases, and the second part i put +2 like this: output[j][i] = numbers[i+2][j];

#include <iostream>
using namespace std;

int main(){
    int numbers[6][8] = {
        {1,1,1,1,1,1,1,1},//0
        {2,2,2,2,2,2,2,2},//1
        {3,3,3,3,3,3,3,3},//2
        {4,4,4,4,4,4,4,4},//3
        {5,5,5,5,5,5,5,5},//4
        {6,6,6,6,6,6,6,6} //5
    };
    int output[2*8][3]; 
    
    for(int i = 0; i < 3; i++){
          for(int j = 0; j < 8; j++){
            output[j][i] = numbers[i][j];         
          }
    }  
    
    for(int i = 0; i < 3; i++){
          for(int j = 8; j < 16; j++){
            output[j][i] = numbers[i+2][j];         
          }
    }  
   
    for(int j = 0; j < 2*8; j++){
        for(int i = 0; i < 3; i++){
            printf("%d ", output[j][i]);
        }
        printf("\n");
    }
 }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda