ERGASTHRIO 9

Pinakes kai xrhsh tous mesa se synarthseis

 

1. Programma pollaplasiasmou 2 disdiastatwn pinakwn (mexri 20 grammes kai sthles o kathenas).

To apotelesma tou poll/smou enos pinaka A(MxP) ki enosB(PxN) einai enas pinakas AB(MxN) ki oti ginetai mesw ths eksiswhs:

            AB(i,j) = Sigma(apo k=1 ws k=P) {A(i,k)*B(k,j)

As dokimastei to programma gia 2 pinakes A kai B, ek twn opoiwn o A na einai o monadiaios wste oAB na einai o B, p.x.

A(3,3) =

1 0 0

0 1 0

0 0 1

 kai

B(3,5)=

1 2 3 4 5

1 3 5 7 9

2 4 6 8 10

 

 

/*Pollaplasiasmos disdiastatwn pinakwn me mexri 20

            grammes kai 20 sthles*/

#include <stdio.h>

#define N_MAX_ROWS 20

#define N_MAX_COLS 20

 

int main(void)

{

            int A[N_MAX_ROWS][N_MAX_COLS];

            int B[N_MAX_ROWS][N_MAX_COLS];

            int AB[N_MAX_ROWS][N_MAX_COLS];

            int NA_rows, NA_cols, NB_rows, NB_cols;

            int i1, i2, j1, j2, j;

            FILE *input1, *input2;

 

/*Oi pinakes diavazontai apo arxeia, opou sthn prwth grammh exoun ton

arithmo grammwn kai ton arithmo sthlwn,  kai oi ypoloipes

grammes periexoun ta stoixeia tou pinaka*/

            input1=fopen("ARRAY1.txt", "r");

            input2=fopen("ARRAY2.txt", "r");

 

            /* Eisagwgh prwtoy pinaka*/

            fscanf(input1, "%d %d", &NA_rows, &NA_cols);

            for(i1=0;i1<NA_rows;i1++)

            {

                        for(j1=0;j1<NA_cols;j1++)

                        {

                                    fscanf(input1,"%d", &A[i1][j1]);

                        }

            }

            fclose(input1);

 

 

            /* Eisagwgh deyteroy pinaka*/

            fscanf(input2, "%d %d", &NB_rows, &NB_cols);

            for(i2=0;i2<NB_rows;i2++)

            {

                        for(j2=0;j2<NB_cols;j2++)

                        {

                                    fscanf(input2,"%d", &B[i2][j2]);

                        }

            }

            fclose(input2);

 

 

            if (NA_cols != NB_rows)

                        printf("Enas pinakas %dx%d den mporei "

                                                "na pollaplasiastei me enan "

                                                "pinaka %dx%d\n", NA_rows, NA_cols,\

                                                NB_rows, NB_cols);

            else

            {

                        /*Routina pollaplasiasmou

                        ABij=SIGMA(k=1->k=n){Aik*Bkj} */

 

                        for (i1=0;i1<NA_rows;i1++)

                        {

                                    for (j2=0;j2<NB_cols;j2++)

                                    {

                                                AB[i1][j2] = 0;

                                                for (j=0;j<NA_cols;j++)

                                                {

                                                            AB[i1][j2] +=\

                                                            A[i1][j]*B[j][j2];

                                                }

                                    }

                        }

                        printf("O neos pinakas exei %d grammes"

                                                " kai %d sthles\n"

                                                "AB[%d][%d]=\n", NA_rows, \

                                                NB_cols, NA_rows,NB_cols);

                        for (i1=0;i1<NA_rows;i1++)

                        {

                                    for (j2=0;j2<NB_cols;j2++)

                                    {

                                                printf("%-7d ",AB[i1][j2]);

                                    }

                                    printf("\n");

                        }

            }/*gia to else*/

 

            return 0;

 

}




2.           To idio programma xrhsimopoiontas mia synarthsh gia ton pollaplasiasmo.

 

 

 

/*Pollaplasiasmos disdiastatwn pinakwn me mexri 20

      grammes kai 20 sthles alla mesw synarthsh*/

#include <stdio.h>

#define N_MAX_ROWS 20

#define N_MAX_COLS 20

 

void matmult(int A1[][N_MAX_COLS], int B1[]\

                                          [N_MAX_COLS], int AB1[][N_MAX_COLS],

                              int N1, int N2, int N3);

int main(void)

{

      int A[N_MAX_ROWS][N_MAX_COLS];

      int B[N_MAX_ROWS][N_MAX_COLS];

      int AB[N_MAX_ROWS][N_MAX_COLS];

      int NA_rows, NA_cols, NB_rows, NB_cols;

      int i1, i2, j1, j2;

      FILE *input1, *input2;

 

      /*Oi pinakes diavazontai apo arxeia, opou

      sthn prwth grammh exoun ton arithmo grammwn

      kai ton arithmo sthlwn,  kai oi ypoloipes

      grammes periexoun ta stoixeia tou pinaka*/

      input1=fopen("ARRAY1.txt", "r");

      input2=fopen("ARRAY2.txt", "r");

 

      /* Eisagwgh prwtoy pinaka*/

      fscanf(input1, "%d %d", &NA_rows, &NA_cols);

      for(i1=0;i1<NA_rows;i1++)

      {

                  for(j1=0;j1<NA_cols;j1++)

                  {

                              fscanf(input1,"%d", &A[i1][j1]);

                  }

      }

      fclose(input1);

 

 

      /* Eisagwgh deyteroy pinaka*/

      fscanf(input2, "%d %d", &NB_rows, &NB_cols);

      for(i2=0;i2<NB_rows;i2++)

      {

                  for(j2=0;j2<NB_cols;j2++)

                  {

                              fscanf(input2,"%d", &B[i2][j2]);

                  }

      }

      fclose(input2);

 

 

      if (NA_cols != NB_rows)

                  printf("Enas pinakas %dx%d den mporei "

                                          "na pollaplasiastei me enan "

                                          "pinaka %dx%d\n", NA_rows, NA_cols,\

                                          NB_rows, NB_cols);

      else

      {

                  matmult(A, B, AB, NA_rows, NB_cols,\

                                          NA_cols);

                  printf("O neos pinakas exei %d grammes"

                                          " kai %d sthles\n"

                                          "AB[%d][%d]=\n", NA_rows, \

                                          NB_cols, NA_rows,NB_cols);

                  for (i1=0;i1<NA_rows;i1++)

                  {

                              for (j2=0;j2<NB_cols;j2++)

                              {

                                          printf("%-7d ",AB[i1][j2]);

                              }

                              printf("\n");

                  }

      }/*gia to else*/

 

      return 0;

}

 

void matmult(int A1[][N_MAX_COLS], int B1[]\

                                          [N_MAX_COLS], int AB1[][N_MAX_COLS],\

                              int N1, int N2, int N3)

{

      int i1,j2, j;

 

 

      /*Routina pollaplasiasmou

                  ABij=SIGMA(k=1->k=n){Aik*Bkj} */

 

                  for (i1=0;i1<N1;i1++)

                  {

                              for (j2=0;j2<N2;j2++)

                              {

                                          AB1[i1][j2] = 0;

                                          for (j=0;j<N3;j++)

                                          {

                                                      AB1[i1][j2] +=\

                                                      A1[i1][j]*B1[j][j2];

                                          }

                              }

                  }

 

 

      return;

 

}