Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
vuoi
o PayPal
tutte le volte che vuoi
inizio : int main(int argc, char *argv[]) {
- dichiarazione variabili
- Acquisizione dati
- elaborazione
- visualità nome dati
{ INDENTAZIONE
- commento /* */ ( // no xk BO)
- variabili float var; contr.l.nume
- float v, m;
- int
- float
- double (doppia pressione)
- char → ASCII
- acquisizione dati tastiera
- .indent → LINUX
tipo var, nome vari dove lo mem.
scanf("%lg%lg", &dvar, &fc);%f/%lg ul leve float/double
%d int
%c char
- Stampa dati
- printf("...", val);
- fine return(0);
- o esempio procedimento
- #include <stdio.h>
- #define GAL_LIT 3.7854
int main(int argc, char *argv[]) {
- float P_D_G, tasso, P_E_L;
- scanf("%f", &P_D_G);
- scanf("%f", &tasso);
- P_E_L = P_D_G * tasso / GAL_LIT;
- printf("...", P_E_L);
- return(0);
- Numeri → Media
- #define N 3.0;
int main(int argc, char *argv[]) {
- float a,b,c,ris;
- scanf("%f %f %f", &a, &b, &c);
- ris = (a+b+c)/N;
- printf("...", ris);
- return(0);
- CASTING → x vedere 1 tipo di variabile come voglio
- media = (float) hack / font
- IF if(condizione) {
- condizione vera 1 sola istruzione
- }
- else {}
- Per dividere programma in funzioni, faccio 1 funzione che uso 1 volte x semplificare codice.
- Interrotta sequenza istruzioni e torna dove ho chiamato funzione
- Se non c'è tipo mette in automatico int
- Programma:
- Include
- Define — Dichiarazione funzioni (tipo nome (int, int) sotto nome var.
- Typedef
- Main
- Sottoprogrammi
- Var: Attuale: nel main Forzante: nella funzione
- Ex sottoprogramma: int —> fattoriale
- int Fattoriale (int);
- int Fattoriale(int m) {
- int f = 1;
- for(i = m; i > 0; i = i - 1)
- f = f * i;
- return f; }
- Controllo m < 0 ? if else...
- Ex: Dato int e passato altro parametro prettamente massimo, torna indice elemento con val max, se è doppio, il primo
- int IndiceMax (int[], e);
- int Indice2 (int v[], e){
- int i, max, j;
- for (indice i = 1, max = v[0]; j < e; j = j + 1)
- if(v[ne x] v[j])
- max = i;
- return ind; }
- malloc(size_t)
- void *malloc(size_t)
- SOTTOPROGRAMMA RICORSIVO: RICHIAMA SE STESSO NEL SOTTOPROGRAMMA
- OGNI VOLTA CHE SI RICHIAMA OCCUPA PIÙ SPAZIO NELLO STACK, PERCHÉ: CREO SEMPRE 1 NUOVA ISTANZA, E NON DEVO FINIRE MALE.
- DICHIARARE ARRAY NON VELO A priori
- DaOPO POsso ALLOCARE
- f(t.e=(tipo*)malloc(sizeof(tipo)*quant))
- QUANDO NON SO QUANTI ELEMENTI INSERIRE
- DOPPIO PUNTATORE QUANDO DEVO MODIFICARE LA TESTA (PARAMETRO) o LISTA.
Calcola fattoriale
int main(int argc, char *argv[]) {
int m, m, p;
m=atoi(argv[1]);
while (m > 1) {
p *= f * (m-1);
m--;
printf("%d \n", p);
return 0;
}
M in binario altro
int main(int argc, char *argv[]) {
int m, k, b, mt, p;
do scanf("%d", &lm); while (m <= 0);
k = m;
pot = 1;
while (k > 0) {
b = k % 2;
mt = b + pot * mt;
k /= 2;
pot *= 10;
}
printf("%d", m, mt);
return 0;
}
Tabula pitagorica
int main(int argc, char *argv[]) {
int m, v, c, r;
do scanf("%d", &m) while (m <= 0);
v = 2;
while (v <= m) {
c = 2;
while (c <= m) {
printf("%d", r*c);
}
r++;
printf("\n");
return 0;
}
Stringa Max 20 al contrario
#define N 20
int main(int argc, char *argv[]) {
char Stringa[N];
scanf("%s", Stringa);
for(i = 0; i <= 1; i++) {
for(j = i; j >= 0; j--) {
echo;
printf("%c", Stringa[j]);
}
}
f(x) = a0 + a1x + a2x^2 + ... amx^m n < 10
#define min 10
#define N 10
int main(int argc, char *argv[]) {
int m, i, j;
float poli[N], vin, x, im;
do scanf("%d", &m), while(m < min || m > N);
for(i = 0; i <= m; i++) {
do scanf("%f", &poli[i]);
} scanf("%f", &x), vin = 0;
for(i = 0; i <= m; i++) {
im = 1;
for(j = 0; j < i; j++) {
im = im * x;
vin = vin + poli[i] * im;
}
printf("%f", vin);
echo;
}
N giudici k concorrenti Voti [0, 10] N, k < 10
#define min 1
#define max 10
int main(int argc, char *argv[]) {
float mat[max][max], M, V;
int sum, k, i, j, voti, non am;
do scanf("%d", &k), while(k < min || k > max);
do scanf("%d", &voti), while(voti > min || voti > max);
for(i = 0; i < j; i++) {
for(j = 0; j <= 1; j++) {
do
printf("inserire voti a candidato %d", i);
scanf("%f", &mat[i][j]);
}
}
V = 0;
M = 0;
Sottoprogrammi/Funzioni
tipo nome(tipo var, tipo var...){
Memory Alloc
Restituisce indica (in generale) a qualunque tipo.
void free(void *) x liberare memoria
sizeof(tipo) ⟶ torna memoria occupata dal tipo (int, char, float...)
int main(int argc, char *argv[]) {
int m, j;
int *v;
scanf("%d", &m);
v = (int *) malloc(m * sizeof(int));
if(v != NULL) {
for(i = 0; i < N; i++)
scanf("%d", (v + i));
for(--j; j >= 0; --j)
printf("%d", *(v + j));
free(v);
} else
printf("Errore allocamento di memoria");
return 0;
}
RICORSIONE
METODO ALTERNATIVO A METODO ITERATIVO
ALLOCAZIONE DINAMICA
1. MALLOC (stc t.e)
2. sizeof(t.po)
3. Free( void * )
LISTE
typedef struct lista_s {
tipo dato; ex int dato;
struct lista_s *next; } lista_t;
lista_t* InsTesta(lista_t*h,int m) {
lista_t *t;
if(t=(lista_t*)malloc(sizeof(lista_t))) {
t->dato=m;
t->next=h;
return t;
}
printf("errore allocazione");
return h;
}
lista_t* InsInCoda(lista_t*h,int m) {
lista_t *t,*p;
if(t=(lista_t*)malloc(sizeof(lista_t))) {
t->dato=m;
t->next=null;
if(h){
for(p=h;p->next!=null;p=p->next);
p->next=t;
}
else { h=t; }
}
else { printf("errore allocazione"); }
return h;
}