This commit is contained in:
Tančáková 2024-03-05 14:30:03 +01:00
parent 9c90122f8d
commit 522adcb11a
2 changed files with 106 additions and 0 deletions

56
cv2/program.c Normal file
View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_CONTESTANTS 50
int main() {
int results[MAX_CONTESTANTS];
int num_results = 0;
int max_cups = 0;
// Read and process input
while (1) {
int value;
if (scanf("%d", &value) != 1) {
printf("Chyba: Nesprávny vstup.\n");
return 1;
}
if (value < 1) {
printf("Chyba: Nesprávna hodnota.\n");
return 1;
}
if (num_results >= MAX_CONTESTANTS) {
printf("Chyba: Príliš veľa hodnôt.\n");
return 1;
}
results[num_results++] = value;
char next_char = getchar();
if (next_char == '\n' || next_char == EOF) {
break;
}
ungetc(next_char, stdin);
}
if (num_results == 0) {
printf("Chyba: Málo platných hodnôt.\n");
return 1;
}
// Find the maximum number of cups drunk
for (int i = 0; i < num_results; i++) {
if (results[i] > max_cups) {
max_cups = results[i];
}
}
// Print results and winners
for (int i = 0; i < num_results; i++) {
printf("Súťažiaci č. %d vypil %d pohárov.\n", i + 1, results[i]);
if (results[i] == max_cups) {
printf("Výherca je súťažiaci č. %d ktorý vypil %d pohárov.\n", i + 1, results[i]);
}
}
return 0;
}

50
cv3/program.c Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_COEFFICIENTS 100
double evaluate_polynomial(double x, double coefficients[], int degree) {
double result = 0;
for (int i = degree; i >= 0; i--) {
result = result * x + coefficients[i];
}
return result;
}
int main() {
double x;
double coefficients[MAX_COEFFICIENTS];
int degree;
// Read the value of x
if (scanf("%lf", &x) != 1) {
printf("Error: Failed to read the value of x.\n");
return 1;
}
// Read the coefficients of the polynomial
int coefficient_number = 1;
while (scanf("%lf", &coefficients[coefficient_number - 1]) == 1) {
coefficient_number++;
if (coefficient_number > MAX_COEFFICIENTS) {
printf("Error: Too many coefficients.\n");
return 1;
}
}
degree = coefficient_number - 2; // degree of polynomial = number of coefficients - 1
// Check if at least one coefficient is loaded
if (degree < 0) {
printf("Error: No coefficients loaded.\n");
return 1;
}
// Evaluate the polynomial
double result = evaluate_polynomial(x, coefficients, degree);
// Print the result rounded to two decimal places
printf("The output is: %.2lf\n", result);
return 0;
}