initial commit
This commit is contained in:
8 files changed
+250
No files matched your search
@@ -0,0 +1,26 @@
|
|||||||
|
# Makefile
|
||||||
|
# Compiler and flags
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -std=c11
|
||||||
|
|
||||||
|
# Source files
|
||||||
|
SRCS = hello.c
|
||||||
|
|
||||||
|
# Output
|
||||||
|
TARGET = hello.out
|
||||||
|
|
||||||
|
# Default target
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
# Rule to build the executable
|
||||||
|
$(TARGET): $(SRCS)
|
||||||
|
$(CC) $(CFLAGS) $(SRCS) -o $(TARGET)
|
||||||
|
|
||||||
|
# Rule to run the program
|
||||||
|
run: $(TARGET)
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
# Rule to clean generated files
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET)
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("Hello World\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,25 @@
|
|||||||
|
# Makefile
|
||||||
|
# Compiler and flags
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -std=c11
|
||||||
|
|
||||||
|
# Source files
|
||||||
|
SRCS = lab2.c
|
||||||
|
|
||||||
|
# Output
|
||||||
|
TARGET = lab2.out
|
||||||
|
|
||||||
|
# Default target
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
# Rule to build the executable
|
||||||
|
$(TARGET): $(SRCS)
|
||||||
|
$(CC) $(CFLAGS) $(SRCS) -o $(TARGET)
|
||||||
|
|
||||||
|
# Rule to run the program
|
||||||
|
run: $(TARGET)
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
# Rule to clean generated files
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET)
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void multiplicationTable(int n) {
|
||||||
|
printf("Multiplication table of %d\n", n);
|
||||||
|
|
||||||
|
for (int i = 1; i <= 10; i++) {
|
||||||
|
printf("%d x %d = %d\n", n, i, n * i);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void numberAnalysis(int n) {
|
||||||
|
int c = (n > 0) - (n < 0);
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
|
case -1: // n is negative
|
||||||
|
multiplicationTable(n);
|
||||||
|
|
||||||
|
printf("Sum not applicable for negative numbers\n");
|
||||||
|
printf("The number is Negative.\n");
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 0: // n is zero
|
||||||
|
multiplicationTable(n);
|
||||||
|
|
||||||
|
printf("Sum not applicable for zero\n");
|
||||||
|
printf("The number is Zero.\n");
|
||||||
|
|
||||||
|
break;
|
||||||
|
default: // n is positive
|
||||||
|
multiplicationTable(n);
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
int current = 0;
|
||||||
|
while (current <= n) {
|
||||||
|
sum += current;
|
||||||
|
current++;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Sum of 1 to %d = %d\n", n, sum);
|
||||||
|
printf("The number is Positive.\n");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int number;
|
||||||
|
|
||||||
|
printf("Enter a number: ");
|
||||||
|
scanf("%d", &number);
|
||||||
|
|
||||||
|
numberAnalysis(number);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
+135
@@ -0,0 +1,135 @@
|
|||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define NUMBER_PARTICLES 100 // 100
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int lastX;
|
||||||
|
int lastY;
|
||||||
|
int velocityX;
|
||||||
|
int velocityY;
|
||||||
|
} PARTICLE;
|
||||||
|
|
||||||
|
void render(char **space, int WIDTH, int HEIGHT) {
|
||||||
|
system("clear");
|
||||||
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
|
printf("%c", space[y][x]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateParticles(PARTICLE *particles, int WIDTH, int HEIGHT) {
|
||||||
|
for (int i = 0; i < NUMBER_PARTICLES; i++) {
|
||||||
|
PARTICLE p = particles[i];
|
||||||
|
|
||||||
|
p.lastX = p.x;
|
||||||
|
p.lastY = p.y;
|
||||||
|
p.x = p.x + p.velocityX;
|
||||||
|
p.y = p.y + p.velocityY;
|
||||||
|
|
||||||
|
if (p.x <= 0 || p.x >= WIDTH - 1) {
|
||||||
|
p.velocityX *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p.y <= 0 || p.y >= HEIGHT - 1) {
|
||||||
|
p.velocityY *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
particles[i] = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void update2DSpace(PARTICLE *particles, char **space) {
|
||||||
|
for (int i = 0; i < NUMBER_PARTICLES; i++) {
|
||||||
|
PARTICLE p = particles[i];
|
||||||
|
|
||||||
|
space[p.y][p.x] = '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char** create2DSpace(int WIDTH, int HEIGHT) {
|
||||||
|
char **space = malloc(HEIGHT * sizeof(char*));
|
||||||
|
if (space == NULL) {
|
||||||
|
perror("Failed to allocate space");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < HEIGHT; i++) {
|
||||||
|
|
||||||
|
space[i] = malloc(WIDTH * sizeof(char));
|
||||||
|
|
||||||
|
if (space[i] == NULL) {
|
||||||
|
perror("Failed to allocate space");
|
||||||
|
|
||||||
|
for (int j = 0; j < HEIGHT; j++) {
|
||||||
|
free(space[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(space);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < HEIGHT; i++) {
|
||||||
|
for (int j = 0; j < WIDTH; j++) {
|
||||||
|
space[i][j] = ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return space;
|
||||||
|
}
|
||||||
|
|
||||||
|
PARTICLE* generateParticles(int WIDTH, int HEIGHT) {
|
||||||
|
PARTICLE *particles = malloc(NUMBER_PARTICLES * sizeof(PARTICLE));
|
||||||
|
|
||||||
|
if (particles == NULL) {
|
||||||
|
perror("Failed to allocate memory for particles");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
int blockWidth = sqrt((float) NUMBER_PARTICLES);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
for (int i = -(blockWidth / 2); i < blockWidth / 2; i++) {
|
||||||
|
for (int j = -(blockWidth / 2); j < blockWidth / 2; j++) {
|
||||||
|
// printf("%d - (%d, %d)\n", index, WIDTH / 2 + i, HEIGHT / 2 + j);
|
||||||
|
PARTICLE p = {WIDTH / 2 + i, HEIGHT / 2 + j, WIDTH / 2 + i, HEIGHT / 2 + j, 0, 1};
|
||||||
|
particles[index] = p;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return particles;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
struct winsize w;
|
||||||
|
ioctl(0, TIOCGWINSZ, &w);
|
||||||
|
|
||||||
|
int HEIGHT = w.ws_row;
|
||||||
|
int WIDTH = w.ws_col;
|
||||||
|
|
||||||
|
char **space = create2DSpace(WIDTH, HEIGHT);
|
||||||
|
|
||||||
|
PARTICLE *particles = generateParticles(WIDTH, HEIGHT);
|
||||||
|
|
||||||
|
// return 0;
|
||||||
|
int micro = 100000;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
updateParticles(particles, WIDTH, HEIGHT);
|
||||||
|
space = create2DSpace(WIDTH, HEIGHT); // resset grid space
|
||||||
|
update2DSpace(particles, space);
|
||||||
|
render(space, WIDTH, HEIGHT);
|
||||||
|
usleep(0.5 * micro);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user