Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define NMAX 100
- void leggimatrice(char file[], float mat[NMAX][NMAX], int *righe, int *colonne);
- void stampamatrice(float mat[NMAX][NMAX], int righe, int colonne);
- void vettorepari(float mat[NMAX][NMAX], int righe, int colonne, int *valore1, float vpari[NMAX]);
- void vettoredispari(float mat[NMAX][NMAX], int righe, int colonne, int *valore1, float vdispari[NMAX]);
- void bubble_sort(float v[], int riemp);
- int main()
- {
- char file[20];
- float mat[NMAX][NMAX];
- int righe;
- int colonne;
- int valore1;
- int valore2;
- float vpari[NMAX];
- float vdispari[NMAX];
- printf("Inserisci il nome del file:");
- scanf("%s",file);
- leggimatrice(file,mat,&righe,&colonne);
- stampamatrice(mat,righe,colonne);
- vettorepari(mat,righe,colonne,&valore1,vpari);
- vettoredispari(mat,righe,colonne,&valore2,vdispari);
- printf("\n\n");
- printf("VETTORE PARI ORDINATO\n");
- bubble_sort(vpari, valore1);
- for(int i=0; i<valore1; i++)
- printf("%.1f\t", vpari[i]);
- printf("\n\n");
- printf("VETTORE DISPARI ORDINATO\n");
- bubble_sort(vdispari, valore2);
- for(int i=0; i<valore2; i++)
- printf("%.1f\t", vdispari[i]);
- printf("\n");
- return 0;
- }
- void leggimatrice(char file[], float mat[NMAX][NMAX], int *righe, int *colonne){
- int r;
- int c;
- FILE *fp;
- fp=fopen(file, "r");
- fscanf(fp,"%d",&r);
- fscanf(fp,"%d",&c);
- *righe=r;
- *colonne=c;
- for(int i=0;i<*righe;i++){
- for(int j=0;j<*colonne;j++){
- fscanf(fp,"%f",&mat[i][j]);
- }
- }
- fclose(fp);
- }
- void stampamatrice(float mat[NMAX][NMAX], int righe, int colonne){
- printf("\n");
- printf("Matrice creata:");
- printf("\n");
- for(int i=0;i<righe;i++){
- printf("\n");
- for(int j=0;j<colonne;j++){
- printf("%.1f",mat[i][j]);
- printf("\t");
- }
- }
- printf("\n");
- }
- void vettorepari(float mat[NMAX][NMAX], int righe, int colonne, int *valore1, float vpari[NMAX]){
- int k=0;
- for(int i=0;i<righe;i++){
- for(int j=0;j<colonne;j++){
- if(i%2==0 && j%2==0){
- vpari[k]=mat[i][j];
- k++;
- }
- }
- }
- *valore1=k;
- printf("\n");
- printf("VETTORE PARI:");
- printf("\n");
- for(int i=0;i<*valore1;i++){
- printf("%.1f ", vpari[i]);
- }
- printf("\n");
- }
- void vettoredispari(float mat[NMAX][NMAX], int righe, int colonne, int *valore1, float vdispari[NMAX]){
- int y=0;
- for(int i=0;i<righe;i++){
- for(int j=0;j<colonne;j++){
- if(i%2!=0 && j%2!=0){
- vdispari[y]=mat[i][j];
- y++;
- }
- }
- }
- *valore1=y;
- printf("\n");
- printf("VETTORE DISPARI:");
- printf("\n");
- for(int i=0;i<*valore1;i++){
- printf("%.1f ", vdispari[i]);
- }
- printf("\n");
- }
- void bubble_sort(float v[], int riemp){
- float temp;
- bool modified;
- modified = true;
- for(int k=0;k<riemp-1 && modified;k++){
- modified = false;
- for(int i=0;i<riemp-k-1;i++){
- if(v[i]>v[i+1]){
- temp=v[i];
- v[i]=v[i+1];
- v[i+1]=temp;
- modified = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement