FabioMurtas

esercizio stringhe

Apr 4th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. /*avendo 3 vettori di 10 posizioni
  2. registrare nome cognome e voto
  3. stampare il nome e cognome col voto più alto
  4. stampare il nome più corto
  5. usare le funzioni per scrivere il codice
  6. stampare il cognome più lungo*/
  7.  
  8. #include <iostream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. void caricaStringhe(string v[],int dim);
  14. void caricaVoto(int v[],int dim);
  15. void votoAlto (string v1[], string v2[],int v3[],int dim);
  16. void stampa (string v1[],string v2[],int dim);
  17.  
  18. int main(){
  19.      int dim=3 ;
  20.     string v1[dim],v2 [dim];
  21.     int v3[dim];
  22.     cout << "inserimento dei nomi" << endl;
  23.     caricaStringhe(v1, dim);
  24.     cout << "inserimento dei cognomi " << endl;
  25.     caricaStringhe(v2, dim);
  26.     cout << "inserimento dei voti" << endl;
  27.     caricaVoto(v3,dim);
  28.     votoAlto(v1,v2,v3,dim);
  29.     stampa(v1,v2,dim);
  30.     return 0;
  31. }
  32.  
  33. void caricaStringhe(string v[],int dim){
  34.      for(int i =0;i<dim;i++){
  35.          cout<<"inserisci la stringa  "<<i+1<<" nel vettore  ";
  36.          cin>>v[i];
  37.      }
  38. }
  39.  
  40. void caricaVoto(int v[],int dim){
  41.      for(int i =0;i<dim;i++){
  42.         cout<<"inserisci il voto "<<i+1<<"  nel vettore  ";
  43.         cin>>v[i];
  44.      }
  45. }
  46.  
  47.  
  48. void votoAlto(string v1[],string v2[],int v3[],int dim){
  49.      int max = v3[0];
  50.      string str1 , str2;
  51.      for(int i=0;i<dim;i++){
  52.          if (max < v3[i]){
  53.               max=v3[i];
  54.               str1=v1[i];
  55.               str2=v2[i];
  56.          }
  57.      }
  58.      cout<<str1<<" "<<str2<<" ha preso il voto più alto :"<<max <<endl;
  59. }
  60.  
  61. void stampa(string v1[],string v2[],int dim){
  62.      int min=0;
  63.      int max=0;
  64.      string str1 ,str2;
  65.      for(int i=0; i<dim; i++){
  66.          if(min > v1[i].length()){
  67.              min=v1[i].length();
  68.              str1= v1[i];
  69.          }
  70.          if(max < v2[i].length()){
  71.              max=v2[i].length();
  72.              str2= v2[i];
  73.          }
  74.       }
  75.       cout<<"il nome più corto è"<<str1<<endl;
  76.       cout<<"il nome più lungo è"<<str2<<endl;
  77. }
  78. /*non capisco perhce non mi da il nome più corto..*/
Add Comment
Please, Sign In to add comment