-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.
– anonimo
I think the problem is the concatenation, I’ll trade for strcpy instead of strcat
– Raul Lima