William Minerva Posted February 10, 2022 at 04:20 PM Share Posted February 10, 2022 at 04:20 PM Construa um programa que calcule o determinante de uma matriz 3x3 usando Regra de Sarrus. (Usando arranjo bidimensional para para representar os dados e um laço duplo para fazer a leitura da matriz). Quote Link to comment Share on other sites More sharing options...
Fernando Mercês Posted February 10, 2022 at 05:00 PM Share Posted February 10, 2022 at 05:00 PM Opa, Vamos preicsar de mais que o enunciado. ? Dá uma lida ó: Valeu! Quote Link to comment Share on other sites More sharing options...
William Minerva Posted February 10, 2022 at 05:44 PM Author Share Posted February 10, 2022 at 05:44 PM Eu escrevi o seguinte programa: #include <stdio.h> #include <stlib.h> int main() { int i, j, det; int matriz[3][3]; for(i=0; i<3; i++) { for(j=0; j<3; j++) { scanf("%d", &matriz[i][j]); } } det = (matriz[0][0]*matriz[1][1]*matriz[2][2]) + (matriz[0][1]*matriz[1][2]*matriz[2][0]) + (matriz[0][2]*matriz[1][0]*matriz[2][1]) - ((matriz[0][1]*matriz[1][0]*matriz[2][2]) + (matriz[0][0]*matriz[1][2]*matriz[2][1]) + (matriz[0][2]*matriz[1][1]*matriz[2][0])); printf("%d", det); return 0; } Mas como eu posso ler as matrizes utilizando os comandos while e do-while? Quote Link to comment Share on other sites More sharing options...
fredericopissarra Posted February 11, 2022 at 11:24 AM Share Posted February 11, 2022 at 11:24 AM Você já está fazendo isso com aqueles dois loops `for`, lá em cima... Quote Link to comment Share on other sites More sharing options...
mauro_b Posted February 11, 2022 at 11:21 PM Share Posted February 11, 2022 at 11:21 PM (edited) Olá @William Minerva É Isso mesmo, as diferenças são poucos e orbitam a sintaxe porque é logicamente igual. Para fim de exemplo, suponho fazer o somatório dos itens de uma lista de n-inteiros. Para isso usarei: for {} e do {} while. Exemplo — escrevo um programa que informada a lista de n-termos responde com somatório dos termos. C language /> _ #include"stdio.h" int main (void) { #define n (8) int termos[n] = { 1,2,3,4,5,6,7,8 }; unsigned long i; int for_somatorio = 0; printf ("FOR-SOMATORIO\n("); for (i = 0; i < n; ++i) { printf (" %d ", termos[i]); for_somatorio += termos[i]; } printf (") = %d\n\n", for_somatorio); int do_somatorio = 0; printf ("DO WHILE-SOMATORIO\n("); i = 0; do { printf (" %d ", termos[i]); do_somatorio += termos[i]; } while (++i, i < n); printf (") = %d\n\n", do_somatorio); printf ("Fim-programa\n"); return 0; } SAÍDA [?] — notou..., 'sintaxe' distinta, resultados iguais. Edited February 11, 2022 at 11:25 PM by mauro_b Quote Link to comment Share on other sites More sharing options...
fredericopissarra Posted February 12, 2022 at 01:01 AM Share Posted February 12, 2022 at 01:01 AM A bem da verdade, for é a mesma coisa que while, só que abreviado: for (inicialização; condição; incremento) statement; inicializacao; while (condicao) { statement; incremento; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.