added weekly exercises of isp-ws25

This commit is contained in:
2026-04-13 19:02:19 +02:00
parent c51e836ed9
commit 00fdd31a18
28 changed files with 1835 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
/*
In this task, you should write a program that checks if an array contains duplicate integer values.
First, the program prints Enter the number of elements to store in the array (max MAX_ARRAY_SIZE): . Then, the user can specify the size of the array (limited to the given constant MAX_ARRAY_SIZE). If the input of the user is less than 1 or greater than MAX_ARRAY_SIZE, the program should print Invalid size\n and ask the user again for the size of the array.
Once the size has been specified by the user, the user can enter n (i.e., n = size of the array) elements as integer values. This is done one after another by asking the user Element i: to enter the ith element.
After all elements have been entered by the user, the duplication check is performed. For this, implement the following function:
containsDuplicates(int values[], int size): This function takes the array containing the user-defined values and the size of the array as parameters. The function should return whether the array contains any duplicate values or not. The function should return the boolean value true if the array contains duplicate values, and false otherwise.
In the main() function, call containsDuplicates(int values[], int size) and print The array contains duplicates\n if the return value is true, and The array does not contain duplicates\n if the return value is false.
This is an example:
Enter the number of elements to store in the array (max 10): 4
Element 1: 2
Element 2: 9
Element 3: -1
Element 4: 9
The array contains duplicates
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_ARRAY_SIZE 10
void getArraySize(int* array_size);
void getArrayValues(int values[], int array_size);
bool containsDuplicates(int values[], int array_size);
int main(void)
{
bool has_duplicates = 0;
int array_size = 0;
getArraySize(&array_size);
int values_to_check[array_size];
memset(values_to_check, 0, array_size*sizeof(int));
getArrayValues(values_to_check, array_size);
has_duplicates = containsDuplicates(values_to_check, array_size);
if (has_duplicates)
{
printf("The array contains duplicates\n");
}
else
{
printf("The array does not contain duplicates\n");
}
return 0;
}
void getArraySize(int* array_size)
{
while (1)
{
printf("Enter the number of elements to store in the array (max %d): ", MAX_ARRAY_SIZE);
scanf("%d", array_size);
if ( 1 > *array_size || *array_size > MAX_ARRAY_SIZE )
{
printf("Invalid size\n");
continue;
}
return;
}
}
void getArrayValues(int values[], int array_size)
{
for ( int counter = 0 ; counter < array_size ; counter++ )
{
printf("Element %d: ", (counter + 1));
scanf("%d", &values[counter]);
}
}
bool containsDuplicates(int values[], int array_size)
{
for (int counter_a = 0 ; counter_a < array_size ; counter_a++)
{
printf("Loop A, Position %d: %d\n", counter_a, values[counter_a]);
for (int counter_b = counter_a + 1 ; counter_b < array_size ; counter_b++)
{
printf("Loop B, Position %d: %d\n", counter_b, values[counter_b]);
if (values[counter_a] == values[counter_b]){ return true; }
}
}
return false;
}