Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- void carica_vet(char path[], int vet[], int &riemp);
- void stampa_vet(int vet[], int &riemp);
- void ricerca_max(int vet[], int &riemp, int &max, int &contatore);
- int main()
- {
- char path[256]; //Dichiaro variabili
- int *vet;
- int riemp;
- int max;
- int contatore = 0;
- carica_vet(path, vet, riemp);
- stampa_vet(vet, riemp);
- ricerca_max(vet, riemp, max, contatore);
- delete[]vet;
- system("pause");
- return 0;
- }
- void carica_vet(char path[], int vet[], int &riemp)
- {
- //Inserimento da tastiera del percorso del file da aprire
- cout <<"Inserire nome del file da aprire:" << endl;
- cin >> path;
- // Apro il file
- fstream f;
- f.open(path, ios::in);
- if (!f) // Controllo file
- {
- cout << "Errore nell'apertura del file" << endl;;
- exit(-1);
- }
- vet = new int[riemp];
- //Inserisco elementi dal file nel vettore
- f >> vet[0];
- while (!f.eof()) //Continua finché non trovi l'end of file
- f >> vet[riemp++]; //Inserisci elementi nel vettore
- riemp--;
- f.close(); //Chiudo File
- }
- void stampa_vet(int vet[], int &riemp)
- {
- cout <<"Il vettore inserito e':" << endl;
- for(int i = 0; i < riemp; i++)
- {
- cout << vet[i] << endl;
- }
- }
- void ricerca_max(int vet[], int &riemp, int &max, int &contatore)
- {//Ricerca del Minimo e del Massimo
- max=vet[0];
- for(int i = 0; i < riemp; i++)
- {
- if (vet[i] > max)
- max = vet[i];
- }
- //Calcolo ripetizione del massimo
- for (int i=0;i<riemp;i++)
- {
- if (vet[i] == max)
- contatore++;
- }
- // Stampa valore massimo trovato e della sua ripetizione
- cout <<"Il valore massimo e':" << max << " ed e' ripetuto " <<contatore << " volte." << endl;
- }
Add Comment
Please, Sign In to add comment