also add c lol

This commit is contained in:
Matyáš Caras 2024-10-10 21:51:50 +02:00
parent c3236bb92b
commit d4f77aed1b
Signed by: hernik
GPG key ID: 2A3175F98820C5C6
4 changed files with 412 additions and 1 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
main* main

75
Cviko4_1/main.c Normal file
View file

@ -0,0 +1,75 @@
#include "types.h"
#include <stdio.h>
/**
* Determine whether the provided character is alphabetic.
*
* @param c character
*
* @return true when the provided character `c` is alphabetic;
* false otherwise
*/
bool is_alpha(char c) {
// TODO: 1. Check whether the value in c represents an alphabetic ASCII
// character
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}
/**
* Determine whether all characters of the provided `string`
* (character array) are alphabetic.
*
* @param string input string
*
* @return true when all characters in the provided string are alphabetic;
* false otherwise
*/
bool is_name(char string[]) {
// TODO: 1. For each character in string
// - determine whether it is alphabetic by calling is_aplha function
bool is = true;
int i = 0;
while (true) {
if (string[i] == '\0') {
break;
}
if (!is_alpha(string[i])) {
is = false;
break;
}
++i;
}
return is;
}
#ifndef TEST_BUILD
int main() {
// Limit the size of names to 20 characters + terminating '\0'
char name[21];
char surname[21];
// Let the user specify their name
printf("Please provide your name: ");
scanf("%20s", name);
// Call our new function that checks if the name contains only alphabetic
// symbols The argument supplied to the function can have a different name
// than the one specified in the function definition.
if (!is_name(name)) {
printf("Provided name contains non-alphabetic characters!\n");
return 1;
}
printf("Please provide your surname: ");
scanf("%20s", surname);
if (!is_name(surname)) {
printf("Provided surname contains non-alphabetic characters!\n");
return 1;
}
printf("Your full name is: %s %s\n", name, surname);
return 0;
}
#endif

129
Cviko4_2/main.c Normal file
View file

@ -0,0 +1,129 @@
#include "types.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
/**
* Get the largest value in the provided array
*
* @note We assume the array is not empty!
*
* @param array Source integer array
* @param length Size of the array
*
* @return the largest value in the provided array
*/
int get_max(int array[], int length) {
// TODO: 1. Find the largest value in the array
// be mindful of possible array values
// (eg. they may all be negative or extremely large)
int result = INT_MIN;
for (int i = 0; i < length; ++i) {
if (array[i] > result) {
result = array[i];
}
}
// TODO: 2. Return the value found
return result;
}
/**
* Get the sum of all values in the provided array
*
* @param array Source integer array
* @param length Size of the array
*
* @return sum of all values in the array
*/
int get_sum(int array[], int length) {
// TODO: 1. Sum up all the values of the array
int result = 0;
for (int i = 0; i < length; ++i) {
result += array[i];
}
// TODO: 2. Return the value
return result;
}
/**
* Check that for all indices (plural of index),
* array1 has smaller values than array2
*
* @example
* arr1: [2, 4, 1, 10, -5]
* arr2: [8, 9, 2, 99, -1]
* ^ ^ ^ ^^ ^^
*
* @example
* arr1: [2, 4, 9, 10, -5]
* arr2: [8, 9, 2, 99, -1]
* ^ ^ x
*
* @note We assume the arrays are of the same length!
*
* @param array1 First integer array
* @param array2 Second integer array
* @param array_length Length of both arrays
*
* @return true when all values at all indices of array1 are
* smaller than corresponding values from array2;
* false otherwise
*/
bool values_are_smaller_than(int array1[], int array2[], int array_length) {
// TODO: 1. Iterate over all the array items and compare
// values from array1 with corresponding ones in array2
bool result = true;
for (int i = 0; i < array_length; ++i) {
if(array1[i] >= array2[i]){
result = false;
break;
}
}
// TODO: 2. Return false when there is value in array1
// larger than corresponding one in array2
// TODO: 3. Return true when all the items have been checked
return result;
}
#ifndef TEST_BUILD
int main() {
int arr1[5];
printf("Please select five numbers: ");
for (int index = 0; index < 5; index++) {
scanf("%d", &arr1[index]);
}
// We can either save the maximum value into our new variable and then use it
// later
int arr_max = get_max(arr1, 5);
printf("Maximum array value: %d\n", arr_max);
// Or we can use the function call directly = function call is an expression!
printf("Sum of array values: %d\n", get_sum(arr1, 5));
// Read another five numbers so that we can test the smaller_than function
int arr2[5];
printf("Please select another five numbers: ");
for (int index = 0; index < 5; index++) {
scanf("%d", &arr2[index]);
}
// We do not actually have to use the return value
// We have seen this before with functions such as printf and scanf
// - they also have a return value!
if (values_are_smaller_than(arr1, arr2, 5)) {
printf("The first array is smaller than the second.\n");
} else {
printf("The first array is not smaller than the second.\n");
}
return 0;
}
#endif

207
Cviko4_3/main.c Normal file
View file

@ -0,0 +1,207 @@
#include "types.h"
#include <limits.h>
#include <stdio.h>
/**
* Check if the value is in the supplied set
*
* @param set Integer array representing a set
* @param length Length of the provided array
* @param value Value to be looked up in the set
*
* @return true when the provided `value` is in `set`;
* false otherwise
*/
bool is_in_set(int set[], int length, int value)
{
bool result = false;
for (int i = 0; i < length; ++i) {
if (set[i] == value) {
result = true;
break;
}
}
return result;
}
/**
* Check if the supplied array represents set,
* i.e., if every value is unique
*
* @param set Integer array representing a set
* @param length Length of the provided array
*
* @return true when the provided `set` is an actual set;
* false otherwise
*/
bool is_set(int set[], int length)
{
bool result = true;
int intermediary[length];
for (int i = 0; i < length; ++i) {
intermediary[i] = 0;
}
bool hadZero = false;
for (int i = 0; i < length; ++i) {
if (set[i] == 0 && !hadZero) {
hadZero = true;
} else if (is_in_set(intermediary, length, set[i]) || (set[i] == 0 && hadZero)) {
result = false;
break;
} else {
intermediary[i] = set[i];
}
}
return result;
}
/**
* Check if the supplied array is a more effective representation of set,
* i.e., every value is unique and the values are sorted
*
* @param set Integer array representing a set
* @param length Length of the provided array
*
* @return true when the provided set is sorted;
* false otherwise
*/
bool is_sorted_set(int set[], int length)
{
if (!is_set(set, length)) {
return false;
}
bool result = true;
for (int outer = 0; outer < length; ++outer) {
// check sort from lowest to higheest
for (int inner = outer; inner != 0; --inner) {
if (set[inner] > set[outer]) {
result = false;
break;
}
}
if (!result) {
break; // break if result is already false
}
}
return result;
}
/**
* Print an intersection of set1 and set2
*
* @param set1 First integer array representing a set
* @param set1_length Length of the first provided array
* @param set2 Second integer array representing a set
* @param set2_length Length of the second provided array
*/
void print_intersection(int set1[], int set2[], int set1_length, int set2_length)
{
printf("{");
for (int i = 0; i < set1_length; ++i) {
if (is_in_set(set2, set2_length, set1[i])) {
printf("%d, ", set1[i]);
}
}
printf("}\n");
}
/**
* Print union of set1 and set2
*
* @param set1 First integer array representing a set
* @param set1_length Length of the first provided array
* @param set2 Second integer array representing a set
* @param set2_length Length of the second provided array
*/
void print_union(int set1[], int set2[], int set1_length, int set2_length)
{
printf("{");
for (int i = 0; i < set1_length; ++i) {
printf("%d, ", set1[i]);
}
for (int i = 0; i < set2_length; ++i) {
if (!is_in_set(set1, set1_length, set2[i])) { // don't print duplicate values
printf("%d, ", set2[i]);
}
}
printf("}\n");
}
/**
* Print cartesian product of set1 and set2
*
* @param set1 First integer array representing a set
* @param set1_length Length of the first provided array
* @param set2 Second integer array representing a set
* @param set2_length Length of the second provided array
*/
void print_product(int set1[], int set2[], int set1_length, int set2_length)
{
printf("{");
for (int i = 0; i < set1_length; ++i) {
for (int j = 0; j < set2_length; ++j) {
printf("(%d, %d), ", set1[i], set2[j]);
}
}
printf("}\n");
}
#ifndef TEST_BUILD
#define SET_LENGTH 5
int main()
{
int set1[SET_LENGTH];
int set2[SET_LENGTH];
// Some example inputs (also try to switch set1 and set2!):
// int set1 = {5, 1, 7, 3, 8}
// int set2 = {4, 10, 6, 8, 5}
// ----
// int set1 = {1, 3, 5, 7, 8}
// int set2 = {4, 5, 6, 8, 10}
// ----
// int set1 = {1, 3, 4, 5, 9}
// int set2 = {2, 6, 7, 8, 10}
printf("Please specify a set (%d numbers): ", SET_LENGTH);
for (int i = 0; i < SET_LENGTH; i++) {
scanf("%d", &set1[i]);
}
// Only test that the array values are unique
if (!is_set(set1, SET_LENGTH)) {
printf("Error: the set contains duplicate values!\n");
return 1;
}
if (!is_sorted_set(set1, SET_LENGTH)) {
printf("Info: the set is not sorted!\n");
}
printf("Please specify a set (%d numbers): ", SET_LENGTH);
for (int i = 0; i < SET_LENGTH; i++) {
scanf("%d", &set2[i]);
}
// Only test that the array values are unique
if (!is_set(set2, SET_LENGTH)) {
printf("Error: the set contains duplicate values!\n");
return 1;
}
if (!is_sorted_set(set2, SET_LENGTH)) {
printf("Info: the set is not sorted!\n");
}
// Since the set do not have to be sorted, use inefficient versions
printf("Intersection: ");
print_intersection(set1, set2, SET_LENGTH, SET_LENGTH);
printf("Union: ");
print_union(set1, set2, SET_LENGTH, SET_LENGTH);
printf("Product: ");
print_product(set1, set2, SET_LENGTH, SET_LENGTH);
printf("\n");
return 0;
}
#endif