Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ all: $(PROGRAMS)
# -----------------------
# Lab 1
# -----------------------
lab1: $(BUILD_DIR)/hello $(BUILD_DIR)/calculator $(BUILD_DIR)/formats

$(BUILD_DIR)/hello: $(SRC_DIR)/hello.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
Expand All @@ -29,11 +27,13 @@ $(BUILD_DIR)/formats: $(SRC_DIR)/format_specifiers.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

hello: $(BUILD_DIR)/hello
calculator: $(BUILD_DIR)/calculator
formats: $(BUILD_DIR)/formats

# -----------------------
# Lab 2
# -----------------------
lab2: $(BUILD_DIR)/lab2_1 $(BUILD_DIR)/lab2_2 $(BUILD_DIR)/lab2_3

$(BUILD_DIR)/lab2_1: $(SRC_DIR)/lab2_1.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
Expand All @@ -46,11 +46,11 @@ $(BUILD_DIR)/lab2_3: $(SRC_DIR)/lab2_3.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

lab2: $(BUILD_DIR)/lab2_1 $(BUILD_DIR)/lab2_2 $(BUILD_DIR)/lab2_3

# -----------------------
# Lab 3
# -----------------------
lab3: $(BUILD_DIR)/lab3_task1 $(BUILD_DIR)/lab3_task2 $(BUILD_DIR)/lab3_task3

$(BUILD_DIR)/lab3_task1: $(SRC_DIR)/lab3_task1.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
Expand All @@ -63,11 +63,11 @@ $(BUILD_DIR)/lab3_task3: $(SRC_DIR)/lab3_task3.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

lab3: $(BUILD_DIR)/lab3_task1 $(BUILD_DIR)/lab3_task2 $(BUILD_DIR)/lab3_task3

# -----------------------
# Lab 4
# -----------------------
lab4: $(BUILD_DIR)/week4_1_dynamic_array $(BUILD_DIR)/week4_2_struct_student $(BUILD_DIR)/week4_3_struct_database

$(BUILD_DIR)/week4_1_dynamic_array: $(SRC_DIR)/week4_1_dynamic_array.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
Expand All @@ -80,6 +80,8 @@ $(BUILD_DIR)/week4_3_struct_database: $(SRC_DIR)/week4_3_struct_database.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

lab4: $(BUILD_DIR)/week4_1_dynamic_array $(BUILD_DIR)/week4_2_struct_student $(BUILD_DIR)/week4_3_struct_database

# -----------------------
# Run targets
# -----------------------
Expand Down Expand Up @@ -119,25 +121,21 @@ run-week4_2_struct_student: $(BUILD_DIR)/week4_2_struct_student
run-week4_3_struct_database: $(BUILD_DIR)/week4_3_struct_database
./$(BUILD_DIR)/week4_3_struct_database

# Run all tasks in Lab 2
run-lab2: lab2
./$(BUILD_DIR)/lab2_1
./$(BUILD_DIR)/lab2_2
./$(BUILD_DIR)/lab2_3

# Run all tasks in Lab 3
run-lab3: lab3
./$(BUILD_DIR)/lab3_task1
./$(BUILD_DIR)/lab3_task2
./$(BUILD_DIR)/lab3_task3

# Run all tasks in Lab 4
run-lab4: lab4
./$(BUILD_DIR)/week4_1_dynamic_array
./$(BUILD_DIR)/week4_2_struct_student
./$(BUILD_DIR)/week4_3_struct_database

# Run all labs (Lab 1 → Lab 4)
run-all: all
./$(BUILD_DIR)/hello
./$(BUILD_DIR)/calculator
Expand Down
2 changes: 1 addition & 1 deletion src/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

int main(int argc, char *argv[]) {
// This is your first C program my friend
printf("Hello, RTU World from C Lab in 2025!\n");
printf("Hello from Ilham! \n");
printf("You passed %d argument(s).\n", argc - 1);
for (int i = 1; i < argc; ++i) {
printf(" arg[%d] = %s\n", i, argv[i]);
Expand Down
31 changes: 19 additions & 12 deletions src/lab2_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@
Task:
Write a function `int sum_to_n(int n)` that computes
the sum of all integers from 1 up to n using a for loop.

In main():
- Ask user for a positive integer n
- If n < 1, print an error
- Otherwise, call sum_to_n and print the result
*/

int sum_to_n(int n) {
// TODO: implement sum with a for loop
return 0; // placeholder
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

int main(void) {
int n;
int n;

printf("Enter a positive integer n: ");
if (scanf("%d", &n) != 1) {
printf("Error: Invalid input.\n");
return 1;
}

printf("Enter a positive integer n: ");
scanf("%d", &n);
if (n < 1) {
printf("Error: n must be >= 1\n");
return 1;
}

// TODO: validate input, call function, and print result
int result = sum_to_n(n);
printf("Sum from 1 to %d = %d\n", n, result);

return 0;
return 0;
}
38 changes: 30 additions & 8 deletions src/lab2_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,43 @@
In main():
- Ask user for an integer n
- If n is negative, print an error and exit
- Otherwise, call factorial and print the result
- Otherwise, call factorial and print the result.
*/

long long factorial(int n) {
// TODO: compute factorial iteratively
return 1; // placeholder
long long result = 1;
for (int i = 1; i <= n; i++) {
result *= i; // faktorial: n * (n-1) * ... * 1
}
return result;
}

int main(void) {
int n;
int n;

printf("Enter a non-negative integer n: ");
scanf("%d", &n);
printf("Enter a non-negative integer n: ");
scanf("%d", &n);

// TODO: validate input, call function, print result
if (n < 0) {
printf("Error: n must be >= 0\n");
} else {
long long fact = factorial(n);
printf("%d! = %lld\n", n, fact);
}

return 0;
return 0;

// TODO: compute factorial iteratively
return 1; // placeholder
}

int main(void) {
int n;

printf("Enter a non-negative integer n: ");
scanf("%d", &n);

// TODO: validate input, call function, print result

return 0;
}
47 changes: 40 additions & 7 deletions src/lab2_3.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

#include <math.h> // for sqrt function
#include <stdio.h>

/*
Expand All @@ -12,17 +14,48 @@
*/

int is_prime(int n) {
// TODO: check if n is prime using loop up to sqrt(n)
return 0; // placeholder
// TODO: check if n is prime using loop up to sqrt(n)
if (n < 2) return 0; // numbers less than 2 are not prime
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0; // found a divisor → not prime
}
}
return 1; // no divisors found → prime
}

int main(void) {
int n;

printf("Enter an integer n (>= 2): ");
scanf("%d", &n);

// TODO: validate input and print all primes up to n
if (n < 2) {
printf("Error: n must be >= 2\n");
return 1;
}

printf("Prime numbers up to %d:\n", n);
for (int i = 2; i <= n; i++) {
if (is_prime(i)) {
printf("%d ", i);
}
}
printf("\n");

return 0;
// TODO: check if n is prime using loop up to sqrt(n)
return 0; // placeholder
}

int main(void) {
int n;
int n;

printf("Enter an integer n (>= 2): ");
scanf("%d", &n);
printf("Enter an integer n (>= 2): ");
scanf("%d", &n);

// TODO: validate input and print all primes up to n
// TODO: validate input and print all primes up to n.

return 0;
return 0;
}
Binary file added src/lab3_task1
Binary file not shown.
52 changes: 30 additions & 22 deletions src/lab3_task1.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Lab 3, Task 1
* Student Name, Student ID
* Student Name:Ilham Dosdiyev, Student ID: 241ADB018
*
* Implement array algorithms:
* - find minimum value
Expand All @@ -21,8 +21,8 @@
* avg = array_avg(arr, 5); // 3.0
*/

#include <stdio.h>
#include <limits.h>
#include <stdio.h>

// Function prototypes
int array_min(int arr[], int size);
Expand All @@ -31,40 +31,48 @@ int array_sum(int arr[], int size);
float array_avg(int arr[], int size);

int main(void) {
int arr[] = {10, 20, 5, 30, 15};
int size = 5;
int arr[] = {10, 20, 5, 30, 15};
int size = 5;

printf("Min: %d\n", array_min(arr, size));
printf("Max: %d\n", array_max(arr, size));
printf("Sum: %d\n", array_sum(arr, size));
printf("Avg: %.2f\n", array_avg(arr, size));
printf("Min: %d\n", array_min(arr, size));
printf("Max: %d\n", array_max(arr, size));
printf("Sum: %d\n", array_sum(arr, size));
printf("Avg: %.2f\n", array_avg(arr, size));

return 0;
return 0;
}

// Implement functions below
int array_min(int arr[], int size) {
// TODO: return smallest element
int my_min = INT_MAX; //set to max infinity
for (int i=0;i<size;i++) {
if (my_min > arr[i]) {
my_min = arr[i];
}
// TODO: return smallest element
int my_min = INT_MAX; // set to max infinity
for (int i = 0; i < size; i++) {
if (my_min > arr[i]) {
my_min = arr[i];
}
return my_min; // placeholder
}
return my_min;
}

int array_max(int arr[], int size) {
// TODO: return largest element
return 0; // placeholder
int max_num = INT_MIN;
for (int i = 0; i < size; i++) {
if (arr[i] > max_num) {
max_num = arr[i];
}
}
return max_num;
}

int array_sum(int arr[], int size) {
// TODO: return sum of elements
return 0; // placeholder
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

float array_avg(int arr[], int size) {
// TODO: return average as float
return 0.0f; // placeholder
int sum = array_sum(arr, size);
return (float)sum / size; // cast to float for decimal average
}
Binary file added src/lab3_task2
Binary file not shown.
25 changes: 15 additions & 10 deletions src/lab3_task2.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Lab 3, Task 2
* Student Name, Student ID
* Student Name: Ilham Dosdiyev, Student ID:241ADB018
*
* Practice using pointers as function parameters.
* Implement:
Expand All @@ -25,22 +25,27 @@ void swap(int *x, int *y);
void modify_value(int *x);

int main(void) {
int a = 3, b = 7;
printf("Before swap: a=%d, b=%d\n", a, b);
swap(&a, &b);
printf("After swap: a=%d, b=%d\n", a, b);
int a = 3, b = 7;
printf("Before swap: a=%d, b=%d\n", a, b);
swap(&a, &b);
printf("After swap: a=%d, b=%d\n", a, b);

modify_value(&a);
printf("After modify_value: a=%d\n", a);
modify_value(&a);
printf("After modify_value: a=%d\n", a);

return 0;
return 0;
}

// Implement functions below

// Swap two integers using pointers
void swap(int *x, int *y) {
// TODO: swap values using a temporary variable
int temp = *x; // keep the value of x in a temporary variable
*x = *y; // assign the value of y to x
*y = temp; // assign the original value of x to y
}

// Multiply the value by 2 using a pointer
void modify_value(int *x) {
// TODO: multiply value by 2
*x = (*x) * 2; // multiply the value that x points to by 2
}
Binary file added src/lab3_task3
Binary file not shown.
Loading