Compare commits

..

2 Commits

Author SHA1 Message Date
a0c4add101 Delete 'cv3' 2024-03-08 08:12:29 +00:00
0facba1a3a program.c
#include <stdio.h>
#include <stdlib.h>

double hornerScheme(double x, double* coef, int n) {
    double result = coef[0];
    for (int i = 1; i < n; i++) {
        result = result * x + coef[i];
    }
    return result;
}

int main() {
    double x;
    printf("Enter the value of x: ");
    if (scanf("%lf", &x) != 1) {
        printf("Invalid input for x.\n");
        return 1;
    }

    int capacity = 10, count = 0;
    double* coefficients = (double*)malloc(capacity * sizeof(double));
    if (coefficients == NULL) {
        printf("Failed to allocate memory.\n");
        return 1;
    }

    printf("Enter coefficients (end with non-numeric input):\n");
    while (scanf("%lf", &coefficients[count]) == 1) {
        count++;
        if (count >= capacity) {
            capacity *= 2;
            double* temp = (double*)realloc(coefficients, capacity * sizeof(double));
            if (temp == NULL) {
                free(coefficients);
                printf("Failed to reallocate memory.\n");
                return 1;
            }
            coefficients = temp;
        }
    }

    // Clean the input buffer
    while (getchar() != '\n');

    double result = hornerScheme(x, coefficients, count);
    printf("The result is: %.2f\n", result);

    free(coefficients);
    return 0;
}
2024-03-08 08:11:37 +00:00
7 changed files with 0 additions and 372 deletions

View File

View File

@ -1,90 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 100
int compare(const void *a, const void *b) {
const char *name1 = *(const char **)a;
const char *name2 = *(const char **)b;
return strcmp(name1, name2);
}
int main() {
int maxStudents;
char name[MAX_NAME_LENGTH];
char **names = NULL;
int count = 0;
int capacity = 0;
if (scanf("%d\n", &maxStudents) != 1 || maxStudents <= 0) {
puts("Nespravny vstup");
return 0;
}
while (fgets(name, MAX_NAME_LENGTH, stdin)) {
if (name[0] == '\n') break;
name[strcspn(name, "\n")] = '\0';
if (count >= capacity) {
capacity = capacity == 0 ? 4 : capacity * 2;
names = realloc(names, capacity * sizeof(char *));
if (names == NULL) {
perror("Failed to reallocate memory");
return 0;
}
}
int isDuplicate = 0;
for (int i = 0; i < count; i++) {
if (strcmp(names[i], name) == 0) {
isDuplicate = 1;
break;
}
}
if (!isDuplicate) {
names[count] = strdup(name);
if (names[count] == NULL) {
perror("Failed to duplicate name");
return 0;
}
count++;
}
}
if (count == 0) {
puts("Ziadne prihlasky");
return 0;
}
qsort(names, count, sizeof(char *), compare);
puts("Prijati studenti:");
int limit = count < maxStudents ? count : maxStudents;
for (int i = 0; i < limit; i++) {
puts(names[i]);
}
if (count > maxStudents) {
puts("Neprijati studenti:");
for (int i = maxStudents; i < count; i++) {
puts(names[i]);
}
}
for (int i = 0; i < count; i++) {
free(names[i]);
}
free(names);
return 0;
}

View File

@ -1,48 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COEFFICIENTS 100
#define BUFFER_SIZE 1024
double evaluatePolynomial(double x, double coefficients[], int n) {
double result = 0.0;
for (int i = 0; i < n; ++i) {
result = result * x + coefficients[i];
}
return result;
}
int main() {
char buffer[BUFFER_SIZE];
double x, coef;
double coefficients[MAX_COEFFICIENTS];
int count = 0;
fgets(buffer, BUFFER_SIZE, stdin);
if (sscanf(buffer, "%lf", &x) != 1) {
printf("Nepodarilo sa nacitat zaklad x\n");
return 0;
}
while (fgets(buffer, BUFFER_SIZE, stdin) && buffer[0] != '\n') {
if (sscanf(buffer, "%lf", &coef) == 1) {
coefficients[count++] = coef;
if (count >= MAX_COEFFICIENTS) {
printf("Maximum number of coefficients exceeded.\n");
return 0;
}
} else {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", count + 1);
return 0;
}
}
double result = evaluatePolynomial(x, coefficients, count);
printf("Vysledok je: %.2f\n", result);
return 0;
}

View File

@ -1,82 +0,0 @@
#include "list_ops.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
list_t *new_list(size_t length, list_element_t elements[]) {
list_t *list = malloc(sizeof(list_t) + length * sizeof(list_element_t));
list->length = length;
for (size_t i = 0; i < length; i++) {
list->elements[i] = elements[i];
}
return list;
}
list_t *append_list(list_t *list1, list_t *list2) {
size_t new_length = list1->length + list2->length;
list_t *new_list = malloc(sizeof(list_t) + new_length * sizeof(list_element_t));
new_list->length = new_length;
for (size_t i = 0; i < list1->length; i++) {
new_list->elements[i] = list1->elements[i];
}
for (size_t i = 0; i < list2->length; i++) {
new_list->elements[list1->length + i] = list2->elements[i];
}
return new_list;
}
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
list_t *temp_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
temp_list->length = 0;
for (size_t i = 0; i < list->length; i++) {
if (filter(list->elements[i])) {
temp_list->elements[temp_list->length++] = list->elements[i];
}
}
list_t *filtered_list = new_list(temp_list->length, temp_list->elements);
free(temp_list);
return filtered_list;
}
size_t length_list(list_t *list) {
return list->length;
}
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) {
list_t *mapped_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
mapped_list->length = list->length;
for (size_t i = 0; i < list->length; i++) {
mapped_list->elements[i] = map(list->elements[i]);
}
return mapped_list;
}
list_element_t foldl_list(list_t *list, list_element_t initial, list_element_t (*foldl)(list_element_t, list_element_t)) {
list_element_t accumulator = initial;
for (size_t i = 0; i < list->length; i++) {
accumulator = foldl(list->elements[i], accumulator);
}
return accumulator;
}
list_element_t foldr_list(list_t *list, list_element_t initial, list_element_t (*foldr)(list_element_t, list_element_t)) {
list_element_t accumulator = initial;
for (int i = list->length - 1; i >= 0; i--) {
accumulator = foldr(list->elements[i], accumulator);
}
return accumulator;
}
list_t *reverse_list(list_t *list) {
list_t *reversed_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
reversed_list->length = list->length;
for (size_t i = 0; i < list->length; i++) {
reversed_list->elements[i] = list->elements[list->length - 1 - i];
}
return reversed_list;
}
void delete_list(list_t *list) {
free(list);
}

View File

@ -1,75 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTOV 100
#define MAX_MENO 100
typedef struct {
char meno[MAX_MENO];
int hlasy;
} Student;
Student studenti[MAX_STUDENTOV];
int pocetStudentov = 0;
int pridajHlas(char* meno, int pocetHlasov) {
for (int i = 0; i < pocetStudentov; i++) {
if (strcmp(studenti[i].meno, meno) == 0) {
studenti[i].hlasy += pocetHlasov;
return 0;
}
}
if (pocetStudentov < MAX_STUDENTOV) {
strcpy(studenti[pocetStudentov].meno, meno);
studenti[pocetStudentov].hlasy = pocetHlasov;
pocetStudentov++;
return 0;
}
return 1;
}
int porovnaj(const void* a, const void* b) {
Student *studentA = (Student *)a;
Student *studentB = (Student *)b;
if (studentA->hlasy == studentB->hlasy) {
return strcmp(studentA->meno, studentB->meno);
}
return studentB->hlasy - studentA->hlasy;
}
int main() {
char riadok[150];
char meno[MAX_MENO];
int pocetHlasov;
int validneZaznamy = 0;
while (fgets(riadok, sizeof(riadok), stdin)) {
if (sscanf(riadok, "%d %[^\n]s", &pocetHlasov, meno) == 2) {
if (pridajHlas(meno, pocetHlasov)) {
printf("Nepodarilo sa pridať hlas.\n");
return 1;
}
validneZaznamy = 1;
} else {
break;
}
}
if (!validneZaznamy) {
printf("Nepodarilo nacitat nic\n");
return 0;
}
qsort(studenti, pocetStudentov, sizeof(Student), porovnaj);
printf("Vysledky:\n");
for (int i = 0; i < pocetStudentov; i++) {
printf("%d %s\n", studenti[i].hlasy, studenti[i].meno);
}
return 0;
}

View File

@ -1,77 +0,0 @@
#include "snake.h"
#include <stdlib.h>
struct snake* add_snake(struct snake* head, int x, int y) {
struct snake* new_head = (struct snake*)malloc(sizeof(struct snake));
new_head->x = x;
new_head->y = y;
new_head->next = head;
return new_head;
}
struct snake* remove_snake(struct snake* head) {
if (head == NULL || head->next == NULL) {
free(head);
return NULL;
}
struct snake* current = head;
while (current->next->next != NULL) {
current = current->next;
}
free(current->next);
current->next = NULL;
return head;
}
int is_snake(struct snake* snake, int x, int y) {
struct snake* current = snake;
while (current != NULL) {
if (current->x == x && current->y == y) {
return 1;
}
current = current->next;
}
return 0;
}
void free_snake(struct snake* snake) {
struct snake* current = snake;
while (current != NULL) {
struct snake* next = current->next;
free(current);
current = next;
}
}
int step_state(struct state* state) {
int new_x = state->snake->x + state->sx;
int new_y = state->snake->y + state->sy;
if (new_x < 0 || new_y < 0 || new_x >= state->width || new_y >= state->height) {
return END_WALL;
}
if (is_snake(state->snake, new_x, new_y)) {
return END_SNAKE;
}
for (int i = 0; i < FOOD_COUNT; i++) {
if (state->foodx[i] == new_x && state->foody[i] == new_y) {
state->foodx[i] = state->foody[i] = -1;
state->snake = add_snake(state->snake, new_x, new_y);
int all_eaten = 1;
for (int j = 0; j < FOOD_COUNT; j++) {
if (state->foodx[j] != -1) {
all_eaten = 0;
break;
}
}
return all_eaten ? END_FOOD : END_CONTINUE;
}
}
state->snake = add_snake(state->snake, new_x, new_y);
state->snake = remove_snake(state->snake);
return END_CONTINUE;
}