Salve a tutti , oggi ho provato a fare un esercizio che dice :
"Letta da file una matrice di interi in cui sono specificati numero di riga come primo valore e numero di colonna come secondo valore, cancellare da questa matrice tutte e sole le colone crescenti"
Ho provato così:
MAIN:
HEADER:codice:#include <iostream> #include "CancRigCresc.h" using namespace std; int main(int argc, char** argv) { matrice m; int r,c; LeggiMatriceFile("Matrice.txt",m,r,c); StampaMatrice(m,r,c); cout<<"La matrice senza le righe crescenti e':\n"; EliminaRigCresc(m,r,c); StampaMatrice(m,r,c); return 0; }
BODY:codice:#include <fstream> #define N 100 typedef int vettore[N]; typedef int matrice[N][N]; typedef char VettoreCaratteri[N]; void LeggiMatriceFile(VettoreCaratteri,matrice,int&,int&); void StampaMatrice(matrice,int,int); void EliminaRiga(matrice,int&,int,int); bool RigaCrescente(matrice,int,int); void EliminaRigCresc(matrice,int&,int);
Mi Funziona SOLO se le righe crescenti sono 1 oppure 2 , altrimenti non va... e non capisco il motivo!codice:#include <iostream> #include "CancRigCresc.h" using namespace std; void LeggiMatriceFile(VettoreCaratteri nomefile,matrice m,int& r,int& c){ fstream f; r=0,c=0; f.open(nomefile,ios::in); if(!f) cout<<"Access Denied Or Can't found file\n"; else{ while(!f.eof()){ f>>r; f>>c; for(int i=0;i<r;i++) for(int j=0;j<c;j++) f>>m[i][j]; } } } void StampaMatrice(matrice m,int r,int c){ for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ cout<<m[i][j]<<" "; } cout<<endl; } } void EliminaRiga(matrice m,int& r,int c,int rigacan){ for(int j=0;j<c;j++) for(int i=rigacan;i<r;i++) m[i][j]=m[i+1][j]; r--; } bool RigaCrescente(matrice m,int rigafissata ,int c){ for(int j=0;j<c-1;j++){ if(m[rigafissata][j]<m[rigafissata][j+1]) return true; } return false; } void EliminaRigCresc(matrice m,int& r,int c){ int k=0; for(int i=0;i<r;i++){ if(RigaCrescente(m,i,c)){ EliminaRiga(m,r,c,i); r++; k++; } } r=r-k; }
ESEMPI:
1) 1 solo Riga Crescente:
provola 1.jpg
2)2 Righe Crescenti
provola 2.jpg
3) 3 Righe Crescenti
provola 3.jpg
4) 4 Righe Crescenti
http://i65.tinypic.com/sl2wit.png
Come vedete negli esempi 3) e 4) non funziona mentre in 1) e 2) si... Non riesco a capire il motivo
Grazie mille a chiunque mi aiuta![]()

Rispondi quotando

