Incepat0r Posted March 13, 2014 Report Share Posted March 13, 2014 Salut!Am de generat o matrice de forma:n..........................zz n.......................yy z n....................xx y z n.................w...o......................z nsi pe moment nu am nici o idee despre cum arata algoritmul care permite o astel de generare. Quote Link to comment Share on other sites More sharing options...
bDyds Posted March 13, 2014 Report Share Posted March 13, 2014 Salut, se numeste algoritm de shiftare. Cauta pe google ca sigur gasesti. Quote Link to comment Share on other sites More sharing options...
totti93 Posted March 13, 2014 Report Share Posted March 13, 2014 (edited) #include <string.h>#include <stdio.h>#include <stdlib.h>char *shift(char *line, size_t n) { char *shifted = (char *) malloc(n * sizeof(char)); int i; shifted[0] = line[n - 1]; for (i = 0; i < n - 1; i++) shifted[i + 1] = line[i]; return shifted;}char **gen_matrice(char *seed, size_t n) { char **matrix = (char **) malloc(n * sizeof(char)); int i, j; for (i = 0; i < n; i++) matrix[i] = (char *) malloc(n * sizeof(char)); for (i = 0; i < n; i++) matrix[i] = seed; for (i = 0; i < n; i++) for (j = 0; j < i; j++) matrix[i] = shift(matrix[i], n); return matrix;}int main() { char line[] = "nopqrstuvwxyz"; int n = strlen(line); char **matrice = gen_matrice(line, n); int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) putchar(matrice[i][j]); putchar('\n'); } getchar(); return 0;}Nu e cea mai buna implementare. Nu mut valorile din tablou in alt tablou, ci doar mut pointerul pe o alta adresa. Memoria se elibereaza la `char line[]`, care e referentiat la prima linie a matricii, iar fiind alocat static, se va elibera zona de memoria la sfarsitul blocului (in cazul de sus e vorba de main(), deci e irelevant). Edited March 13, 2014 by totti93 Quote Link to comment Share on other sites More sharing options...
Incepat0r Posted March 13, 2014 Author Report Share Posted March 13, 2014 merci frumos @totti93 Quote Link to comment Share on other sites More sharing options...