Advertisement
kozubovskyy

lettura matrice da file con relative posizione

Mar 9th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #define d 3
  5.  
  6. using namespace std;
  7.  
  8. struct struttura
  9. {
  10.     int matrice[d][d];
  11.     int posizione[2] ;
  12. } ;
  13. int main()
  14. {
  15.     struttura matrices[9] ;
  16.     ifstream file ;
  17.     file.open("input.txt") ;
  18.     if (!file)
  19.     {
  20.         cout << "ERRORE: File non presente o danneggiato" ;
  21.         system("pause") ;
  22.     }
  23.     int a = 1 ;
  24.     for (int i = 0 ; i < d ; i++ )
  25.     {
  26.  
  27.         for (int j = 0 ; j < d ; j++ )
  28.         {
  29.             file >> matrices[0].matrice[i][j] ;
  30.             matrices[a].posizione[0] = {i} ;
  31.             matrices[a].posizione[1] = {j} ;
  32.             a++ ;
  33.         }
  34.     }
  35.     // stampa matrice
  36.     a = 1 ;
  37.     cout << "Stampa matrice: " << endl ;
  38.     for (int i = 0 ; i < d ; i++ )
  39.     {
  40.         for (int j = 0 ; j < d ; j++ )
  41.         {
  42.             cout << "|" << matrices[0].matrice[i][j] << "| " ;
  43.             cout <<  matrices[a].posizione[0] << matrices[a].posizione[1] << "  " ;
  44.             a++ ;
  45.         }
  46.         cout <<endl ;
  47.     }
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement