Guest User

Untitled

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