Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define NMAX 100
  5.  
  6. void leggimatrice(char file[], float mat[NMAX][NMAX], int *righe, int *colonne);
  7. void stampamatrice(float mat[NMAX][NMAX], int righe, int colonne);
  8. void vettorepari(float mat[NMAX][NMAX], int righe, int colonne, int *valore1, float vpari[NMAX]);
  9. void vettoredispari(float mat[NMAX][NMAX], int righe, int colonne, int *valore1, float vdispari[NMAX]);
  10. void bubble_sort(float v[], int riemp);
  11.  
  12. int main()
  13. {
  14.     char file[20];
  15.     float mat[NMAX][NMAX];
  16.     int righe;
  17.     int colonne;
  18.     int valore1;
  19.     int valore2;
  20.     float vpari[NMAX];
  21.     float vdispari[NMAX];
  22.  
  23.     printf("Inserisci il nome del file:");
  24.     scanf("%s",file);
  25.  
  26.     leggimatrice(file,mat,&righe,&colonne);
  27.     stampamatrice(mat,righe,colonne);
  28.     vettorepari(mat,righe,colonne,&valore1,vpari);
  29.     vettoredispari(mat,righe,colonne,&valore2,vdispari);
  30.  
  31.     printf("\n\n");
  32.  
  33.     printf("VETTORE PARI ORDINATO\n");
  34.     bubble_sort(vpari, valore1);
  35.     for(int i=0; i<valore1; i++)
  36.         printf("%.1f\t", vpari[i]);
  37.     printf("\n\n");
  38.  
  39.     printf("VETTORE DISPARI ORDINATO\n");
  40.     bubble_sort(vdispari, valore2);
  41.     for(int i=0; i<valore2; i++)
  42.         printf("%.1f\t", vdispari[i]);
  43.     printf("\n");
  44.  
  45.     return 0;
  46. }
  47.  
  48. void leggimatrice(char file[], float mat[NMAX][NMAX], int *righe, int *colonne){
  49.     int r;
  50.     int c;
  51.   FILE *fp;
  52.  
  53.  fp=fopen(file, "r");
  54.  fscanf(fp,"%d",&r);
  55.  fscanf(fp,"%d",&c);
  56.  *righe=r;
  57.  *colonne=c;
  58.  
  59.  for(int i=0;i<*righe;i++){
  60.     for(int j=0;j<*colonne;j++){
  61.         fscanf(fp,"%f",&mat[i][j]);
  62.      }
  63.   }
  64.  fclose(fp);
  65. }
  66.  
  67. void stampamatrice(float mat[NMAX][NMAX], int righe, int colonne){
  68.   printf("\n");
  69.   printf("Matrice creata:");
  70.   printf("\n");
  71.  
  72.   for(int i=0;i<righe;i++){
  73.         printf("\n");
  74.     for(int j=0;j<colonne;j++){
  75.         printf("%.1f",mat[i][j]);
  76.         printf("\t");
  77.     }
  78.   }
  79.   printf("\n");
  80. }
  81.  
  82. void vettorepari(float mat[NMAX][NMAX], int righe, int colonne, int *valore1, float vpari[NMAX]){
  83.  int k=0;
  84.  
  85.  for(int i=0;i<righe;i++){
  86.     for(int j=0;j<colonne;j++){
  87.         if(i%2==0 && j%2==0){
  88.             vpari[k]=mat[i][j];
  89.             k++;
  90.          }
  91.        }
  92.      }
  93.    *valore1=k;
  94.  
  95.  printf("\n");
  96.  printf("VETTORE PARI:");
  97.  printf("\n");
  98.  
  99.  for(int i=0;i<*valore1;i++){
  100.     printf("%.1f ", vpari[i]);
  101.   }
  102.   printf("\n");
  103. }
  104.  
  105. void vettoredispari(float mat[NMAX][NMAX], int righe, int colonne, int *valore1, float vdispari[NMAX]){
  106.  int y=0;
  107.  
  108.  for(int i=0;i<righe;i++){
  109.     for(int j=0;j<colonne;j++){
  110.         if(i%2!=0 && j%2!=0){
  111.             vdispari[y]=mat[i][j];
  112.             y++;
  113.          }
  114.        }
  115.      }
  116.    *valore1=y;
  117.  
  118.  printf("\n");
  119.  printf("VETTORE DISPARI:");
  120.  printf("\n");
  121.  
  122.  for(int i=0;i<*valore1;i++){
  123.     printf("%.1f ", vdispari[i]);
  124.   }
  125.   printf("\n");
  126. }
  127.  
  128. void bubble_sort(float v[], int riemp){
  129.     float temp;
  130.     bool modified;
  131.  
  132.     modified = true;
  133.     for(int k=0;k<riemp-1 && modified;k++){
  134.       modified = false;
  135.         for(int i=0;i<riemp-k-1;i++){
  136.             if(v[i]>v[i+1]){
  137.                  temp=v[i];
  138.                  v[i]=v[i+1];
  139.                  v[i+1]=temp;
  140.                  modified = true;
  141.             }
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement