Partilhar: WhatsApp
aulify
UC UC00669 · T. Desenv. Software

Ficha 01 · C básico

Sintaxe, arrays, strings, funções
Versão · Aluno
Tempo · 45 minutos
Aluno(a)
Turma
Data

Exercício 1 · Hello + variáveis

Cria um programa C que peça nome (string) e idade (int) ao utilizador, e imprima "Olá, [nome]! Daqui a 10 anos terás [idade+10] anos."

Resposta:
#include <stdio.h>

int main() {
    char nome[50];
    int idade;

    printf("Nome: ");
    scanf("%49s", nome);

    printf("Idade: ");
    scanf("%d", &idade);

    printf("Olá, %s! Daqui a 10 anos terás %d anos.\n", nome, idade + 10);

    return 0;
}
gcc programa.c -o programa
./programa

Exercício 2 · Soma de array

Cria função int soma_array(int arr[], int n) que recebe array e tamanho, retorna soma. No main, declara array com 5 elementos, chama a função e imprime.

Resposta:
#include <stdio.h>

int soma_array(int arr[], int n) {
    int total = 0;
    for (int i = 0; i < n; i++) {
        total += arr[i];
    }
    return total;
}

int main() {
    int numeros[5] = {10, 20, 30, 40, 50};
    int n = sizeof(numeros) / sizeof(int);

    int s = soma_array(numeros, n);
    printf("Soma: %d\n", s);

    // Bonus: média
    double media = (double) s / n;
    printf("Média: %.2f\n", media);

    return 0;
}

Exercício 3 · Máximo e mínimo

Lê 10 inteiros do utilizador para um array, e imprime o máximo e o mínimo.

Resposta:
#include <stdio.h>

int main() {
    int nums[10];

    printf("Introduz 10 números:\n");
    for (int i = 0; i < 10; i++) {
        printf("  [%d]: ", i + 1);
        scanf("%d", &nums[i]);
    }

    int max = nums[0], min = nums[0];
    for (int i = 1; i < 10; i++) {
        if (nums[i] > max) max = nums[i];
        if (nums[i] < min) min = nums[i];
    }

    printf("Máximo: %d\n", max);
    printf("Mínimo: %d\n", min);

    return 0;
}

Exercício 4 · Reverter string

Cria função void reverter(char *s) que inverte uma string in-place. Testa-a no main com a string "olá mundo".

Resposta:
#include <stdio.h>
#include <string.h>

void reverter(char *s) {
    int n = strlen(s);
    for (int i = 0; i < n / 2; i++) {
        char tmp = s[i];
        s[i] = s[n - 1 - i];
        s[n - 1 - i] = tmp;
    }
}

int main() {
    char texto[] = "olá mundo";
    printf("Antes: %s\n", texto);
    reverter(texto);
    printf("Depois: %s\n", texto);
    return 0;
}

Exercício 5 · Factorial

Cria função recursiva long factorial(int n) que calcula n!. Testa para 5, 10 e 15.

Resposta:
#include <stdio.h>

long factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int valores[] = {5, 10, 15};

    for (int i = 0; i < 3; i++) {
        int n = valores[i];
        printf("%d! = %ld\n", n, factorial(n));
    }

    // 5! = 120
    // 10! = 3628800
    // 15! = 1307674368000

    return 0;
}

Exercício 6 · Swap com pointers

Cria função void swap(int *a, int *b) que troca os valores de duas variáveis. Demonstra no main.

Resposta:
#include <stdio.h>

void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int main() {
    int x = 5, y = 10;
    printf("Antes: x=%d, y=%d\n", x, y);

    swap(&x, &y);

    printf("Depois: x=%d, y=%d\n", x, y);
    return 0;
}

Exercício 7 · Contar vogais

Cria função int contar_vogais(const char *s) que conta vogais (a, e, i, o, u, maiúsculas e minúsculas) numa string. Testa.

Resposta:
#include <stdio.h>
#include <ctype.h>

int contar_vogais(const char *s) {
    int c = 0;
    while (*s) {
        char ch = tolower(*s);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            c++;
        }
        s++;
    }
    return c;
}

int main() {
    const char *frases[] = {
        "Olá mundo",
        "Programar é divertido",
        "C é poderoso"
    };

    for (int i = 0; i < 3; i++) {
        printf("\"%s\" → %d vogais\n", frases[i], contar_vogais(frases[i]));
    }

    return 0;
}

Exercício 8 · Struct Aluno

Cria struct Aluno { char nome[50]; double nota; }. No main, declara array de 3 alunos, preenche-os, e imprime o nome do aluno com nota mais alta.

Resposta:
#include <stdio.h>
#include <string.h>

typedef struct {
    char nome[50];
    double nota;
} Aluno;

int main() {
    Aluno alunos[3] = {
        {"Ana", 17.5},
        {"Bruno", 12.3},
        {"Carla", 19.0}
    };

    int idx_max = 0;
    for (int i = 1; i < 3; i++) {
        if (alunos[i].nota > alunos[idx_max].nota) {
            idx_max = i;
        }
    }

    printf("Melhor: %s com %.1f valores\n", 
        alunos[idx_max].nome, 
        alunos[idx_max].nota);

    // Bonus: média turma
    double soma = 0;
    for (int i = 0; i < 3; i++) {
        soma += alunos[i].nota;
    }
    printf("Média turma: %.2f\n", soma / 3);

    return 0;
}