Guest User

Untitled

a guest
Jun 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. void carica_vet(char*, int*, int &);
  8. void stampa_vet(int*, int &);
  9. void ricerca_max(int*, int &, int &, int &);
  10.  
  11.  
  12. int main()
  13. {
  14.     char path[256];  //Dichiaro variabili
  15.     int *vet[100];
  16.     int riemp = 1;
  17.     int max;
  18.     int contatore = 0;
  19.  
  20.     carica_vet(path, vet[100], riemp);
  21.     stampa_vet(vet[100], riemp);
  22.     ricerca_max(vet[100], riemp, max, contatore);
  23.  
  24.  
  25.     return 0;
  26.  
  27. }
  28.  
  29.  
  30. void carica_vet(char path[], int vet[], int &riemp)
  31. {
  32.     //Inserimento da tastiera del percorso del file da aprire
  33.     fstream f;
  34.     cout <<"Inserire nome del file da aprire:" << endl;
  35.     cin >> path;
  36.  
  37.     // Apro il file
  38.  
  39.  
  40.     f.open(path, ios::in);
  41.     if (!f)                                                     // Controllo file
  42.     {
  43.         cout << "Errore nell'apertura del file" << endl;;
  44.         exit(-1);
  45.     }
  46.  
  47.  
  48.     //Inserisco elementi dal file nel vettore
  49.     f >> vet[0];
  50.  
  51.     while (!f.eof())              //Continua finché non trovi l'end of file
  52.         f >> vet[riemp++];        //Inserisci elementi nel vettore
  53.     riemp--;
  54.  
  55.  
  56.     f.close();             //Chiudo File
  57. }
  58.  
  59. void stampa_vet(int vet[], int &riemp)
  60. {
  61.     cout <<"Il vettore inserito e':" << endl;
  62.  
  63.     for(int i = 0; i < riemp; i++)
  64.     {
  65.         cout << vet[i] << endl;
  66.     }
  67.  
  68. }
  69.  
  70.  
  71. void ricerca_max(int vet[], int &riemp, int &max, int &contatore)
  72. {//Ricerca del Massimo
  73.  
  74.     max=vet[1];
  75.  
  76.     for(int i = 0; i < riemp; i++)
  77.     {
  78.         if (vet[i] > max)
  79.             max = vet[i];
  80.     }
  81.  
  82.  
  83.     //Calcolo ripetizione del massimo
  84.  
  85.     for (int i=0;i<riemp;i++)
  86.     {
  87.         if (vet[i] == max)
  88.         contatore++;
  89.     }
  90.  
  91.     // Stampa valore massimo trovato e della sua ripetizione
  92.     cout <<"Il valore massimo e':" << max << " ed e' ripetuto " <<contatore << "volte." << endl;
  93. }
Add Comment
Please, Sign In to add comment