Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************
- Nome: Alessio Sferro
- Titolo: StrPrefisso
- Data: 28/11/2015
- *************************************/
- /*
- Esercizio 1:
- Data una matrice di stringhe dire se esiste una colonna in cui siano presenti
- due stringhe con lo stesso prefisso di lunghezza 3.
- */
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int DIM = 3;
- bool checkMatrix(string A[][DIM]);
- int main(){
- srand(time(NULL));
- string matStr[DIM][DIM];
- int x = 0, j = 0, y, k;
- cout << "Matrice A:" << endl;
- while(x < DIM){
- y = 0;
- while(y < DIM){
- j = 0;
- k = rand()%7+4;
- while(j < k){
- matStr[x][y] += char(rand()%2+97);
- j++;
- }
- cout << "A[" << x << "][" << y << "] = " << matStr[x][y] << endl;
- y++;
- }
- x++;
- }
- if(checkMatrix(matStr))
- cout << "\nSUCCESS: stringhe PRESENTI!" << endl;
- else
- cout << "\nFAIL: stringhe non presenti..." << endl;
- cout << "\nPremi INVIO per continuare...";
- cin.get();
- return 0;
- }
- bool checkMatrix(string A[][DIM]){
- int y = 0, x, k;
- while(y < DIM){
- x = 0;
- while(x < DIM){
- k = x+1;
- while(k < DIM){
- if(A[x][y].substr(0, 3) == A[k][y].substr(0, 3)){
- cout << "\nIndice della colonna in cui sono presenti le stringhe: " << y << endl;
- return true;
- }else k++;
- }
- x++;
- }
- y++;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement