add initial c, cpp files

This commit is contained in:
2026-04-02 01:33:59 +02:00
commit c51e836ed9
5 changed files with 425 additions and 0 deletions

47
c/isp-ws25/a2-vowels.c Normal file
View File

@@ -0,0 +1,47 @@
#include <stdio.h>
#include <string.h>
#define STRING_SIZE 30
void inputString(char user_input[])
{
printf("Please enter the string: ");
fgets(user_input, STRING_SIZE, stdin);
}
void countVowels(char user_input[], int vowels[])
{
int position = 0;
while (user_input[position] != '\n')
{
if (user_input[position] == 'a' || user_input[position] == 'A') vowels[0]++;
if (user_input[position] == 'e' || user_input[position] == 'E') vowels[1]++;
if (user_input[position] == 'i' || user_input[position] == 'I') vowels[2]++;
if (user_input[position] == 'o' || user_input[position] == 'O') vowels[3]++;
if (user_input[position] == 'u' || user_input[position] == 'U') vowels[4]++;
position++;
}
}
void printVowels(int vowels[])
{
printf("a: %d\n", vowels[0]);
printf("e: %d\n", vowels[1]);
printf("i: %d\n", vowels[2]);
printf("o: %d\n", vowels[3]);
printf("u: %d\n", vowels[4]);
}
int main(void)
{
char user_input[STRING_SIZE] = {'\0'};
int vowels[5] = {0};
inputString(user_input);
countVowels(user_input, vowels);
printVowels(vowels);
return 0;
}

View File

@@ -0,0 +1,163 @@
/******************************************************************************
Student Management Using Linked Lists
Task Description
In this coding task, you will extend a student management system. The program already creates students and stores them in a sorted linked list. Your task is to implement the functionality of adding additional students into the sorted linked list. Some parts of the program are already implemented.
Key points:
Already Implemented: The student struct is defined. Students are created in the main function and added manually into a linked list.
Your task: The function insertSorted should insert a student into the existing linked list at the correct position.
The linked list is sorted alphabetically by student names.
Hints:
How to find the position to insert?
Stop iterating over the linked list at the element before the one you want to insert.
In the visualization above, we insert "Benedikt" after "Andreas" and before "Daniel". Stop at "Andreas" and change the pointers accordingly.
Consider the case of adding the student in the middle of the linked list, but also at the end and at the head (index to insert = 0)
Have a look at the testcases
Implement the following functions:
-> insertSorted(Student *list, Student *new_student)
This function takes two arguments:
i) a pointer to the current list of students (list),
ii) a pointer to the students to insert into the list.
Add the student into the existing linked list at the correct position.
The linked list is sorted alphabetically by name.
The function should return the head of the linked list.
-> freeStudents(Student *list)
Free all students in the linked list.
This is tested with valgrind checks at testcase 2 and 3.
The function should not return anything.
Specifications
Use dynamic memory allocation
The program must compile and run without memory leaks/errors.
Use the existing Linked List.
You don't need to modify the structs or the print function.
You don't have to consider the case where no more memory is available.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student
{
char name_[32];
struct Student *next_;
} Student;
Student* createStudent(char* name)
{
Student *s = malloc(sizeof(Student));
if (!s) return NULL;
strcpy(s->name_, name);
s->next_ = NULL;
return s;
}
void printStudents(Student *list)
{
Student *cur = list;
int index = 0;
while (cur != NULL)
{
printf("%d: %s\n", index++, cur->name_);
cur = cur->next_;
}
}
// TODO: write your code here
Student *insertSorted(Student *list, Student *new_student)
{
Student *current = list;
Student *previous = NULL;
while(current != NULL && strcmp(current->name_, new_student->name_) <= 0)
{
previous = current;
current = current->next_;
}
if(previous == NULL)
{
new_student->next_ = current;
list = new_student;
}
else
{
new_student->next_ = current;
previous->next_ = new_student;
}
return list;
}
void freeStudents(Student *list)
{
if (!list) return;
freeStudents(list->next_);
free(list);
list = NULL;
}
// TODO: write your code here
int main(void)
{
int test;
printf("Enter tc: ");
scanf("%d", &test);
if(test == 1)
{
// test adding at the second and third position
Student* s1 = createStudent("Andreas");
Student* s2 = createStudent("Daniel");
s1->next_ = s2;
Student* to_add1 = createStudent("Benedikt");
Student* to_add2 = createStudent("Clemens");
s1 = insertSorted(s1, to_add1);
s1 = insertSorted(s1, to_add2);
printStudents(s1);
//freeStudents(s1); // uncomment that after you implemented the function
}
else if(test == 2)
{
// test adding at same first character (T)
Student* s1 = createStudent("Benjamin");
Student* s2 = createStudent("Tanja");
Student* s3 = createStudent("Thomas");
s1->next_ = s2;
s2->next_ = s3;
Student* to_add1 = createStudent("Toni");
Student* to_add2 = createStudent("Tamim");
s1 = insertSorted(s1, to_add1);
s1 = insertSorted(s1, to_add2);
printStudents(s1);
//freeStudents(s1); // uncomment that after you implemented the function
}
else if(test == 3)
{
// test adding in the beginning and end of the LL
Student* s1 = createStudent("Lenon");
Student* s2 = createStudent("Margarita");
s1->next_ = s2;
Student* to_add1 = createStudent("Julia");
Student* to_add2 = createStudent("Kilian");
s1 = insertSorted(s1, to_add1);
s1 = insertSorted(s1, to_add2);
printStudents(s1);
//freeStudents(s1); // uncomment that after you implemented the function
}
return 0;
}

View File

@@ -0,0 +1,25 @@
/*
Instructions
The classical introductory exercise. Just say "Hello, World!".
"Hello, World!" is the traditional first program for beginning programming in a new language or environment.
The objectives are simple:
Modify the provided code so that it produces the string "Hello, World!".
Run the test suite and make sure that it succeeds.
Submit your solution and check it at the website.
If everything goes well, you will be ready to fetch your first real exercise.
*/
#include "hello_world.h"
using namespace std;
namespace hello_world {
string hello() { return "Goodbye, Mars!"; }
} // namespace hello_world

View File

@@ -0,0 +1,77 @@
/*
Instructions
Lucian's girlfriend is on her way home and he hasn't cooked their anniversary dinner!
In this exercise, you're going to write some code to help Lucian
cook an exquisite lasagna from his favorite cookbook.
You have four tasks, all related to the time spent cooking the lasagna.
1. Define the expected oven time in minutes
Define the ovenTime() function that does not take any arguments
and returns how many minutes the lasagna should be in the oven.
According to the cookbook, the expected oven time is 40 minutes:
ovenTime();
// => 40
2. Calculate the remaining oven time in minutes
Define the remainingOvenTime(int actualMinutesInOven) function that takes
the actual minutes the lasagna has been in the oven as an argument and
returns how many minutes the lasagna still has to remain in the oven,
based on the expected oven time in minutes from the previous task.
remainingOvenTime(30);
// => 10
3. Calculate the preparation time in minutes
Define the preparationTime(int numberOfLayers) function that takes the number
of layers you added to the lasagna as an argument and returns how many minutes
you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.
preparationTime(2);
// => 4
4. Calculate the elapsed time in minutes
Define the elapsedTime(int numberOfLayers, int actualMinutesInOven) function
that takes two arguments:
- first argument is number of layers you added to the lasagna
- second argument is number of minutes the lasagna has been in the oven
The function should return how many minutes you've worked on cooking the lasagna,
which is the sum of the preparation time in minutes,
and the time in minutes the lasagna has spent in the oven at the moment.
elapsedTime(3, 20);
// => 26
*/
// ovenTime returns the amount in minutes that the lasagna should stay in the
// oven.
int ovenTime() {
return 40;
}
/* remainingOvenTime returns the remaining
minutes based on the actual minutes already in the oven.
*/
int remainingOvenTime(int actualMinutesInOven) {
return ovenTime() - actualMinutesInOven;
}
/* preparationTime returns an estimate of the preparation time based on the
number of layers and the necessary time per layer.
*/
int preparationTime(int numberOfLayers) {
return 2 * numberOfLayers;
}
// elapsedTime calculates the total time spent to create and bake the lasagna so
// far.
int elapsedTime(int numberOfLayers, int actualMinutesInOven) {
return preparationTime(numberOfLayers) + actualMinutesInOven;
}

View File

@@ -0,0 +1,113 @@
# About
To set off the C++ journey we are starting with variables, function calls, and comments.
## Comments
Comments come in two flavors: single- and multi-line.
Everything that comes after `//` on the same line is ignored by the compiler.
Multi-line comments are also known as C-style comments.
They are surrounded by `/*` and `*/`.
Anything that comes between these will be ignored as well.
## Variables
C++ is a typed language.
All types need to be known at compile time, and you generally need to state them explicitly.
A variable's type cannot change.
An integer variable with the name `years` can be declared like this:
```cpp
int years;
```
It is good practice to initialize variables upon declaration.
C++ offers different mechanisms to do so.
The version with the curly braces is more in line with modern C++, but the equal-sign version is also very common.
```cpp
int tomatoes{80};
int potatoes = 40;
```
~~~~exercism/caution
C++ does allow using uninitialized variables.
Until the variable is deliberately set, it is undefined and might contain anything.
To avoid used-before-set errors and undefined behavior it is adviseable to **always initialize**.
Undefined behavior can crash your program at the worst possible moment, while it was running fine previously.
It cannot be stressed enough: avoid undefined behavior at all cost.
~~~~
## Arithmetic Operations
Arithmetic operators like `*`, `+`, or `-` can be part of an expression like `3 * 2` or `tomatoes + potatoes`.
## Updating Variables
You can reassign variables, as long as they keep their type:
```cpp
tomatoes = tomatoes - 5; // tomatoes is now 75
potatoes = (32 * 2) + 11; // potatoes is now 75 as well
```
## Functions
Functions have a name, a return type and a (possibly empty) parameter list.
An example of a function named `always_fortyseven` that would always return 47 would look like this:
```cpp
int always_fortyseven() {
return 47;
}
```
Here is `vip_fee`, which has one parameter:
```cpp
int vip_fee(int standard_fee) {
/*
vip_fee calculates the vip fee based on the standard_fee.
*/
int vip_multi{3};
return standard_fee * vip_multi;
}
```
Or `total_fee`, a function with three parameters and a call to another function.
```cpp
int total_fee(int vips, int adults, int kids) {
/*
total_fee calculates the total price for a group of VIP and adult guests with kids.
Kids get a flat discount on the standard fee.
VIP guest fees are calculated by calling vip_fee.
*/
int standard_fee{30};
int kids_discount{15};
int kids_total_fee = kids * (standard_fee - kids_discount);
int vips_total_fee = vips * vip_fee(standard_fee);
int adult_total_fee = adults * standard_fee;
return vips_total_fee + adult_total_fee + kids_total_fee;
}
```
Functions in C++ do not return the value of the last statement like in some other languages.
The `return` keyword is required for the code to compile.
### Whitespace
Whitespace is used for formatting source code and includes spaces, tabs, or newlines.
As the compiler ignores unnecessary whitespace, you can use it to structure your code.
Smart use of whitespace can improve the readability of your code.
There are different formatting standards, but these are all conventional and not enforced by the compiler.
```cpp
// Formatting makes it easier to find bugs
int eggs_yolks = 3;
int yeast = 15;
int flour=500;int sugar=200;// compact, but difficult to read
```