added weekly exercises of isp-ws25
This commit is contained in:
59
c/isp-ws25/weeklies/week02/w02ex01.c
Executable file
59
c/isp-ws25/weeklies/week02/w02ex01.c
Executable file
@@ -0,0 +1,59 @@
|
||||
/* Bounding Box 2D
|
||||
- = rectangle that completely encloses the circle (whole circle lies within the rectangle or on its boundary)
|
||||
- bounding box must be smallest possible rectangle that encloses the circle (square)
|
||||
[x] ask for x of the center of the circle
|
||||
[x] ask for y of the center of the circle
|
||||
[x] ask for radius of the circle
|
||||
[x] calculate 2D bounding box for given circle
|
||||
[x] print coordinates of 4 corners
|
||||
[x] print coordinates with 2 decimals
|
||||
[x] print as "Bounding Box - <CORNER_NAME>: (<X_COORDINATE> / <Y_COORDINATE>)"
|
||||
[x] order: TL (top-left), TR (top-right), BL (bottom-left), BR (bottom-right)
|
||||
[x] circle as typedef struct
|
||||
[x] bounding box as typedef struct
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct _circle_
|
||||
{
|
||||
float coord_x_;
|
||||
float coord_y_;
|
||||
float radius_;
|
||||
};
|
||||
|
||||
struct _bbox_
|
||||
{
|
||||
float highest_x;
|
||||
float highest_y;
|
||||
float lowest_x;
|
||||
float lowest_y;
|
||||
};
|
||||
|
||||
typedef struct _circle_ Circle;
|
||||
typedef struct _bbox_ BoundingBox;
|
||||
|
||||
int main()
|
||||
{
|
||||
Circle newcircle;
|
||||
BoundingBox newbox;
|
||||
|
||||
printf("X-coordinate of the circle's center: ");
|
||||
scanf(" %f",&newcircle.coord_x_); //input 6
|
||||
printf("Y-coordinate of the circle's center: ");
|
||||
scanf(" %f",&newcircle.coord_y_); //input 3
|
||||
printf("Radius of the circle: ");
|
||||
scanf(" %f",&newcircle.radius_); // input 5
|
||||
|
||||
newbox.highest_x = newcircle.coord_x_ + newcircle.radius_;
|
||||
newbox.highest_y = newcircle.coord_y_ + newcircle.radius_;
|
||||
newbox.lowest_x = newcircle.coord_x_ - newcircle.radius_;
|
||||
newbox.lowest_y = newcircle.coord_y_ - newcircle.radius_;
|
||||
|
||||
printf("Bounding Box - TL: (%.2f/%.2f)\n", newbox.lowest_x, newbox.highest_y);
|
||||
printf("Bounding Box - TR: (%.2f/%.2f)\n", newbox.highest_x, newbox.highest_y);
|
||||
printf("Bounding Box - BL: (%.2f/%.2f)\n", newbox.lowest_x, newbox.lowest_y);
|
||||
printf("Bounding Box - BR: (%.2f/%.2f)\n", newbox.highest_x, newbox.lowest_y);
|
||||
|
||||
return 0;
|
||||
}
|
||||
32
c/isp-ws25/weeklies/week02/w02ex02.c
Executable file
32
c/isp-ws25/weeklies/week02/w02ex02.c
Executable file
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int votes_windows;
|
||||
int votes_linux;
|
||||
|
||||
printf("Votes for Windows: ");
|
||||
scanf("%d", &votes_windows);
|
||||
|
||||
printf("Votes for Linux: ");
|
||||
//scanf("%c", &votes_linux);
|
||||
scanf("%d", &votes_linux);
|
||||
|
||||
// Compare the votes and announce the winner
|
||||
//if (votes_windows = votes_linux)
|
||||
if (votes_windows == votes_linux)
|
||||
{
|
||||
printf("It's a tie between Windows and Linux!\n");
|
||||
}
|
||||
//else (votes_windows < votes_linux)
|
||||
else if (votes_windows > votes_linux)
|
||||
{
|
||||
printf("Windows wins!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Linux wins!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
64
c/isp-ws25/weeklies/week02/w02ex03.c
Executable file
64
c/isp-ws25/weeklies/week02/w02ex03.c
Executable file
@@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
[x] Get user input as char
|
||||
[x] check if valid input ([ A- Z][ a- z])
|
||||
[x] check if valid input ([65-90][97-122])
|
||||
[x] if valid, print numerical
|
||||
[x] if valid, print ASCII
|
||||
[x] if invalid, print "[ERROR] not a valid character [A-Z] or [a-z]"
|
||||
[x] if invalid, no further output
|
||||
[x] request input repeatedly
|
||||
[x] print round number at start
|
||||
[x] if "Q" or "q", terminate
|
||||
[x] regular output when terminating
|
||||
[x] print "GOODBYE" before terminating
|
||||
*/
|
||||
|
||||
unsigned char userchar = 0;
|
||||
unsigned int counter = 1;
|
||||
|
||||
bool charcheck(unsigned char char_)
|
||||
{
|
||||
if((char_ >= 65 && char_ <= 90) || (char_ >= 97 && char_ <= 122)) // A <= %c <= Z OR a <= %c <= z
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void round_start()
|
||||
{
|
||||
printf("Round %d:\n", counter);
|
||||
counter++;
|
||||
printf("%s", "Insert character: ");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
round_start();
|
||||
scanf(" %c", &userchar);
|
||||
|
||||
if (charcheck(userchar) == true)
|
||||
{
|
||||
printf("%-17s %d\n", "Number:", userchar);
|
||||
printf("%-17s %c\n", "ASCII character:", userchar);
|
||||
//Q == 81, q == 113
|
||||
if (userchar == 81 || userchar == 113) {break;}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[ERROR] not a valid character [A-Z] or [a-z]\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
printf("GOODBYE\n");
|
||||
return 0;
|
||||
}
|
||||
58
c/isp-ws25/weeklies/week02/w02ex04.c
Executable file
58
c/isp-ws25/weeklies/week02/w02ex04.c
Executable file
@@ -0,0 +1,58 @@
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
- find mistakes in the given source code
|
||||
|
||||
[x] ask for lower limit
|
||||
[x] ask for upper limit
|
||||
[x] calculate sum of all even numbers contained
|
||||
[x] calculate average of all even numbers contained
|
||||
[x] include lower, exclude upper
|
||||
[x] error if not lower < upper: "Upper limit must be higher than lower limit\n"
|
||||
[x] terminate on error
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
//int lower_limit = 0
|
||||
int lower_limit = 0;
|
||||
int upper_limit = 0;
|
||||
|
||||
printf("Enter lower limit: ");
|
||||
scanf("%d", &lower_limit);
|
||||
|
||||
printf("Enter upper limit: ");
|
||||
//scanf("%d", upper_limit);
|
||||
scanf("%d", &upper_limit);
|
||||
|
||||
//if (lower_limit <= upper_limit)
|
||||
if (upper_limit <= lower_limit)
|
||||
{
|
||||
printf("Upper limit must be higher than lower limit\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
//int sum;
|
||||
int sum = 0;
|
||||
//int count;
|
||||
int count = 0;
|
||||
|
||||
// Loop to find the sum of even numbers
|
||||
for (int i = lower_limit; i < upper_limit; i++)
|
||||
{
|
||||
//if ((i % 2) = 0)
|
||||
if ((i % 2) == 0)
|
||||
{
|
||||
sum = sum + i;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the average of the even numbers
|
||||
//float average = count / sum;
|
||||
float average = sum / count;
|
||||
printf("Sum of even numbers: %d\n", sum);
|
||||
printf("Average of even numbers: %f\n", average);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user