Linguagem C_Exercicios Propostos Solucao_5

Page 1


5.1

Linguagem C

25.ª Edição Atualizada e Aumentada

FCA Editora (c) 2025

5

EXERCÍCIOS PROPOSTOS – SOLUÇÃO

a) Falso.

b) Verdade.

c) Falso. Pode devolver qualquer tipo de dados válido ou apontador. Poderá também não devolver nada (void ).

d) Falso. Um parâmetro tem de ter sempre um tipo de dados associado.

e) Verdade.

f) Falso.

g) Falso. Termina sempre a execução da função.

h) Falso. Esta afirmação apenas é verdadeira na função main

i) Verdade. Ao sair da função main, não há mais nada a executar e o programa termina.

j) Falso. Se fosse verdade, como se poderia invocar a função?

k) Verdade.

l) Falso. Deve ser construída de maneira a executar uma única tarefa.

m) Falso. Porquê 10?

n) Falso. Porquê seis?

o) Verdade.

p) Verdade.

q) Verdade.

r) Verdade.

5.2

5.2.1 Ponto e vírgula a seguir ao cabeçalho da função.

5.2.2 A função void não pode fazer return de qualquer valor.

5.2.3 O protótipo difere do cabeçalho da função.

5.2.4 O protótipo difere do cabeçalho da função. Se a função não é declarada indicando explicitamente o tipo de retorno ou void , então, por defeito, o compilador assume que a função devolve int , o que não está de acordo com a definição da função, pois não tem tipo de retorno (void ).

5.2.5 Falta o nome da função.

5.2.6 A declaração dos parâmetros está feita incorretamente. Cada um dos parâmetros deve ser precedido do respetivo tipo: void f(int x, int y).

5.3

5.4

a) Sem qualquer output

b)

Ja Passei a C

c) Sem qualquer output

d) 0

e) -1 -3 -5

(ciclo infinito com os números ímpares)

int Abs(int x)

{ if (x>=0) return x; else

return -x; }

ou

int Abs(int x)

{ return (x>=0) ? x : -x; }

5.5

5.6

double my_power(double x, int n)

{ double res=1.0; while (n>0)

{ res *=x; } return res; } double getVAL(double x, int n, float t)

{ double res=0; for(int i=1; i<=n ; i++) res += x/my_power(1+t, i); return res; }

long int n_segundos(int n_horas)

{ return ((long) n_horas) * 3600; }

5.7

5.8

a) /* com if-else */

#include <stdio.h>

long int num(int n_horas, char tipo)

{ if (tipo=='h' || tipo == 'H')

return (long) n_horas; else if (tipo=='m' || tipo == 'M')

return ((long) n_horas)*60; else

return ((long) n_horas)*60*60; }

int main(void)

{ printf("%ld\n", num(3, 'h')); printf("%ld\n", num(3, 'm')); printf("%ld\n", num(3, 's')); return 0; }

b) /* Com o switch com break */

long int num(int n_horas, char tipo)

{ switch(tipo)

{ case 'h': case 'H': return (long) n_horas; case 'm': case 'M': return ((long) n_horas)*60; default: return ((long) n_horas)*60*60; } }

c) /* Com o switch sem qualquer break */

long int num(int n_horas, char tipo)

{ long res = n_horas; switch(tipo)

{ case 's': case 'S': res *= 60; case 'm': case 'M': res *= 60; } return res; }

float max3(float x, float y, float w)

{ if (x >= y) if (x >= w) return x; else

return w; else if (y >= w)

return y; else return w; }

5.9

Ou, então, podemos simplificar o problema escrevendo a função adicional max2 :

1: #include <stdio.h>

2:

3: float max2(float a, float b)

4: {

5: return (a>=b)? a : b; 6: }

7:

8: float max3(float x, float y, float w)

9: {

10: return max2(x, max2(y, w)); 11: }

12:

13: int main(void)

14: {

15: float a1, a2, a3; 16:

17: a1 = 1.0; a2 = 2.0; a3 = 3.0; 18: printf("Max(%.2f, %.2f, %.2f) --> %.2f\n", a1, a2, a3, 19: max3(a1, a2, a3));

20:

21: a1 = 3.0; a2 = 2.0; a3 = 1.0; 22: printf("Max(%.2f, %.2f, %.2f) --> %.2f\n", a1, a2, a3, 23: max3(a1, a2, a3));

24:

25: a1 = 1.0; a2 = 1.0; a3 = 1.0; 26: printf("Max(%.2f, %.2f, %.2f) --> %.2f\n", a1, a2, a3, 27: max3(a1, a2, a3));

28:

29: a1 = -1.0; a2 = -2.0; a3 = -3.0;

30: printf("Max(%.2f, %.2f, %.2f) --> %.2f\n", a1, a2, a3, 31: max3(a1, a2, a3));

32:

33: a1 = 1.0; a2 = 7.0; a3 = 3.0;

34: printf("Max(%.2f, %.2f, %.2f) --> %.2f\n", a1, a2, a3,

35: max3(a1, a2, a3));

36:

37: return 0;

38: }

int impar(int x) /* Se não é múltiplo de 2*/ { return (x%2==1); /* ou return x%2; */ }

5.10

int Entre(int x, int lim_inf, int lim_sup) { return (x >= lim_inf && x <= lim_sup); } 5.11

1: // 11.1

2: int isdigit(int ch)

3: {

4: return (ch>='0' && ch<='9');

5: }

6:

7: // 11.2

8: int isalpha(int ch)

PROG0516.C

5.12

9: { 10: return ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')); 11: }

12:

13: // 11.3

14: int isalnum(int ch)

15: {

16: return isalpha(ch) || isdigit(ch); 17: }

18:

19: // 11.4

20: int islower(int ch)

21: {

22: return (ch>='a' && ch<='z'); 23: }

24:

25: // 11.5

26: int isupper(int ch)

27: {

28: return (ch>='A' && ch<='Z');

29: }

30:

31: // 11.6

32: int isspace(int ch)

33: {

34: return ch==' ' || ch=='\t'; /* Espaço ou TAB */ 35: }

36:

37: // 11.7

38: int tolower(int ch)

39: {

40: if (isupper(ch))

41: return ch + 'a' -'A';

42: else

43: return ch;

44: }

45: // 11.8

46: int toupper(int ch)

47: {

48: if (islower(ch))

49: return ch + 'A' -'a';

50: else

51: return ch; 52: }

53:

54: int main(void) { 55: return 0; 56: }

int isSquare(int x, int y) { return x==y*y; }

5.13

int Minus(int valor)

{ if (valor<0) return valor; else return -valor; }

5.14

int Minus(int valor)

{ if (valor<0) return valor; return -valor; }

ou

int Minus(int valor)

{ return -Abs(valor); /* Usando a função escrita anteriormente */ }

int isSpecial(int x)

{ return 2*x==x*x; }

5.15

int Cubo(int x)

{ return x*x*x; }

5.16

int isVowel(char ch)

{ return ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U';

}

ou

int isVowel(char ch)

{ ch = toupper(ch); return ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'; }

ou

int isVowel(char ch)

{ switch (tolower(ch))

{ case 'a': case 'e': case 'i': case 'o': case 'u': return 1; default : return 0; } }

5.17

5.18

double Inverso(int x)

{ if (x==0)

return 0.0; else

return 1/(double) x; }

void linha(int n, char ch) // Função auxiliar

{ while (n--) putchar(ch);

}

void Triangulo(int n)

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

{ linha(n-i, ' '); linha(i, '*'); putchar('\n'); }

}

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.