Advertisement
Guest User

ex 1,2

a guest
Dec 10th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct matrice {
  6.     int l, c;
  7.     int n;
  8.     int lin[100];
  9.     int col[100];
  10.     float x[100];
  11. };
  12.  
  13. void citire(struct matrice *m)
  14. {
  15.     int l1,c1;
  16.     scanf("%d%d", &l1, &c1);
  17.     m->l=l1;
  18.     m->c=c1;
  19.     m->n = 0;
  20.     int i, j;
  21.     float x;
  22.     for (i = 0; i < m->l; i++) {
  23.         for (j = 0; j < m->c; j++) {
  24.             scanf("%f", &x);
  25.             if (x) {
  26.                 m->x[m->n] = x;
  27.                 m->col[m->n] = j;
  28.                 m->lin[m->n] = i;
  29.                 m->n++;
  30.             }
  31.         }
  32.     }
  33. }
  34.  
  35. void afisare(struct matrice *m)
  36. {
  37.     int i, j, nr = 0;
  38.     for (i = 0; i < m->l; i++) {
  39.         for (j = 0; j < m->c; j++) {
  40.             if (m->lin[nr] == i && m->col[nr] == j) {
  41.                 printf("%.0f ", m->x[nr]);
  42.                 nr++;
  43.             }
  44.             else {
  45.                 printf("0 ");
  46.             }
  47.         }
  48.         printf("\n");
  49.     }
  50. }
  51.  
  52. void adunare(struct matrice *a, struct matrice *b)
  53. {
  54.     float c[100][100]={0};
  55.     for(int i=0;i<a->n;i++){
  56.         c[a->lin[i]][a->col[i]]=a->x[i];
  57.     }
  58.     for(int i=0;i<b->n;i++){
  59.         c[b->lin[i]][b->col[i]]+=b->x[i];
  60.     }
  61.     for(int i=0;i<a->l;i++){
  62.         for(int j=0;j<a->c;j++){
  63.             printf("%.0f ",c[i][j]);
  64.         }
  65.         printf("\n");
  66.     }
  67. }
  68. int main()
  69. {
  70.     struct matrice *a,*b;
  71.     a=(struct matrice *)calloc(1,sizeof(struct matrice));
  72.     b=(struct matrice *)calloc(1,sizeof(struct matrice));
  73.     citire(a);
  74.     citire(b);
  75.     adunare(a,b);
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement