diff --git a/Makefile b/Makefile index c1b3192..80e2473 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 # ----------------------- @@ -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 diff --git a/src/hello.c b/src/hello.c index 9d6338f..c49b823 100644 --- a/src/hello.c +++ b/src/hello.c @@ -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]); diff --git a/src/lab2_1.c b/src/lab2_1.c index 699339a..230857f 100644 --- a/src/lab2_1.c +++ b/src/lab2_1.c @@ -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; } diff --git a/src/lab2_2.c b/src/lab2_2.c index d88ef6d..51e0f3d 100644 --- a/src/lab2_2.c +++ b/src/lab2_2.c @@ -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; } diff --git a/src/lab2_3.c b/src/lab2_3.c index ddc8af6..c0a9fd7 100644 --- a/src/lab2_3.c +++ b/src/lab2_3.c @@ -1,3 +1,5 @@ + +#include // for sqrt function #include /* @@ -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; } diff --git a/src/lab3_task1 b/src/lab3_task1 new file mode 100755 index 0000000..3757e48 Binary files /dev/null and b/src/lab3_task1 differ diff --git a/src/lab3_task1.c b/src/lab3_task1.c index f0d73ee..318a387 100644 --- a/src/lab3_task1.c +++ b/src/lab3_task1.c @@ -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 @@ -21,8 +21,8 @@ * avg = array_avg(arr, 5); // 3.0 */ -#include #include +#include // Function prototypes int array_min(int arr[], int size); @@ -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 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 } diff --git a/src/lab3_task2 b/src/lab3_task2 new file mode 100755 index 0000000..a4f632f Binary files /dev/null and b/src/lab3_task2 differ diff --git a/src/lab3_task2.c b/src/lab3_task2.c index fcc50d8..c2799ae 100644 --- a/src/lab3_task2.c +++ b/src/lab3_task2.c @@ -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: @@ -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 } diff --git a/src/lab3_task3 b/src/lab3_task3 new file mode 100755 index 0000000..9c3b86d Binary files /dev/null and b/src/lab3_task3 differ diff --git a/src/lab3_task3.c b/src/lab3_task3.c index 55cc20f..6a4b31f 100644 --- a/src/lab3_task3.c +++ b/src/lab3_task3.c @@ -1,6 +1,6 @@ /* * Lab 3, Task 3 - * Student Name, Student ID + * Student Name:Ilham Dosdiyev, Student ID:241ADB018 * * Implement basic string handling functions. * Write your own versions of: @@ -26,25 +26,37 @@ int my_strlen(const char *str); void my_strcpy(char *dest, const char *src); int main(void) { - // TODO: Test your functions here - char test[] = "Programming in C"; - char copy[100]; + // TODO: Test your functions here + char test[] = "Programming in C"; + char copy[100]; - int len = my_strlen(test); - printf("Length: %d\n", len); + int len = my_strlen(test); + printf("Length: %d\n", len); - my_strcpy(copy, test); - printf("Copy: %s\n", copy); + my_strcpy(copy, test); + printf("Copy: %s\n", copy); - return 0; + return 0; } // Implement functions below int my_strlen(const char *str) { - // TODO: count characters until '\0' - return 0; // placeholder + // Count characters using pointer arithmetic + int count = 0; + const char *ptr = str; // point to the first character + while (*ptr != '\0') { // stop at null terminator + count++; + ptr++; // move to next character + } + return count; } void my_strcpy(char *dest, const char *src) { - // TODO: copy characters until '\0' + // Copy string using pointers + while (*src != '\0') { // until end of source + *dest = *src; // copy character + dest++; // move dest pointer + src++; // move src pointer + } + *dest = '\0'; // add null terminator } diff --git a/src/week4_1_dynamic_array.c b/src/week4_1_dynamic_array.c index 575567f..67023f7 100644 --- a/src/week4_1_dynamic_array.c +++ b/src/week4_1_dynamic_array.c @@ -4,35 +4,50 @@ * Student ID: [Your ID] * Description: * Demonstrates creation and usage of a dynamic array using malloc. - * Students should allocate memory for an integer array, fill it with data, - * compute something (e.g., average), and then free the memory. + * Program asks the user for number of elements, allocates memory dynamically, + * reads integers, computes their sum and average, and frees memory before + * exit. */ #include #include int main(void) { - int n; - int *arr = NULL; - - printf("Enter number of elements: "); - if (scanf("%d", &n) != 1 || n <= 0) { - printf("Invalid size.\n"); - return 1; + int n; + int *arr = NULL; + int sum = 0; + double average = 0.0; + + printf("Enter number of elements: "); + if (scanf("%d", &n) != 1 || n <= 0) { + printf("Invalid size.\n"); + return 1; + } + + // Allocate memory for n integers using malloc + arr = malloc(n * sizeof(int)); + if (arr == NULL) { + printf("Memory allocation failed.\n"); + return 1; + } + + printf("Enter %d integers: ", n); + for (int i = 0; i < n; i++) { + if (scanf("%d", &arr[i]) != 1) { + printf("Invalid input detected. Exiting.\n"); + free(arr); + return 1; } + sum += arr[i]; + } - // TODO: Allocate memory for n integers using malloc - // Example: arr = malloc(n * sizeof(int)); - - // TODO: Check allocation success - - // TODO: Read n integers from user input and store in array - - // TODO: Compute sum and average + average = (double)sum / n; - // TODO: Print the results + printf("Sum = %d\n", sum); + printf("Average = %.2f\n", average); - // TODO: Free allocated memory + // Free allocated memory + free(arr); - return 0; + return 0; } diff --git a/src/week4_2_struct_student.c b/src/week4_2_struct_student.c index b04fc32..ce08e62 100644 --- a/src/week4_2_struct_student.c +++ b/src/week4_2_struct_student.c @@ -4,27 +4,37 @@ * Student ID: [Your ID] * Description: * Demonstrates defining and using a struct in C. - * Students should define a 'Student' struct with fields like name, id, and grade. - * Then create a few instances and print them. + * Students should define a 'Student' struct with fields like name, id, and + * grade. Then create a few instances and print them. */ #include #include -// TODO: Define struct Student with fields: name (char[]), id (int), grade (float) -// Example: -// struct Student { -// char name[50]; -// int id; -// float grade; -// }; +// Define struct Student with name, id, and grade fields +struct Student { + char name[50]; + int id; + float grade; +}; int main(void) { - // TODO: Declare one or more Student variables + // Declare two Student variables + struct Student s1, s2; - // TODO: Assign values (either manually or via scanf) + // Assign values to the first student + strcpy(s1.name, "Alice Johnson"); + s1.id = 1001; + s1.grade = 9.1; - // TODO: Print struct contents using printf + // Assign values to the second student + strcpy(s2.name, "Bob Smith"); + s2.id = 1002; + s2.grade = 8.7; - return 0; + // Print student information in a clear format + printf("Student 1: %s, ID: %d, Grade: %.1f\n", s1.name, s1.id, s1.grade); + printf("Student 2: %s, ID: %d, Grade: %.1f\n", s2.name, s2.id, s2.grade); + + return 0; } diff --git a/src/week4_3_struct_database.c b/src/week4_3_struct_database.c index 663b998..c7c2fe3 100644 --- a/src/week4_3_struct_database.c +++ b/src/week4_3_struct_database.c @@ -12,27 +12,58 @@ #include #include -// TODO: Define struct Student with fields name, id, grade +// Define struct Student with fields name, id, grade +struct Student { + char name[50]; + int id; + float grade; +}; int main(void) { - int n; - struct Student *students = NULL; + int n; + struct Student *students = NULL; - printf("Enter number of students: "); - if (scanf("%d", &n) != 1 || n <= 0) { - printf("Invalid number.\n"); - return 1; - } + printf("Enter number of students: "); + if (scanf("%d", &n) != 1 || n <= 0) { + printf("Invalid number.\n"); + return 1; + } - // TODO: Allocate memory for n Student structs using malloc + // Allocate memory for n Student structs using malloc + students = malloc(n * sizeof(struct Student)); + if (students == NULL) { + printf("Memory allocation failed.\n"); + return 1; + } - // TODO: Read student data in a loop + // Read student data in a loop + for (int i = 0; i < n; i++) { + printf("Enter data for student %d (Name ID Grade): ", i + 1); + if (scanf("%49s %d %f", students[i].name, &students[i].id, + &students[i].grade) != 3) { + printf("Invalid input.\n"); + free(students); + return 1; + } + } - // TODO: Display all student records in formatted output + // Display all student records in formatted output + printf("\n%-8s %-15s %-6s\n", "ID", "Name", "Grade"); + printf("---------------------------------\n"); + for (int i = 0; i < n; i++) { + printf("%-8d %-15s %.2f\n", students[i].id, students[i].name, + students[i].grade); + } - // Optional: Compute average grade or find top student + // Optional: Compute average grade (example) + float total = 0.0f; + for (int i = 0; i < n; i++) { + total += students[i].grade; + } + printf("\nAverage grade: %.2f\n", total / n); - // TODO: Free allocated memory + // Free allocated memory + free(students); - return 0; + return 0; }