This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c4151b
commit 274d8dd
Showing
5 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Matrici | ||
=> tabella n-dimensionale ordinata di elementi | ||
|
||
Utilizzeremo principalmente matrici bdimensionali | ||
|
||
## Dimensione | ||
Una dimensione NxM è da intendersi come una matrice di N righe ed M colonne | ||
|
||
## Sintassi - matrice bdimensionale | ||
``` | ||
[const] <tipo_dato> <nome_matrice> [<numero_righe>][<numero_colonne>]; | ||
``` | ||
|
||
## Riempimento matrice | ||
Metodo 1: riempire riga per riga. All'interno di ogni riga riempio colonna per colonna. Utilizzo 2 cicli for innestati. | ||
Meotodo 2: un solo ciclo for che fa uso dell'operatore modulo per determinare su quale riga/colonna si trova (oppure accedo come se fosse un array, con le righe allocate in memoria in modo contiguo) | ||
|
||
## Matrice k-dimensionale | ||
``` | ||
[const] <tipo_dato> <nome_matrice> [<dim1>] [<dim2>] {[dimk]}; | ||
``` | ||
|
||
## Inizializzazione matrici | ||
È possibile inizializzare le matrici come se fossero un array di più array. | ||
|
||
Nella dichiarazione + inizializzazione di una matrice non è necessario specificare il numero di righe, perchè tale valore viene dedotto dal compilatore | ||
|
||
``` | ||
int mat[][2] = | ||
{ | ||
{1, 2}, | ||
{3, 4} | ||
} | ||
``` | ||
|
||
## Diagonanale | ||
Matrici quadrate | ||
|
||
### DIAGONALE PRINCIPALE | ||
Indici: i, i => mi trovo sulla diagonale principale se i == i | ||
|
||
### DIAGONALE SECONDARIA | ||
Indici: i, N-i-1 => mi trovo sulla diagonale secondaria se i == N-i-1 | ||
|
||
Ho bisogno di un solo ciclo per accedere alla diagonale | ||
|
||
## Raw major e column major | ||
Il C è un linguaggio raw major, perchè memorizza in memoria le matrici come array contigui, ognuno dei quali rappresenta una riga | ||
Linguaggi come il Fortran memorizza le colonne in modo sequenziale | ||
|
||
## Memoria | ||
In memoria le matrici sono rappresentate come righe contigue | ||
|
||
## Passaggio a funzione | ||
Equivalente al passaggio di array | ||
=> sempre passate per riferimento | ||
|
||
Sintassi: | ||
``` | ||
[cont] <tipo_elementi> <identificatore> [] [<numero_colonne>] | ||
``` | ||
|
||
Infatti: quando passo un array ad una funzione non esplicito il numero di elementi presenti nell'array | ||
|
||
Per efettuare l'acccesso in memoria basta conoscere l'indirizzo della cella di memoria dell'elemento richiesto | ||
|
||
Indice per l'elemento \[indice_riga][indice_colonna] è calcolato come segue: | ||
indirizzo_matrice + byte_tipo_dato_elementi \* (indice_riga\*num_colonne + indice_colonna) | ||
|
||
Vettorizzazione o linearizzazione di una matrice | ||
|
||
## Array di strighe | ||
Posso memorizzare un array di stringhe. Su ogni riga si trova una array di caratteri terminati di '\0', tutto lo spazio dopo il terminatore è purtroppo sprecato. | ||
|
||
// TODO: traccia_disegno.cpp | ||
// TODO: battaglia navale | ||
// TODO: implementa il gioco della vita | ||
// TODO: traccia_lista_esami.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int calcola_somma(int riga[], int dim_riga) { | ||
int count = 0; | ||
|
||
for (int i=0; i<dim_riga; i++) { | ||
count += riga[i]; | ||
} | ||
|
||
return count; | ||
} | ||
|
||
int main() { | ||
const int dim_r = 3, dim_c = 3; | ||
int M[dim_r][dim_c]; | ||
|
||
for (int i=0; i<dim_r; i++) { | ||
for (int j=0; j<dim_c; j++) { | ||
cout << "Elemento ["<<i<<"]["<<j<<"]: "; | ||
cin >> M[i][j]; | ||
} | ||
} | ||
|
||
|
||
for (int i=0; i<dim_r; i++) { | ||
for (int j=0; j<dim_c; j++) { | ||
cout << M[i][j] << " "; | ||
} | ||
cout << " " << calcola_somma(M[i], dim_c); // Passo un vettore riga e il numero di elementi presenti in esso | ||
cout << endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* file: contenitore_no_struct.cpp | ||
* author: francesco mecatti | ||
* | ||
* Seconda simulazione prova programmazione 1 | ||
* | ||
*/ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
const char MIN_CHAR = 'a'; | ||
const char MAX_CHAR = 'z'; | ||
|
||
|
||
const int DIM = int(MAX_CHAR)-int(MIN_CHAR)+1; | ||
|
||
// struttura dati | ||
// cout << int(MAX_CHAR) - int(MIN_CHAR) + 1; | ||
struct contenitore_t { | ||
int dim; | ||
int capienza; // numero totale di elementi | ||
int dimensione_attuale; | ||
int array[DIM]; | ||
}; | ||
|
||
enum errore {OK = 0, ELE_NOT_VALID, NOT_ENOUGH_SPACE, TOO_SMALL_CAP, NUM_ERR}; | ||
const char error_msgs[NUM_ERR][50] = {"", "Elemento non valido", "Spazio finito", "Capienza troppo piccola"}; | ||
|
||
/* | ||
* stampa l'error message ed esce con errore | ||
* @param msg: string, esce con errore | ||
*/ | ||
void gestisci_errore(errore e) { | ||
cerr << error_msgs[e] << endl; | ||
// exit(1); | ||
} | ||
|
||
/* | ||
* inizializza tutti i contatori a 0 e ritorna | ||
* @param cont: alias al contenitore che si vuole inizializzare | ||
* @return: void | ||
*/ | ||
void inizializza(contenitore_t &cont) { | ||
for (int i=0; i<cont.dim; i++) | ||
cont.array[i] = 0; | ||
} | ||
|
||
/* | ||
* incrementa il contatore di v[ele] e aggiorna il numero di elementi | ||
* @param ele: elemento da inserire | ||
* @param cont: struttura contenitore | ||
* @return: tipo di errore se occurred, OK altrimenti | ||
*/ | ||
errore inserisci(char ele, contenitore_t &cont) { | ||
if (ele<MIN_CHAR || ele>MAX_CHAR) | ||
return ELE_NOT_VALID; | ||
// cout << "valid" << endl; | ||
|
||
if (cont.dimensione_attuale >= cont.capienza) | ||
return NOT_ENOUGH_SPACE; | ||
// cout << "space" << endl; | ||
|
||
// cout << v[ele-MIN_CHAR] << endl; | ||
cont.array[ele-MIN_CHAR]++; | ||
cont.dimensione_attuale++; | ||
return OK; | ||
} // se esce senza errori ritorna 0 => OK | ||
|
||
errore estrai(char ele, contenitore_t &cont) { | ||
if (ele<MIN_CHAR || ele>MAX_CHAR) | ||
return ELE_NOT_VALID; | ||
|
||
cont.dimensione_attuale -= cont.array[ele-MIN_CHAR]; | ||
cont.array[ele-MIN_CHAR] = 0; | ||
return OK; | ||
} // se esce senza errori ritorna 0 => OK | ||
|
||
errore modifica_capienza(int nuova_capienza, contenitore_t &cont) { | ||
if (nuova_capienza < cont.dimensione_attuale) | ||
return TOO_SMALL_CAP; | ||
|
||
cont.capienza = nuova_capienza; | ||
return OK; | ||
} | ||
|
||
void stampa(contenitore_t &cont) { | ||
cout << "<"; | ||
for (int i=0; i<cont.dim; i++) { | ||
if (cont.array[i] != 0) | ||
cout << char(i+MIN_CHAR) << ":" << cont.array[i] << ", "; | ||
} | ||
cout << ">" << endl; | ||
} | ||
|
||
int main() { | ||
const char menu[] = | ||
"i - inserisci elemento\n" | ||
"e - estrai elementi\n" | ||
"m - modifica capienza\n" | ||
"c - stampa capienza\n" | ||
"q - stampa numero elementi\n" | ||
"s - stampa vettore\n" | ||
"t - termina programma\n"; | ||
|
||
errore err; | ||
contenitore_t contenitore = {DIM, 0, 0, {}}; | ||
|
||
// inizializzazione | ||
cout << "Capienza: "; cin >> contenitore.capienza; | ||
inizializza(contenitore); | ||
|
||
char ch; // scelta utente | ||
char ele; int aux; | ||
while (true) { | ||
cout << menu << endl; | ||
cout << ">> "; cin >> ch; | ||
switch (ch) { | ||
case 'i': | ||
cout << "Nome elemento: "; cin >> ele; | ||
err = inserisci(ele, contenitore); | ||
if (err != OK) { | ||
// cout << err << endl; | ||
gestisci_errore(err); | ||
} | ||
break; | ||
case 'e': | ||
cout << "Nome elemento: "; cin >> ele; | ||
err = estrai(ele, contenitore); | ||
if (err != OK) | ||
gestisci_errore(err); | ||
break; | ||
case 'm': | ||
cout << "Nuova capienza: "; cin >> aux; | ||
err = modifica_capienza(aux, contenitore); | ||
if (err != OK) | ||
gestisci_errore(err); | ||
break; | ||
case 'c': | ||
cout << "Capienza: " << contenitore.capienza << endl; | ||
break; | ||
case 'q': | ||
cout << "Quanti elementi sono nel vettore? " << contenitore.dimensione_attuale << endl; | ||
break; | ||
case 's': | ||
stampa(contenitore); | ||
break; | ||
case 't': | ||
return 0; | ||
break; | ||
default: | ||
cout << "Scelta errata" << endl; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Scrivere un programma che definisca una mat bdimensionale di interi | ||
(dim stabilite a tempo di scrittura) ed assegni ad ogni elemento un | ||
valore letto da stdin | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
const int dimX = 3, dimY = 2; | ||
int M[dimY][dimX]; | ||
for (int i=0; i<dimY; i++) { | ||
for (int j=0; j<dimX; j++) { | ||
cout << "Inserisci l'elemento ["<<i<<"]["<<j<<"]: "; | ||
cin >> M[i][j]; | ||
} | ||
} | ||
|
||
for (int i=0; i<dimY; i++) { | ||
for (int j=0; j<dimX; j++) { | ||
cout << M[i][j] << " "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Scrivere un programma che definisca una mat bdimensionale di interi, la | ||
inizializzi e conti il numero di valori positivi, negativi e nulli | ||
*/ | ||
|
||
#include <iostream> | ||
#include <ctime> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
const int dim_r= 100, dim_c = 100; | ||
int M[dim_r][dim_c]; | ||
|
||
srand(time(0)); | ||
|
||
// Inizializzazione | ||
for (int i=0; i<dim_r; i++) { | ||
for (int j=0; j<dim_c; j++) { | ||
M[i][j] = (rand()%3) - 1; | ||
} | ||
} | ||
|
||
int pos, neg; | ||
pos = neg = 0; | ||
|
||
for (int i=0; i<dim_r; i++) { | ||
for (int j=0; j<dim_c; j++) { | ||
if (M[i][j] > 0) | ||
pos++; | ||
else if (M[i][j] < 0) | ||
neg++; | ||
} | ||
} | ||
|
||
cout | ||
<< "Pos: " << pos << endl | ||
<< "Neg: " << neg << endl | ||
<< "Nulli: " << dim_r*dim_c-(pos+neg) << endl; | ||
|
||
return 0; | ||
} |