Cosa sarà mai un vettore bidimensionale??? O.O…Bene..Una semplice matrice. ![]()
Prendendo spunto da questo, cosa che ovviamente “ancora” non so fare nè in python nè in altri linguaggi, ho deciso di creare un programmino simile, ma non con quelle funzionalità.
Questo semplicissimo programmino crea una matrice 10×10 riempiendola con valori da 0 a 99 che non si ripetono mai. Fatto ciò visualizza la matrice e stampa su terminale la somma dei valori di ogni colonna. Essi verranno analizzati e alla fine verrà stampato il massimo risultato tra tutti quelli di ogni colonna.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | /* grid.c Copyright 2010 Antonio Murdaca <tonicooperi13@tonicooperi.eu> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* Semplice programma che opera su una matrice 10x10 */ #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 int grid[ SIZE ][ SIZE ]; /* MATRICE 10x10 */ int row; /* RIGHE */ int column; /* COLONNE */ int checkNumber(int n) { /* VERICFICO CHE DEI VALORI NON SI RIPETANO */ int i, k; int check = 0; for(i=0;i<SIZE;i++) for(k =0;k<SIZE;k++) if(n == grid [i][k]) check = 1; return check; } void riempi() { /* RIEMPIO LA MATRICE CON VALORI CASUALI */ srand( time(NULL) ); int c, i; int contatore = 1; for (row=0;row<SIZE;row++) for (column=0;column<SIZE;column++,contatore<=99) { int check=0; do { c = rand() % 100; check = checkNumber(c); }while(check == 1 && contatore <= 99); grid[ row ][ column ] = c; contatore++; } for(i=0;i<100;i++) if(checkNumber(i) == 0) grid[ SIZE - 1 ][ SIZE - 1 ] = i; } int somma_e_max() { int max[SIZE]; int totcol = 0; int i, pass; int massimo; /* SOMMA DEGLI ELEMENTI DI OGNI COLONNA */ for(column=0;column<SIZE;column++){ for(row=0;row<SIZE;row++) totcol += grid[ row ][ column ]; max[ column ] = totcol; printf(" %2d ", totcol); totcol = 0; } printf("\n\n"); /* DETERMINO IL MASSIMO RISULTATO DELLE SOMME PRECEDENTI */ for(i=0;i<SIZE-1;i++){ if (max[ i ] > max[ i + 1 ]){ massimo = max[ i ]; max[ i + 1 ] = max[ i ]; massimo = max[ i + 1 ]; } } printf(" Il massimo risultato è %d\n\n", massimo); } int main() { riempi(); printf("\n"); for(row=0;row<SIZE;row++){ /* VISUALIZZO LA MATRICE */ for(column=0;column<SIZE;column++) printf("%4d ", grid[ row ][ column ]); printf("\n"); } printf("\n"); somma_e_max(); return EXIT_SUCCESS; } |
Questo è un possibile output:

..Difatti ho già iniziato a scrivere qualche piccolo programmino così, tanto per iniziare, e mi sto aiutando col libro















