Month: gennaio 2010

Vettori bidimensionali..alteresì conosciuti come Matrici

Posted by – 31 gennaio 2010

Cosa sarà mai un vettore bidimensionale??? O.O…Bene..Una semplice matrice. :D
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:


Python, PEP8 e repository su github.com

Posted by – 29 gennaio 2010

Oggi è un grande giorno!!! :D …Ebbene si, proprio oggi ho deciso di ampliare le mie conoscenze in fatto di programmazione. E ho deciso di implementare in me stesso le conoscenze riguardo Python. :) ..Difatti ho già iniziato a scrivere qualche piccolo programmino così, tanto per iniziare, e mi sto aiutando col libro “Dive into Python 3″, che a dir la verità è fatto davvero bene. Almeno per chi ha qualche “basettina” di programmazione.
In contemporanea, mentre girovagavo nel sito di Python, ho scoperto la PEP8. La PEP8 è una specifica di Python che indica come si dovrebbe scrivere il codice (definisce come deve essere l’ indentazione ecc.). E fu così che trovai questo utilissimo script: http://github.com/jcrocholl/pep8/raw/master/pep8.py. Questo programma verifica se il codice scritto è conforme alla PEP8. Per usarlo digitate da terminale python pep8.py –filename-patterns codicedaverificare.py..et voilà!..Scopriremo all’ istante se abbiamo effettivamente scritto codice python in maniera adeguata!
Un’ altra fantastica notizia riguarda sempre me. :D Mi sono registrato su github.com. Potete trovare tutti i programmi che scriverò d’ ora in avanti qui: http://github.com/tonicooperi.
Per oggi è tutto. :)
A presto!

Processi e sostituzione del programma in esecuzione

Posted by – 28 gennaio 2010

Un semplicissimo esempio riguardo l’ uso delle più comuni funzioni per gestire i processi, ovviamente solo sotto Linux: pid_t fork( );, pid_t getpid( );, pid_t waitpid(pid_t pid, int *status, int options);, void exit(int);.
I due programmi sono strettamente correlati tra loro in quanto il primo, richiamando la funzione int execl (char *nome_programma, char *arg0, char *arg1, …NULL );, sostituisce la sua esecuzione con quella del secondo programma passandogli 4 parametri. Il secondo programma, il broswer Icecat (potete scegliere quale volete), aprirà 3 pagine web. :)
Che divertimento. :D

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
/*
 
 pidexecl.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 esempio sulla creazione di nuovi processi e un pò della
   loro gestione. Inoltre è presente l' uso della funzione
   int execl (char *nome_programma, char *arg0, char *arg1,...NULL );.
   che sostituisce il primo programma con un altro.
   Il broswer Icecat /usr/bin/icecat (sostituibile con uno qualsiasi)
   aprirà 3 pagine web in 3 schede.
   I numeri di printf (Es. "2.") aiutano a capire l' ordine "cronologico"
   dell' esecuzione delle funzioni sui processi. */
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
 
int main(){
 
pid_t pid, pid1, pid2;
int status, status1;
 
char p1[]="";
char p2[]="tonicooperi.eu";
char p3[]="google.it";
char p4[]="google.it";
 
pid=fork();
 
	if (pid==0){
		pid=getpid();
		printf("3.Sono il primo processo figlio con PID=%d\n", pid);
		pid2=fork();
			if(pid2==0){
				pid2=getpid();
				printf("5.Sono il primo processo generato da PID=%d"
							" prima di morire,\n"
							"6.il mio PID invece è"
							" %d\n",pid,pid2);
				exit(26);
			}
		pid=waitpid(pid2,&status1,0);
		printf("7.Il processo %d è morto con Stato=%d\n",pid2,status1/256);
		exit(13);
	} else {
		pid1=getpid();
		printf("1.Sono il processo padre con PID=%d,\n"
		"2.ho creato il primo processo figlio con PID=%d\n", pid1, pid);
		pid1=waitpid(pid,&status,0); 
		printf("4.Il primo processo figlio PID=%d è morto con Stato=%d\n",
			 pid, status/256);
		}
 
		printf("\n8.PID=%d passa 4 parametri, e poi muore, con execl() "
			"al programma /usr/bin/icecat\n", getpid());
		execl("/usr/bin/icecat", p1, p2, p3, p4, NULL);
		exit(1);
 
return EXIT_SUCCESS;
 
}

Ancora sulle Liste Dinamiche :)

Posted by – 28 gennaio 2010

Questo è un qualcosa di più automatizzato dell’ esempio dell’ altra volta. :D Editando il codice sorgente decidete voi cosa inserire e cosa cancellare.

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 
 listedinamiche.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/>.
 
*/
 
/* Ancora sulle liste dinamiche :)
   Compilare con "gcc listedinamiche.c -o liste.bin -D DEBUG"
   Eseguite "./liste.bin -D DEBUG" */
 
#include <stdio.h>
#include <stdlib.h>
 
#define SIZE 100
char debug[SIZE];
 
#ifdef DEBUG
#define term( debug, startPtr ) { printf("%s ", debug); show( startPtr ); }
#else
#define term( debug, startPtr )
#endif
 
typedef struct Node{
	int element;
	struct Node *NextNode;
	} list;
/* Ogni list node contiene questa struttura */
 
typedef list *listPtr;  /* Sinonimo di list */
listPtr startPtr=NULL; /* Inizzializzo la lista, all' inizio non ci sono nodi */
int items = 0; /* contatore degli elementi presenti */
 
/* FUNZIONI */ 
void show (listPtr current){
	if (current == NULL ){              /* Se la lista è vuota.. */
		printf("List is empty!\n");
	}
	else{
		printf("List is: ");
			while (current != NULL ){
				printf( "[%d]",  current->element );
				current=current->NextNode;
			}
		printf("\n");
	}
}
 
void insert (listPtr *sPtr, int value){
 
	listPtr new;      /* Puntatore al nuovo nodo */
	listPtr previous; /* Puntatore al nodo precedente nella lista */
	listPtr current;  /* Puntatore al nodo corrente della lista */
 
	new=malloc(sizeof(struct Node)); /* Crea il nuovo nodo allocando la memoria */
 
	if (new != NULL ){                 /* Se la memoria è disponibile... */
		new->element=value;         /* Memorizzo l' elemento */
		new->NextNode=NULL;      /* Il nodo non è collegato ad un altro nodo */
		previous=NULL;
		current=*sPtr;
 
		/* Ciclo per trovare la posizione corretta */
		while ( current != NULL && value > current->element ){
			previous=current;
			current=current->NextNode;
			}
		/* inserisce il nuovo nodo all' inizio della lista */
		if (previous==NULL){
			new->NextNode=*sPtr;
			*sPtr= new;
		}
		else{ /* inserisce il nuovo nodo tra previous e current */
			previous->NextNode=new;
			new->NextNode=current;
		}
		items++;
		snprintf(debug,SIZE,"Insert element %d, current elements %d"
				    " + [NULL]\n", value, items);
		term(debug, startPtr);
	}
	else{
		snprintf(debug,SIZE,"Error!");
		term(debug, startPtr);
	}
}
 
int delete (listPtr *sPtr , int value ){
 
	listPtr previous; 
	listPtr current; 
	listPtr temp;         /* Puntatore ad un nodo temporaneo */
	/* Elimino il primo nodo */
	if ( value==(*sPtr)->element ){
		temp=*sPtr; /* Memorizzo nodo che sta per essere rimosso */
		*sPtr=(*sPtr)->NextNode; /* "sfilo il nodo" */
		free(temp); /* Dealloco la memoria usata dal nodo "sfilato" */
		items--;
		snprintf(debug,SIZE,"Deleted element %d, current elements %d"
				    " + [NULL]\n", value, items);
		term(debug, startPtr);
	}
	else{
		previous=*sPtr;
		current=(*sPtr)->NextNode;
		/* Esegue un ciclo per trovare la posizione corretta nella lista */
		while (current != NULL && current->element != value){
			previous=current;
			current=current->NextNode;
		}
		/* Elimino il nodo puntato da current */
		if (current != NULL ){
			temp=current;
			previous->NextNode=current->NextNode;
			free(temp);
			items--;
			snprintf(debug,SIZE,"Deleted element %d, current elements %d"
					    " + [NULL]\n", value, items);
			term(debug, startPtr);
		}
	}
}
 
int main(){
 
int i;
 
/* Operazioni sulla lista */
 
show(startPtr);
insert(&startPtr, 1);
insert(&startPtr, 2);
insert(&startPtr, 13);
delete(&startPtr, 1);
insert(&startPtr, 26);
insert(&startPtr, 99);
delete(&startPtr, 26);
delete(&startPtr, 2);
delete(&startPtr, 99);
 
for (i=0;i<3;i++)
	insert(&startPtr, i * 3);
 
return EXIT_SUCCESS;
}

Perchè dovreste passare a Linux

Posted by – 28 gennaio 2010

Girovagando trovai questa pagina http://www.istitutomajorana.it/passare-linux/index5.html.
Ebbene, l’ articolo presente in essa spiega che ci sono ALMENO (e sottolineo almeno) 27 validi motivi per passare, finalmente, a Linux.
Leggetelo e convincetevi anche voi che passare a Linux è la miglior cosa da fare. Resterete certamente soddisfatti.


Switch to our mobile site