|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
[C]Array multidimensionali, comparazioni di matrici
Ciao a tutti, sto facendo questo programma che prese in input due matrix a e b, il programma in output restituisce se la matrice b è una sottomatrice di a...
Purtroppo, non sono ancora riuscito ad arrivare a questo punto, infatti ho problemi col caricamento dei dati nel vettore... E non capisco cosa non vada... Infatti, i dati vengo caricati correttamente ( penso ) ma se metto una matrice 4 x 4 i dati inseriti vengono disposti su un'unica riga del terminale ( si noti che 4 è la grandezza massima di righe per la matrice, ho definito una costante apposta per questo ). Se vado invece a mettere una matrice 2 x 2 e scrivo, per esempio: as df Questi mi vengono stampati correttamente... Scrivendo invece, nella 4 x 4: qwer tyui asdf ghjk Mi viene stampato: qwertyuiasdfghjk Questo è il codice fin ora scritto: Codice:
/* Write a C program that reads from keyboard 2 matrices of characters (of size at most 10x10), and then checks if the second matrix occurs as a sub-matrix in the first one. For example, let us consider the matrices A B C k F m2: F G m3: F G m1: D E F G A K L K i m I K L i m2 occurs in m1 as a sub-matrix while m3 does not. Be careful in not crossing the matrix boundaries. */ #include <stdio.h> #define MAX 4 //Change to 10 when done void get_matrix(char matrix[][MAX], int *n1, int *n2); void empty_buffer(void); int main (void) { int i=0, j=0; //Indexes for matrix 1 int z=0, c=0; //Indexes for matrix 2 char matrix[MAX][MAX]; printf("This program read from keybord 2 matrices\n"); printf("of character, and checks\n"); printf("if the second matrix occurs\n"); printf("as a submatrix in the first one.\n"); for (i=0; i < MAX; i++) matrix[i][i]=0; i = 0; get_matrix(matrix, &i, &j); return 0; } void get_matrix(char matrix[][MAX], int *n1, int *n2) { int i1, i2; //Indexes for the matrix, no matter which one printf("Insert the size ( rows ) of the matrix\n"); scanf("%d", n1); while ( *n1 > MAX || *n1 <= 0 ) { printf("You can enter only a number between 1 and %d\n", MAX); scanf("%d", n1); } printf("Your matrix will have %d rows\n", *n1); printf("Now enter the size ( columns ) of the matrix\n"); scanf("%d", n2); while ( *n2 <= 0 ) { printf("Your matrix must have at least 1 column\n"); scanf("%d", n2); } printf("Your matrix will have %d columns\n", *n2); printf("Now enter your matrix, insert the characters for each row sequentially, without any space among them\n"); printf("The program will automatically position the elements inserted\n"); empty_buffer(); for (i1=0; i1 < *n1; i1++) { for(i2=0; i2 <= *n2; i2++) // i2 has to be less or equal to *n2 because the loop stops only if i2 == *n2, is this the reason?? { scanf("%c", &matrix[i1][i2]); } } printf("\n"); for (i1=0; i1 < *n1; i1++) { for(i2=0; i2 <= *n2; i2++) // Same as written above { printf("%c", matrix[i1][i2]); } } } void empty_buffer(void) { while(getchar() != '\n'); }
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:31.