Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.    
  4.  
  5.  
  6. void CreaVett(int **,int,int);
  7.  
  8.  
  9. int main()
  10. {
  11.  int M,N,m,n;
  12.  printf("Inserisci il numero di righe : ");
  13.  scanf("%d",&M);
  14.  printf("Inserisci il numero di colonne : ");
  15.  scanf("%d",&N);
  16.  int **Matrix;
  17.  Matrix=(int**)malloc(M*sizeof(int*));
  18.  for(int x=0; x<M;x++)
  19.  {
  20.     Matrix[x]= malloc(N*sizeof(int));
  21.  }
  22.  for(int i=0;i<M;i++)
  23.  {
  24.      for(int j=0;j<N;j++)
  25.      {
  26.          Matrix[i][j]=0;
  27.      }
  28.  }
  29.  int scelta=0;
  30.  int s;
  31.  int e;
  32.  while(scelta==0)
  33. {
  34.  printf("Quale righa  vuoi modificare ? : ");
  35.  scanf("%d",&m);
  36.  printf("Quale colonna vuoi modificare ? : ");
  37.  scanf("%d",&n);
  38.  printf("Quale elemento vuoi inserire ? : ");
  39.  scanf("%d",&e);
  40.  Matrix[m][n]=e;
  41.  printf("\n MATRICE A : \n");
  42.  for(int i=0;i<M;i++)
  43.  {
  44.      for(int j=0;j<N;j++)
  45.      {
  46.          printf("[%d]",Matrix[i][j]);
  47.      }
  48.      printf("\n");
  49.  }
  50.  printf("Vuoi continuare ? : \n 1)si \n 2)no  \n ");
  51.  scanf("%d",&s);
  52.  if(s ==1)
  53.  {
  54.    scelta = 0;
  55.  }
  56.  else
  57.  {
  58.  scelta = 1;
  59.  }
  60. }
  61.  CreaVett(Matrix,M,N);
  62.  return 0;
  63.  
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70. void CreaVett(int **Matrix, int M, int N)
  71. {
  72.  
  73. int contatore=0;
  74.    for(int i=0;i<M;i++)
  75.    {
  76.      for(int j=0;j<N;j++)
  77.      {
  78.          if(Matrix[i][j]!=0)
  79.           {
  80.              contatore=contatore+1;
  81.           }
  82.      }
  83.    }
  84. int DIM= contatore;
  85. int *vettA=malloc(DIM*sizeof(int)); /*CREO VETTORE A*/
  86. int i=0;
  87.    for(int x=0;x<M;x++)
  88.    {
  89.       for(int y=0;y<N;y++)
  90.       {
  91.        if (Matrix[x][y]!=0)
  92.          {
  93.            vettA[i]=Matrix[x][y];
  94.            i++;
  95.          }
  96.       }
  97.    }
  98. printf("VETTORE A : \n");
  99. for(int s=0;s<DIM;s++)
  100. {
  101.   printf("[%d]", vettA[s]);
  102. }
  103. int contatore3=0;
  104. int DIM1=DIM+1;
  105. int *vettIA=malloc(DIM1*sizeof(int)); /*CREO VETTORE IA*/
  106. vettIA[0]=0;
  107. for(int k=0; k<M;k++)
  108. {
  109.   for(int j=0;j<N;j++)
  110.    {
  111.       if(Matrix[k][j] !=0)
  112.       {
  113.       contatore3=contatore3+1;
  114.       }
  115.    }
  116.    vettIA[k+1]=vettIA[k]+contatore3;
  117.    contatore3=0;
  118. }
  119. printf("\nVETTORE IA : \n");
  120. for(int m=0; m<DIM1;m++)
  121. {
  122.   printf("[%d]",vettIA[m]);
  123. }
  124. int *vettJA=malloc(DIM*sizeof(int)); /*CREO VETTORE JA*/
  125. int f=0;
  126. for(int t=0;t<M;t++)
  127. {
  128.    for(int u=0;u<N;u++)
  129.    {
  130.       if(Matrix[t][u] != 0)
  131.       {
  132.        vettJA[f]=u;
  133.        f++;
  134.        }
  135.     }
  136. }
  137. printf("\nVETTORE JA :\n");
  138. for(int h=0;h<DIM;h++)
  139. {
  140.  printf("[%d]",vettJA[h]);
  141. }
  142. printf("\n");
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement