obrazky
-

Opravte chyby v programoch:

  1. Program má:
  • načítať N čísel,

  • spočítať ich priemer,

  • vypísať najväčšie číslo.

Niečo však nefunguje správne. Použi AI ako pomoc pri hľadaní chýb a vysvetli, prečo bol problém a ako si ho opravil.

int main() {
    int n;
    printf("Zadaj pocet cisel: ");
    scanf("%d", n);
    int numbers[n];

    for (int i = 1; i <= n; i++) {
        printf("Zadaj cislo %d: ", i);
        scanf("%d", &numbers[i]);
    }

    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += numbers[i];
    }

    float avg = sum / n;

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

    printf("Priemer: %.2f\n", avg);
    printf("Najvacsie cislo: %d\n", max);

    return 0;
}
  1. Program má:
  • načítať vetu,

  • otočiť reťazec,

  • spočítať samohlásky,

  • zistiť, či je veta palindróm (ignorujúc medzery a veľkosť písmen).

Program je chybný. Pomôž si AI, ale opravy musíš vedieť odôvodniť.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char text[30];
    printf("Zadaj vetu: ");
    fgets(text, 100, stdin);
    int len = strlen(text);

    char reversed[30];

    for (int i = 0; i <= len; i++) {
        reversed[i] = text[len - i];
    }

    printf("Otoceny retazec: %s\n", reversed);

    int vowels = 0;
    for (int i = 0; i < len; i++) {
        char c = tolower(text[i]);
        if (c == "a" || c == "e" || c == "i" || c == "o" || c == "u" || c == "y") {
            vowels++;
        }
    }

    char cleaned[30];
    int j = 0;
    for (int i = 0; i < len; i++) {
        if (isalpha(text[i])) {
            cleaned[j] = tolower(text[i]);
            j++;
        }
    }

    char cleaned_reversed[30];
    for (int i = 0; i < j; i++) {
        cleaned_reversed[i] = cleaned[j - i];
    }

    if (strcmp(cleaned, cleaned_reversed) == 1) {
        printf("Je to palindrom.\n");
    } else {
        printf("Nie je to palindrom.\n");
    }

    return 0;
}
  1. Program má:
  • Načítať text zo súboru input.txt
  • Spočítať počet:
    • riadkov
    • slov
    • znakov
  • Zapísať výsledok do súboru output.txt

Kód obsahuje viacero chýb – tvojou úlohou je ich nájsť a opraviť.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    FILE *f;
    f = fopen("input.txt", "r");

    if (f = NULL) { 
        printf("Subor sa nepodarilo otvorit\n");
        return 1;
    }

    char line[50];
    int lines = 0;
    int words = 0;
    int chars = 0;

    while (feof(f) == 0) {
        fgets(line, 100, f);

        lines++;

        chars += strlen(line);

        for (int i = 0; i <= strlen(line); i++) {
            if (line[i] == ' ' || line[i] == '\n') {
                words++;
            }
        }
    }

    fclose(f);

    FILE *out;
    out = fopen("output.txt", "w");

    fprintf(out, "Riadky: %d\n", lines);
    fprintf(out, "Slova: %d\n", words);
    fprintf(out, "Znaky: %d\n", chars);

    fclose(out);

    return 0;
}
-
Copyright © 2008-2026 Miroslava Valíková