Advertisement
Emanuele_Bruno

Esercizio 4

Dec 2nd, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. void array_b(string a[],int DIM)
  7. {
  8.     int i=0,h=0;
  9.     // verificare la lunghezza massima di stringhe in a
  10.     while (i<DIM)
  11.     {
  12.         int dim_a;
  13.         dim_a=a[i].length();
  14.         if (dim_a>h) h=dim_a;
  15.         i++;
  16.     }
  17.  
  18.     if (h<=2)
  19.     {
  20.         cout << "Spiacente! Valori maggiori di 2 non trovati...\n";
  21.         exit(0);
  22.     }
  23.     // cout << "La lunghezza massima di h trovata nell'array A e' : " << h << "\n"; // per debug
  24.     string b[h];
  25.     char car_a=('a');
  26.     i=1; // cominciamo dalla seconda riga perchΓ© b[0]='';
  27.     while (i<h)
  28.     {
  29.         b[i]=b[i-1]+car_a;
  30.         i++;
  31.     }
  32.     i=0;
  33.     while (i<h)
  34.     {
  35.         cout << "b["<< i << "]=" << b[i]<<"\n";
  36.         i++;
  37.     }
  38. }
  39.  
  40. int main()
  41. {
  42.     int DIM,i=0;
  43.     cout << "Indicare la grandezza dell'array A: ";
  44.     cin >> DIM;
  45.     string a[DIM];
  46.  
  47.     // inizializzo l'array di stringhe A
  48.  
  49.     while (i<DIM)
  50.     {
  51.         int k,j=0;
  52.         k=rand()%10+1;
  53.         while (j<k)
  54.         {
  55.             a[i]+=rand()%26+int(char('a'));
  56.             j++;
  57.         }
  58.         // cout << a[i] << endl; // per debug
  59.         i++;
  60.     }
  61.     array_b(a,DIM);
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement