Skip to content

Instantly share code, notes, and snippets.

@simonespa
Created August 10, 2020 17:40
Show Gist options
  • Select an option

  • Save simonespa/c787fe420d1b1783954e5770be5cab84 to your computer and use it in GitHub Desktop.

Select an option

Save simonespa/c787fe420d1b1783954e5770be5cab84 to your computer and use it in GitHub Desktop.
Merge C++
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
void readGroups(char** g1, char** g2, int& n, int& m) {
cout << "Numero di parole: ";
cin >> n;
cout << "Numero di caratteri: ";
cin >> m;
g1 = new char*[n];
cout << "Inserisci " << n << " parole del primo gruppo: " << endl;
for (int i = 0; i < n; i++) {
g1[i] = new char[m];
cin >> g1[i];
}
g2 = new char*[n];
cout << "Inserisci " << n << " parole del secondo gruppo: " << endl;
for (int i = 0; i < n; i++) {
g2[i] = new char[m];
cin >> g2[i];
}
}
void findMatch(char** g1, char** g2, char** schema, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g1[i][strlen(g1[i]) - 1] == g2[j][1]) {
cout << g1[i] << endl;
cout << g2[j] << endl;
}
}
}
}
char** getSchema(int n, int m) {
static char** schema = new char*[n];
for (int i = 0; i < n; i++) {
schema[i] = new char[m];
}
return schema;
}
void clean(char** g1, char** g2, char** schema, int n) {
for (int i = 0; i < n; i++) {
delete[] g1[i];
delete[] g2[i];
delete[] schema[i];
}
delete[] g1;
delete[] g2;
delete[] schema;
}
int main(int argc, char** argv) {
char** g1;
char** g2;
int n, m;
readGroups(g1, g2, n, m);
char** schema = getSchema(n, m);
findMatch(g1, g2, schema, n, m);
clean(g1, g2, schema, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment