sábado, 8 de dezembro de 2012

Média das Diagonais de Uma Matriz

Este simples programa está áptio a imprimir
as diagonais de uma matriz,
e esta aqui é do tipo float 3 x 3.
Além de imprimir as diagonais da matriz,
o programa também imprime
a média dos valores somados nas diagonais.
Muito simples de entender e indicado
para iniciantes em linguagem C.
foi criado no eclipse, e não apresentou nenhum warnings.

Veja a saída deste programa no cmd:




#include <stdio.h>
#include <conio.h>
#define tam 10
/*============================================================================*/
void Informe ( ) {
    textcolor ( LIGHTBLUE );
    gotoxy ( 26, 22 );
    printf ( "Criado por: " );
    textcolor ( LIGHTMAGENTA );
    gotoxy ( 126, 20 );
    printf ( "Samuel Lima" );
    textcolor ( BLACK );
    gotoxy ( 26, 21 );
    printf ( "sa_sp10@hotmail.com" );
    textcolor ( LIGHTRED );
    gotoxy ( 36, 24 );
    printf ( "MUITO OBRIGADO" );
}
/*============================================================================*/
void Janela5 ( ) {
    int lin, col;
    col = 0;
    system ( "Color 10" );
    for ( lin = 2; lin <= 24; lin++ )
        for ( col = 3; col <= 78; col++ ) {
            gotoxy ( col, lin );
            textbackground ( WHITE );
            printf ( " " );
        }
}
/*============================================================================*/
int main ( ) {
    Janela5 ( );
    system ( "title MÉDIA DAS DIAGONAIS DE UMA MATRIZ" );
    float matriz [ 3 ] [ 3 ];
    int i = 0, j = 0;
    float soma = 0, result = 0, media = 0, me_dia = 0;
    for ( i = 0; i < 3; i++ ) {
        for ( j = 0; j < 3; j++ ) {
            textcolor ( LIGHTRED );
            gotoxy ( 28, 3 );
            printf ( "MÉDIA DAS DIAGONAIS DE UMA MATRIZ" );
            textcolor ( LIGHTBLUE );
            gotoxy ( 18, 5 );
            printf ( "DIGITE UM VALOR PARA A POSIÇÃO %d :%d DA MATRIZ : ", i,j );
            textcolor ( LIGHTRED );
            scanf ( "%f", &matriz [ i ] [ j ] );
        }
    }
    textcolor ( LIGHTRED );
    gotoxy ( 27, 7 );
    printf ( "IMPRIMINDO A MATRIZ ORIGINAL" );
    printf ( "\n" );
    textcolor ( BLACK );
    for ( i = 0; i < 3; i++ ) {
        printf ( "\n" );
        gotoxy ( 33, i + 9 );
        for ( j = 0; j < 3; j++ ) {
            printf ( " %.1f ", matriz [ i ] [ j ] );
        }
    }
    getche ( );
    textcolor ( LIGHTRED );
    gotoxy ( 17, 13 );
    printf ( "IMPRIMINDO A DIAGONAL PRIMÁRIA: " );
    textcolor ( BLACK );
    for ( i = 0; i < 3; i++ ) {
        for ( j = 0; j < 3; j++ ) {
            if ( i + j == 2 ) {
                printf ( " %.1f ", matriz [ i ] [ j ] );
                soma += matriz [ i ] [ j ] / 3;
            }
        }
    }
    media = soma;
    textcolor ( LIGHTRED );
    gotoxy ( 17, 15 );
    printf ( "A MÉDIA DA DIAGONAL PRIMÁRIA É: " );
    textcolor ( BLACK );
    gotoxy ( 50, 15 );
    printf ( "%.1f", media );
    getche ( );
    textcolor ( LIGHTRED );
    gotoxy ( 17, 17 );
    printf ( "IMPRIMINDO A DIAGONAL SECUNDÁRIA: " );
    textcolor ( BLACK );
    for ( i = 0; i < 3; i++ ) {
        for ( j = 0; j < 3; j++ ) {
            if ( i == j ) {
                printf ( " %.1f ", matriz [ i ] [ j ] );
                result += matriz [ i ] [ j ] / 3;
            }
        }
    }
    me_dia = result;
    textcolor ( LIGHTRED );
    gotoxy ( 17, 19 );
    printf ( "A MÉDIA DA DIAGONAL SECUNDÁRIA É: " );
    textcolor ( BLACK );
    gotoxy ( 52, 19 );
    printf ( "%.1f", me_dia );
   Informe ( );
    getche ( );
}
/*============================================================================*/

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.