CSE1146 Vize I Çözümleri
İlk soruyu sınav esnasında yazmış olmama rağmen, bir hatadan dolayı (128. satırı unutmuştum) çok zaman kaybettim ve ikinci soruya bakamamıştım. Fırsat oldu baktım, buyurun onun çözümünü de ekliyorum.
Not, sorular bize tekrar açıldıktan sonra birkaç yanlış hatırladığım nokta fark ettim ikinci soruda. Onları düzelttim.
60 Puanlık İlk Sorunun Çözümü
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* insertLast
* insertFirst
* insertAfter
*
* searchItem
* deleteItem
*
* print
* bye
*/
void print_linked_list(); // Print all elements in List
void insert_at_last(int value); // Insert an element to end of List
void insert_at_first(int value); // Insert an element to head of List
void insert_after(int key, int value); // Insert an element after a certain element in List
void delete_item(int value); // Delete an element in List
void search_item(int value); // Search an element if exists in List
typedef struct node_str {
int value;
struct node_str *next;
struct node_str *prev;
} intNode;
intNode *database;
int main() {
// Database creation
database = (intNode *) malloc(sizeof(intNode));
if (database == NULL) {
printf("Stack overflow occur. Exiting.");
exit(-1);
}
// Menu and other things
char command[30];
int i_1 = 0;
int i_2 = 0;
command[0] = '\0';
while (strcmp(command, "bye")!=0) {
scanf("%s", command);
if (strcmp(command, "insertLast")==0) {
scanf("%d", &i_1);
insert_at_last(i_1);
} else if (strcmp(command, "insertFirst")==0) {
scanf("%d", &i_1);
insert_at_first(i_1);
} else if (strcmp(command, "insertAfter")==0) {
scanf("%d %d", &i_1, &i_2);
insert_after(i_1, i_2);
} else if (strcmp(command, "print")==0) {
print_linked_list();
} else if (strcmp(command, "deleteItem") == 0) {
scanf("%d", &i_1);
delete_item(i_1);
} else if (strcmp(command, "searchItem") == 0) {
scanf("%d", &i_1);
search_item(i_1);
} else if (strcmp(command, "bye") == 0) {
break;
} else {
printf("No such command!\n");
}
}
printf("End session.\n");
free(database);
return 0;
}
void print_linked_list() {
// I do not put any values to head, so go to next.
intNode *cursor = database->next;
// If list is empty
if (cursor == NULL) {
printf("Linked list is empty.\n");
return;
}
printf("Linked list items: ");
// Go the next item and print them
while (cursor->next != NULL) {
printf("%d ", (cursor->value));
cursor = cursor->next;
}
// Print the last element
printf("%d\n", (cursor->value));
}
void insert_at_last(int value) {
intNode *cursor = database;
// Go the last item of the LL.
while (cursor->next != NULL) {
cursor = cursor->next;
}
// Create new intNode
intNode *new_data = (intNode*) malloc(sizeof(intNode));
if (new_data == NULL) {
printf("Stack overflow occur. Exiting.\n");
exit(-1);
}
// Assign right values
new_data->value = value;
new_data->prev = cursor;
new_data->next = NULL;
// Append to the cursor
cursor->next = new_data;
printf("%d is inserted to the last.\n", value);
}
void insert_at_first(int value) {
// Creating new data
intNode *new_data = (intNode*) malloc(sizeof(intNode));
if (new_data == NULL) {
printf("Stack overflow occur. Exiting.\n");
exit(-1);
}
// Assigning right values
new_data->prev = database;
new_data->next = database->next;
new_data->value = value;
database->next->prev = new_data;
database->next = new_data;
printf("%d is inserted as the first.\n", value);
}
void insert_after(int key, int value) {
// THIS FUNCTION IS NOT CLEAR ENOUGH IF THE KEY IS IN DATABASE
// MORE THEN ONE, THIS WILL EXECUTE FIRST OCCURENCE.
// value will inserted after key
intNode *cursor = database;
// Go the last item of the LL.
while (cursor->next != NULL) {
// If it is equal, break
if (cursor->value == key) break;
// If not go the next one.
cursor = cursor->next;
}
// Creating new data
intNode *new_data = (intNode*) malloc(sizeof(intNode));
if (new_data == NULL) {
printf("Stack overflow occur. Exiting.\n");
exit(-1);
}
// Assigning right values
cursor->next->prev = new_data;
new_data->next = cursor->next;
new_data->prev = cursor;
new_data->value = value;
cursor->next = new_data;
printf("%d is inserted after %d.\n", value, key);
}
void delete_item(int value) {
// THIS FUNCTION IS NOT CLEAR ENOUGH IF THE value IS IN DATABASE
// MORE THEN ONE
intNode* head = database;
intNode *cursor = database->next;
// If list is empty
if (cursor == NULL) {
printf("Element not found!\n");
return;
}
// Go the last item of the LL.
while (cursor->next != NULL) {
// If it is equal, break
if (cursor->value == value) break;
// If not go the next one.
cursor = cursor->next;
}
if (cursor->next == NULL && cursor->value != value) {
printf("Element not found!\n");
return;
} else if (cursor->next == NULL && cursor->value == value) {
intNode *temp = cursor->prev;
free(cursor);
temp->next = NULL;
database = head;
printf("%d is deleted from the list.\n", value);
return;
}
// Assigning right values
intNode *temp_prev = cursor->prev;
temp_prev->next = cursor->next;
cursor->next->prev = temp_prev;
// Freeing allocated space
free(cursor);
printf("%d is deleted from the list.\n", value);
database = head;
}
void search_item(int value) {
// THIS FUNCTION IS NOT CLEAR ENOUGH IF THE value IS IN DATABASE
// MORE THEN ONE
intNode *cursor = database->next;
// Go the last item of the LL.
while (cursor->next != NULL) {
// If it is equal, break
if (cursor->value == value) break;
// If not go the next one.
cursor = cursor->next;
}
if (cursor->next == NULL) {
printf("Element not found!\n");
return;
} else {
printf("%d is present in the list.\n", value);
}
}
40 Puanlık İkinci Sorunun Çözümü
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int data = 0;
int num_count = 0;
int *numbers = calloc(2, sizeof(numbers));
while (1) {
// Getting input from the user
scanf("%d", &data);
// If the input is negative, break
if (data < 0) break;
// I'm allocation num_count+1 because of
// suspending the allocation is integer error.
num_count++;
numbers = (int *) realloc(numbers, (num_count * sizeof(int)));
// Checking if its allocated right.
if (numbers==NULL) {
printf("Stack overflow occur.");
exit(-1);
}
// Adding the data to array.
numbers[num_count-1] = data;
}
int sum = 0;
int geo = 1;
// Traveling through the array
for (int i = 0; i < num_count; i++) {
sum += numbers[i];
geo *= numbers[i];
}
// Calculating necessary values
float avg = (float) sum/(float) num_count;
double geometric = pow(geo, (float)1/(float)num_count);
// Printing: maybe some of the prints are wrongly written
// according to the question wants since I write it later
printf("Arithmetic Mean: %.2f\n", avg);
printf("Geometric Mean: %.2f\n", geometric);
printf("Numbers above average: ");
for (int j = 0; j < num_count; ++j) {
if ((float) numbers[j] > avg) printf("%d ", numbers[j]);
}
printf("\nNumbers bellow geometric: ");
for (int j = 0; j < num_count; ++j) {
if ((float) numbers[j] < geometric) printf("%d ", numbers[j]);
}
return 0;
}
Bir not olarak belirtmeliyim ki, gcc
'de -Wall
ile derlemeli ve math.h
kütüphanesini çağırabilmek için -lm
bayrağını eklemelisiniz. Ben CMake kullandığım için bunu şu şekilde yapabildim: target_link_libraries(Playground m)
.
Kolaylıklar dilerim.