Grupo I
Teoria
1.a) A definição de MULT3()
1.b) A chamada printf("%d\n", 8 / MULT1(1, 4))
imprime 32
.
1.c) A chamada printf("%d\n", MULT2(1+1, 2))
imprime
3
.
2.a) A chamada a f2()
deveria ser f2(b)
.
2.b) Imprime 1, 0
3. A chamada func(4)
imprime:
1 3 6 10
Grupo II
Programação - Listas
1.
typedef struct Lelemento { int valor; struct Lelemento *seguinte; } Lelemento; typedef struct { Lelemento *inicio; } Lista;
2.
Lista *Lcria(void) { Lista *l; if((l = malloc(sizeof(*l))) == NULL) return NULL; l->inicio = NULL; return l; }
3.
Lista *Linsere(Lista *l, int valor) { Lelemento *novo, *ptr; if(l == NULL) return NULL; if((novo = malloc(sizeof(*novo))) == NULL) return NULL; novo->valor = valor; if(l->inicio == NULL || l->inicio->valor >= valor) { novo->seguinte = l->inicio; l->inicio = novo; return l; } for(ptr = l->inicio; ptr->seguinte != NULL; ptr = ptr->seguinte) if(ptr->seguinte->valor > valor) break; novo->seguinte = ptr->seguinte; ptr->seguinte = novo; return l; }
4.
#include <stdio.h> #include <stdlib.h> typedef struct Lelemento ... /* ver acima */ typedef struct ... /* ver acima */ Lista *Lcria(void) ... /* ver acima */ Lista *Linsere(Lista *l, int valor) ... /* ver acima */ int main(void) { Lista *l; Lelemento *e; int valor; if((l = Lcria()) == NULL) return EXIT_FAILURE; while(scanf("%d", &valor) == 1 && valor >= 0) Linsere(l, valor); for(e = l->inicio; e != NULL; e = e->seguinte) printf("%d\n", e->valor); return EXIT_SUCCESS; }
Grupo III
Programação - Ficheiros binários
#include <stdio.h> #include <stdlib.h> int main(void) { int valor, valori, valorj, i, j, n; FILE *saida; if((saida = fopen("numeros.bin", "w+b")) == NULL) { fprintf(stderr, "Erro abrindo ficheiro!\n"); return EXIT_FAILURE; } for(n = 0; scanf("%d", &valor) == 1 && valor >= 0; n++) fwrite(&valor, sizeof(int), 1, saida); if(ferror(saida)) { fprintf(stderr, "Erro escrevendo no ficheiro!\n"); return EXIT_FAILURE; } for(i = 0; i < n; i++) { fseek(saida, i * sizeof(int), SEEK_SET); fread(&valori, sizeof(int), 1, saida); for(j = 0; j < n; j++) { fseek(saida, j * sizeof(int), SEEK_SET); fread(&valorj, sizeof(int), 1, saida); if(valori < valorj) { fseek(saida, j * sizeof(int), SEEK_SET); fwrite(&valori, sizeof(int), 1, saida); fseek(saida, i * sizeof(int), SEEK_SET); fwrite(&valorj, sizeof(int), 1, saida); valori = valorj; } } } if(ferror(saida)) { fprintf(stderr, "Erro ordenando!\n"); return EXIT_FAILURE; } fseek(saida, 0, SEEK_SET); while(fread(&valor, sizeof(int), 1, saida) == 1) printf("%d\n", valor); fclose(saida); return EXIT_SUCCESS; }
Grupo IV
Programação - Recursividade, funções e ciclos
1.
double fact(int n) { if(n <= 1) return 1.0; return n * fact(n-1); }
2.
double seno(double val, double tol) { double res, novo; int sinal, n; sinal = 1; n = 1; res = 0; do { res += novo = sinal * pow(val, n) / fact(n); sinal = -sinal; n += 2; } while(novo < -tol || novo > tol); return res; }
3.
#include <stdio.h> #include <stdlib.h> #include <math.h> double fact(int n) ... /* ver acima */ double seno(double val) ... /* ver acima */ int main(int argc, char **argv) { double valor; while(scanf("%lg", &valor) == 1) printf("sin(%g) = %g, seno(%g) = %g\n", valor, sin(valor), valor, seno(valor, 1e-6)); return EXIT_SUCCESS; }
Página
concebida e mantida por Eng. Manuel Menezes de Sequeira (última actualização 2006/07/07) Copyright © 1996-2001 ISCTE |