Printing garbage in matrix string in C

Asked

Viewed 88 times

-1

Code Issue: Create a program that receives from user two vectors of size 5, whose elements are the names of persons (maximum size 10) who will dance in a party gang Junina. After this, the program must traverse the two vectors joining the ith names of each vector into a 2x5 matrix, or that is, will be formed pairs between the two vectors (forming the row for the gang). Finally, display the built matrix.

information:I had the idea to create a 2x5 matrix, and then do its transposes, but in your print presents memory junk, I would like to know how to fix this problem, and if part of the code that generates the transposed matrix is correct:

 //Matriz Transposta
for(i=0;i<N;i++){
    for(j=0;j<M;j++){
        strcat(transQuadrilha[j][i],quadrilha[i][j]);
    }
}
for(i=0;i<M;i++){
    for(j=0;j<N;j++){
        printf("%s ",transQuadrilha[i][j]);
    }
printf("\n");
}

code:

#include <stdio.h>
#include <string.h>
#define N 2
#define M 5
#define LIM 10
int main(){
    int i,j;
    char quadrilha[N][M][LIM];
    char transQuadrilha[N][M][LIM];
//prenximento do vetor tipo char
for(i=0;i<N;i++){
    for(j=0;j<M;j++){
        if(i==0){
        printf("Insira os nomes dos acompanhantes: \n");
        scanf("%[^\n]s",quadrilha[i][j]);
        getchar();
        }else{
        printf("Insira os nomes das acompanhantes: \n");
        scanf("%[^\n]s",quadrilha[i][j]);
        getchar
        }
    }
}
//MAtriz
for(i=0;i<N;i++){
    for(j=0;j<M; j++){
        printf("%s ",quadrilha[i][j]);
        }
    printf("\n");
    }
printf("\n");
//Matriz Transposta
for(i=0;i<N;i++){
    for(j=0;j<M;j++){
        strcat(transQuadrilha[j][i],quadrilha[i][j]);
    }
}
for(i=0;i<M;i++){
    for(j=0;j<N;j++){
        printf("%s ",transQuadrilha[i][j]);
    }
printf("\n");
}
}
  • If you have a matrix a[N][M] then your transpose will be: at[M][N] and not with the same dimensions as you stated.

  • I think the problem is the concatenation, I’ll trade for strcpy instead of strcat

1 answer

0


code:

#include <stdio.h>
#include <string.h>
#define N 2
#define M 5
#define LIM 10
int main(){
    int i,j;
    char quadrilha[N][M][LIM];
    char transQuadrilha[M][N][LIM];
//prenximento do vetor tipo char
for(i=0;i<N;i++){
    for(j=0;j<M;j++){
        if(i==0){
        printf("Insira os nomes dos acompanhantes: \n");
        scanf("%[^\n]s",quadrilha[i][j]);
        getchar();
        }else{
        printf("Insira os nomes das acompanhantes: \n");
        scanf("%[^\n]s",quadrilha[i][j]);
        getchar();
        }
    }
}
//MAtriz
for(i=0;i<N;i++){
    for(j=0;j<M; j++){
        printf("%s ",quadrilha[i][j]);
        }
    printf("\n");
    }
printf("\n");
//Matriz Transposta
for(i=0;i<N;i++){
    for(j=0;j<M;j++){
        strcpy(transQuadrilha[j][i],quadrilha[i][j]);
    }
}
for(i=0;i<M;i++){
    for(j=0;j<N;j++){
        printf("%s ",transQuadrilha[i][j]);
    }
printf("\n");
}
}

problem encountered: there was the impression of garbage, because, in the part of the code below, appeared the function strcat(which added up to what was in the quadrilha[i][j]with transQuadrilha[j][i] )instead of strcpy that copied the string to the matrix.

//Matriz Transposta
for(i=0;i<N;i++){
    for(j=0;j<M;j++){
        strcat(transQuadrilha[j][i],quadrilha[i][j]);
    }
}
for(i=0;i<M;i++){
    for(j=0;j<N;j++){
        printf("%s ",transQuadrilha[i][j]);
    }
printf("\n");
}
}

Browser other questions tagged

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