18 Eylül 2013 Çarşamba

Matrislerde toplama, fark, transpoze ve çarpım

Sponsorlu Bağlantılar


// programda random ile seçilmiş 2 tane matrisin toplam, fark, çarpım ve transpozesi ayrı ayrı fonksiyonlar yardımı ile yapılmıştır.
#include<stdio.h>
#include<stdlib.h>
#define S 3
void m_toplam(int [][],int [][],int[][]);
void m_carpim(int [][],int [][],int [][]);
void m_fark(int a[][], int b[][], int c[][]);
void transpoze1(int [][],int [][]);
void transpoze2(int [][],int [][]);

int main()
{
    int i,j;
 
    int d[S][S]={};
    int d2[S][S]={};
    int d3[S][S]={};
    int d4[S][S]={};
    char cevap;
    sor: // kullanici farkli bir karakter girerse goto devreye girecek
    printf("\n ilk matris");
    for(i=0;i<S;i++)
    {
                    printf("\n\t");
                    for(j=0;j<S;j++)
 
                    {
                    d[i][j]=-5+(rand()%10);
                    printf("%2d",d[i][j]);
                    }
                    }
 
    printf("\n Ikinci matris");
                 
    for(i=0;i<S;i++)
    {
                    printf("\n\t");
                    for(j=0;j<S;j++)
                    {
                    d2[i][j]=-5+(rand()%10);
                    printf("%2d",d2[i][j]);
                 
                    }
                    }
                    printf("\n\n");
                 
                printf("Yapmak istediginiz islemi seciniz :\n");
                printf("A) Matris toplami \n");
                printf("B) Matris farki :\n");
                printf("C) Matrislerin carpimi \n");
                printf("D) Matrislerin transpozesi \n");
                printf("E) Cikis\n");
                scanf("%c",&cevap);
                switch(cevap)
                  {
                              case 'A':
                              printf("Girilen iki matrisin toplami -->\n");  // buyuk kucuk harf duyarliligi kalmadi...
                              m_toplam(d,d2,d3);  // fonksiyonun çagirilmasi
                              break;
                           
                              case 'a':
                              printf("Girilen iki matrisin toplami -->\n");
                              m_toplam(d,d2,d3);  // fonksiyonun çagirilmasi
                              break;
                           
                              case 'B':
                              printf("Girilen iki matrisin farki -->\n");
                              m_fark(d,d2,d3);
                              break;
                           
                              case 'b':
                              printf("Girilen iki matrisin farki -->\n");
                              m_fark(d,d2,d3);
                              break;
                           
                              case 'C':
                              printf("Girilen iki matrisin carpimi -->\n");
                              m_carpim(d,d2,d3);
                              break;
                           
                              case 'c':
                              printf("Girilen iki matrisin carpimi -->\n");
                              m_carpim(d,d2,d3);
                              break;
                           
                              case 'D':
                              printf("Girilen ilk matrisin transpozesi -->\n");
                              transpoze1(d,d3);
                              printf("Girilen iki matrisin transpozesi -->\n");
                              transpoze2(d2,d4);
                              break;
                           
                              case 'd':
                              printf("Girilen ilk matrisin transpozesi -->\n");
                              transpoze1(d,d3);
                              printf("Girilen ikinci matrisin transpozesi -->\n");
                              transpoze2(d2,d4);
                              break;
                           
                              case 'E':
                              printf("isleminiz bitti");
                              break;
                           
                              case 'e':
                              printf("isleminiz bitti");
                              break;
                           
                              default:
                              printf("Yanlis bir secenek girdiniz lutfen tekrar deneyiniz...\n");
                              goto sor;
                          
                              }
                    system("pause");
                    }
void m_toplam(int a[S][S], int b[S][S], int c[S][S])
/* c = a +b */
{
  int i,j;


  for (i=0; i<S; i++)
 {
     for (j=0; j<S; j++)
                 {
                 c[i][j] = a[i][j] + b[i][j];
                 }
                 }
                 for (i=0; i<S; i++)
                 {
                     printf("\n\t");
                     for(j=0; j<S; j++)
                     {
                    printf("%2d",c[i][j]);
}
}
}
void m_carpim(int a[S][S], int b[S][S], int c[S][S])
/* c = a * b */
{
  int i, j, k;

  for (i = 0; i<S; i++)
      for (j = 0; j<S; j++) {
          c[i][j] = 0;
          for (k=0; k<S; k++)
              c[i][j] = c[i][j] + a[i][k]*b[k][j];
      }
       for (i=0; i<S; i++)
                 {
                     printf("\n\t");
                     for(j=0; j<S; j++)
                     {
                    printf("%2d",c[i][j]);

}
}
}             
void transpoze1(int a[S][S], int b[S][S])
{
  int i, j;

  for (i = 0; i<S; i++)
  {
      for (j = 0; j<S; j++)
          b[i][j] = a[j][i];
          }
       
      for (i=0; i<S; i++)
                 {
                     printf("\n\t");
                     for(j=0; j<S; j++)
                     {
                    printf("%2d",b[i][j]);
                    }
                    }
}
void transpoze2(int a[S][S], int b[S][S])
{
  int i, j;

  for (i = 0; i<S; i++)
  {
      for (j = 0; j<S; j++)
          b[i][j] = a[j][i];
          }
       
      for (i=0; i<S; i++)
                 {
                     printf("\n\t");
                     for(j=0; j<S; j++)
                     {
                    printf("%2d",b[i][j]);
                    }
                    }
}
void m_fark(int a[S][S], int b[S][S], int c[S][S])
/* c = a - b */
{
  int i,j;

  for (i=0; i<S; i++)
  {
      for(j=0; j<S; j++)
      {
      c[i][j] = a[i][j] - b[i][j];
      }
      }
      for (i=0; i<S; i++)
                 {
                     printf("\n\t");
                     for(j=0; j<S; j++)
                     {
                    printf("%2d",c[i][j]);
}
}
}
Sponsorlu Bağlantılar

Hiç yorum yok:

Yorum Gönder