Sei sulla pagina 1di 6

ALLOCAZIONE DINAMICA DI UNA MATRICE DI STRINGHE

char**matrice = malloc(m * sizeof(char*));

for(int i = 0; i < m; i++){


matrice[i] = malloc(n * sizeof(char));

//controllo malloc
if(!matrice){
printf("errore nella creazione della matrice");
exit(EXIT_FAILURE);
}

/////////////////////////////////////////////////////////

APERTURA E CONTROLLI

if(argc < 2){


printf("parametri non specificati correttamente");
return 1;
}

FILE *fp = fopen(argv[1], "r");

if(!fp){
puts("file non esistente o nome non corretto");
return 1;
}

////////////////////////////////////////////////

ALLOCARE UNA MATRICE DI INTERI (FUNZIONE)

int **create_matrix(int m, int n, int fill_value){

int **matrix;

matrix = malloc (sizeof(int *) * m);

for(int i = 0; i < m; i++){


matrix[i] = malloc(sizeof(int) * n);
}

for(int i = 0; i < m; i++)


for(int j = 0; j < n; j++)
matrix[i][j] = fill_value;

return matrix;
}

nel main -->

int **matrix;
matrix = create_matrix(m, n, 4);

for( int i = 0; i < m; i++)


free(matrix[i]);

free(matrix);
\\

/////////////////////////////////////////////////////////////

CALCOLA IL QUADRATO DI TUTTI I NUMERI PRIMA DEL NUMERO INSERITO:

while (fgets(line, sizeof(line), fp)) {

i = 0;
token = strtok(line, " ");

while (token != NULL) {

matrix[i][y] = strtol(token, &end, 10);


y++;
token = strtok(NULL, " ");

if (y == columns) {
i++;
y = 0;
}

}
int numero;

puts("inserisci un numero:");
scanf("%d", &numero);

if(numero < 0){


puts("inserisci un numero valido");
return 1;
}

for(int i = 1; i <= numero; i++){


printf("quadrato di %d: %d\n", i, i*i);
}

/////////////////////////////////////////////////////////

GENERA NUMERI CASUALI

void generaCasuali(int *vett, int n){

int numero;
for(int i = 0; i < n; i++){
numero = rand()%n;
vett[i] = numero;
}
}

////////////////////////////////////////////////////

CALCOLA LA SOMMA DEGLI ELEMENTI DI UN VETTORE

int somma(const int *vett, int n){

int somma = 0;

for(int i = 0; i < n; i++){


somma += vett[i];
}
return somma;
}

///////////////////////////////////////////////////

CALCOLA IL MASSIMO TRA GLI ELEMENTI DI UN ARRAY

int max(const int *vett, int n){

int massimo = vett[0];


for(int i = 0; i <n; i++){
if(massimo < vett[i])
massimo = vett[i];
}

return massimo;
}

////////////////////////////////////////////////////

CALCOLA IL MINIMO TRA GLI ELEMENTI DI UN ARRAY

int minimo(const int *vett, int n){

int minimo = vett[0];


for(int i = 0; i < n; i++){
if(minimo > vett[i])
minimo = vett[i];
}

return minimo;
}

//////////////////////////////////////////////////

LEGGE DA FILE RIGA PER RIGA E TOKENIZZA LE RIGHE

char line[70];
while(fgets(line, sizeof(line), fp)) {
char *token;
token = strtok(line, " ");
variabile = strtol(token, &ptr, 10);

////////////////////////////////////////////////

CALCOLO DELLA MEDIA DI NUMERI PRESI DA UN CAMPO DI UNA MATRICE

y = 2;
for (i = 0; i < count_righe; i++) {

if (studenti[i][y].voto > 17) {


media += studenti[i][y].voto;
promossi++;

}
media = media / promossi;
printf("voto medio: %.2f\n", media);
}

///////////////////////////////////////////////////

STABILISCE SE UN NUMERO E' PRIMO O MENO

for (i = 2; i <= n/2; i++) {


if (n % i == 0) {
flag = 1;
break;
}
}

if (n == 1) {
printf("1 non è un numero primo.\n");
}
else {
if (flag == 0)
printf("%d è un numero primo.\n", n);
else
printf("%d non è un numero primo.\n", n);
}

//////////////////////////////////////////////////////

ALLOCAZIONE DINAMI DI UNA STRUTTURA

struct course *ptr;


int noOfRecords;
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);

// Memory allocation for noOfRecords structures


ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));
for (int i = 0; i < noOfRecords; ++i) {
printf("Enter subject and marks:\n");
scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks);
}

printf("Displaying Information:\n");
for (int i = 0; i < noOfRecords; ++i) {
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
}

/////////////////////////////////////////////////////

COUNT OCCORRENZE

int array[] = {1 ,5 ,6 ,4, 4 ,5 ,6 ,7 ,8 ,9,5};

int count_occorrenze = 0; //variabile che conta le occorrenze


int flagTrovato = 0;

int dimensione = 0;

int vett[10];
int i = 0, y = 0;

for(i = 0; i < 10; i++){ //cicla sul primo array

flagTrovato = 0;

for(y = 0; y < dimensione; y++){ //cicla su vett


if(array[i] == vett[y]){ //se è vero abbiamo trovato l'elemento
flagTrovato = 1; //vuol dire che c'è e non serve
}
}

if(flagTrovato == 0){
vett[dimensione] = array[i];
dimensione++;
}

for(int j = 0; j < dimensione; j++){


for(int k = 0; k < 10; k++) {
if (vett[j] == array[k]) {
count_occorrenze++;
}
}

printf("%d:%d ", vett[j], count_occorrenze);


count_occorrenze = 0;
}
/////////////////////////////////////////////

Potrebbero piacerti anche