thinkabout Posted March 31, 2013 at 12:25 AM Report #500963 Posted March 31, 2013 at 12:25 AM (edited) Só faz a transposta da 1 linha, devo ter que usar algo auxiliar porque ele depois começa-se a baralhar todo. E mais uma vez não me livro dos warnings... 😕 'function' : 'int *' differs in levels of indirection from 'int [3][3]' 'transporta' : different types for formal and actual parameter 1 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> /* Desenvolva uma função que efectue a transposição numa matriz N*N de inteiros. A transposição consiste em trocar as linhas pelas colunas. A função recebe como argumentos o nome da matriz e o valor N (pode assumir que a matriz é quadrada). */ void transporta(int *matrizA, int N) { int i,j,*matrizATransporta=matrizA; for(i=0; i<N;i++) { for(j=0;j<N;j++) *(matrizA+i*N+j)=*(matrizATransporta+i+j*N); } for(i=0; i<N;i++) { for(j=0;j<N;j++) { printf("%d \t", *(matrizA+i*N+j)); } puts(""); } } int main() { int matrizA[3][3] = {{1,3,5},{6,3,2},{10,45,4}} ; transporta(matrizA,3); } Matriz Orignial 1 3 5 6 3 2 10 45 4 Output pretendido 1 6 10 3 3 45 5 2 4 Output que estou a obter. 1 6 10 6 3 45 10 45 4 Edited March 31, 2013 at 12:27 AM by thinkabout
Marzkor Posted March 31, 2013 at 01:48 AM Report #500970 Posted March 31, 2013 at 01:48 AM (edited) void transporta(int (*matrizA)[3], int N){ printf ("%d",matrizA[0][0]); } void main () { ..... transporta(matrizA,3); } Edited March 31, 2013 at 01:49 AM by Marzkor
HappyHippyHippo Posted March 31, 2013 at 06:30 AM Report #500981 Posted March 31, 2013 at 06:30 AM #include <stdio.h> void transporta(int *matrix, int N) { int i, j; int aux; for(i=0; i<N; i++) { for(j=i; j<N; j++) // <---- j=i { // troca de elementos !!! aux = *(matrix + (i*N) + j); *(matrix + (i*N) + j) = *(matrix + i + (j*N)); *(matrix + i + (j*N)) = aux; } } for(i=0; i<N; i++) { for(j=0; j<N; j++) { printf("%d \t", *(matrix + i*N + j)); } puts("\n"); } } int main() { int matriz[3][3] = {{1,3,5}, {6,3,2}, {10,45,4}} ; transporta(matriz, 3); return 0; } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted March 31, 2013 at 10:54 AM Author Report #500991 Posted March 31, 2013 at 10:54 AM Boas a versão do HappyHippyHippo corre na perfeição, tirando a luta dos warnings. Tentei por o cabeçalho como Marzkor diz mas ainda obtenho mais erros. void transporta(int (*matrizA)[3], int N) Error 2 error C2106: '=' : left operand must be l-value Error 4 error C2106: '=' : left operand must be l-value 5 IntelliSense: a value of type "int *" cannot be assigned to an entity of type "int" 6 IntelliSense: expression must be a modifiable lvalue 7 IntelliSense: expression must be a modifiable lvalue Warning 1 warning C4047: '=' : 'int' differs in levels of indirection from 'int *' Warning 3 warning C4047: '=' : 'int [3]' differs in levels of indirection from 'int' Contudo essa sintaxe trabalhou bem em. https://www.portugal-a-programar.pt/topic/59736-comparar-matrizes-duas-dimensoes-ponteiros/ Mas já voltou a trabalhar mal em. https://www.portugal-a-programar.pt/topic/59752-mostrar-matrizes-no-ecra/
pmg Posted March 31, 2013 at 11:28 AM Report #500995 Posted March 31, 2013 at 11:28 AM Boas a versão do HappyHippyHippo corre na perfeição, tirando a luta dos warnings. A versão do Hippo tem erro (que o compilador se queixa na forma de warnings). O erro dessa versão, normalmente, não se manifesta, e muitos programadores optam por ignorar o erro. O erro é considerar que os tipos ponteiro para int e ponteiro para array de int têm a mesma representação, o mesmo método de passagem entre funções, as mesmas necessidades de alinhamento, entre outros (provavelmente não há nenhum computador "normal" actual onde isso não seja verdade). Podes configurar o teu compilador para não emitir esses warnings (e perpetuar o erro) ou dar a volta ao problema passando o e3ndereço dum int em vez do endereço dum array. What have you tried? Não respondo a dúvidas por PM A minha bola de cristal está para compor; deve ficar pronta para a semana. Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!
HappyHippyHippo Posted March 31, 2013 at 10:06 PM Report #501039 Posted March 31, 2013 at 10:06 PM eu não queria fugir muito do código que tinhas, mas se o problema é então os warnings : #include <stdio.h> void transporta(int matrix[][3], int N) { int i, j; int aux; for(i=0; i<N; i++) { for(j=i; j<N; j++) // <---- j=i { // troca de elementos !!! aux = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = aux; } } for(i=0; i<N; i++) { for(j=0; j<N; j++) { printf("%d \t", matrix[i][j]); } printf("\n"); } } int main() { int matriz[3][3] = {{1,3,5}, {6,3,2}, {10,45,4}} ; transporta(matriz, 3); return 0; } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted March 31, 2013 at 10:49 PM Author Report #501046 Posted March 31, 2013 at 10:49 PM (edited) O teu código já a trabalhar com ponteiros, sem warnings. #include <stdio.h> void transporta(int *matrix, int N) { int i, j; int aux; for(i=0; i<N; i++) { for(j=i; j<N; j++) // <---- j=i { // troca de elementos !!! aux = *(matrix + (i*N) + j); *(matrix + (i*N) + j) = *(matrix + i + (j*N)); *(matrix + i + (j*N)) = aux; printf("O elemento que ficou no aux e %d \n", aux); } } for(i=0; i<N; i++) { for(j=0; j<N; j++) { printf("%d \t", *(matrix + i*N + j)); } puts("\n"); } } int main() { int matriz[3][3] = {{1,3,5}, {6,3,2}, {10,45,4}} ; transporta(&matriz[0][0], 3); return 0; } Edited March 31, 2013 at 10:50 PM by thinkabout
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now