diff --git a/src/hello.c b/src/hello.c index 9d6338f..da97572 100644 --- a/src/hello.c +++ b/src/hello.c @@ -3,7 +3,8 @@ 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 Murad! \n"); + printf("Hello, RTU World from C Lab in 2025!!!!\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 b/src/lab2_1 new file mode 100755 index 0000000..893fb3a Binary files /dev/null and b/src/lab2_1 differ diff --git a/src/lab2_1.c b/src/lab2_1.c index 699339a..c3b1d32 100644 --- a/src/lab2_1.c +++ b/src/lab2_1.c @@ -1,3 +1,4 @@ +// Name : Murad Hashimov , id : 241ADB148 #include /* @@ -12,17 +13,26 @@ */ int sum_to_n(int n) { - // TODO: implement sum with a for loop - return 0; // placeholder + int inp = 0; + for (int i = 1; i <= n; i++) { + inp = inp + i; + } + return inp; // placeholder } int main(void) { - int n; + int n; - printf("Enter a positive integer n: "); - scanf("%d", &n); + printf("Enter a positive integer n: "); + scanf("%d", &n); + if (n < 1) { + printf("Error"); + } else { + int result = sum_to_n(n); + printf("Result %d\n", result); + } - // TODO: validate input, call function, and print result + // TODO: validate input, call function, and print result - return 0; + return 0; } diff --git a/src/lab2_2 b/src/lab2_2 new file mode 100755 index 0000000..316e49d Binary files /dev/null and b/src/lab2_2 differ diff --git a/src/lab2_2.c b/src/lab2_2.c index d88ef6d..5ea138a 100644 --- a/src/lab2_2.c +++ b/src/lab2_2.c @@ -1,3 +1,5 @@ +// Name : Murad Hashimov , id : 241ADB148 + #include /* @@ -12,17 +14,30 @@ */ long long factorial(int n) { - // TODO: compute factorial iteratively - return 1; // placeholder + int a = 1; + int res = 1; + + while (a <= n) { + res = res * a; + a++; + } + return res; // placeholder } 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); + if (n < 0) { + printf("Error , cause it is negative"); + } else { + int res = factorial(n); + printf("Factorial of %d is %d", n, res); + } - // TODO: validate input, call function, print result + // TODO: validate input, call function, print result - return 0; + return 0; } diff --git a/src/lab2_3.c b/src/lab2_3.c index ddc8af6..a7d45de 100644 --- a/src/lab2_3.c +++ b/src/lab2_3.c @@ -1,3 +1,6 @@ +// Name : Murad Hashimov , id : 241ADB148 + +#include #include /* @@ -12,17 +15,28 @@ */ int is_prime(int n) { - // TODO: check if n is prime using loop up to sqrt(n) - return 0; // placeholder + for (int i = 2; i <= sqrt(n); i++) { + if (n % i == 0) return 0; + } + + return 1; } int main(void) { - int n; - - printf("Enter an integer n (>= 2): "); - scanf("%d", &n); + int n; - // TODO: validate input and print all primes up to n + printf("Enter an integer n (>= 2): "); + scanf("%d", &n); - return 0; + if (n < 2) { + printf("Error"); + } else { + printf("Prime numbers - %d are:\n", n); + for (int i = 2; i <= n; i++) { + if (is_prime(i)) { + printf("%d ", i); + } + } + } + return 0; } diff --git a/src/lab3_task1 b/src/lab3_task1 new file mode 100755 index 0000000..25f9889 Binary files /dev/null and b/src/lab3_task1 differ diff --git a/src/lab3_task1.c b/src/lab3_task1.c index f0d73ee..d88d1de 100644 --- a/src/lab3_task1.c +++ b/src/lab3_task1.c @@ -1,6 +1,6 @@ /* * Lab 3, Task 1 - * Student Name, Student ID + * Murad Hashimov, 241ADB148 * * 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,51 @@ 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]; - } + int my_min = INT_MAX; + for (int i = 1; i < size; i++) { + if (arr[i] < my_min) { + my_min = arr[i]; } - return my_min; // placeholder + } + return my_min; // placeholder } int array_max(int arr[], int size) { - // TODO: return largest element - return 0; // placeholder + int my_max = INT_MIN; + for (int i = 1; i < size; i++) { + if (arr[i] > my_max) { + my_max = arr[i]; + } + } + return my_max; // placeholder } int array_sum(int arr[], int size) { - // TODO: return sum of elements - return 0; // placeholder + int my_sum = 0; + for (int i = 0; i < size; i++) { + my_sum = my_sum + arr[i]; + } + return my_sum; // placeholder } float array_avg(int arr[], int size) { - // TODO: return average as float - return 0.0f; // placeholder + float my_sum = 0.0; + for (int i = 0; i < size; i++) { + my_sum = my_sum + arr[i]; + } + float my_avg = my_sum / size; + return my_avg; // placeholder } 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..8a29484 100644 --- a/src/lab3_task2.c +++ b/src/lab3_task2.c @@ -1,6 +1,6 @@ /* * Lab 3, Task 2 - * Student Name, Student ID + * Murad Hashimov, 241ADB148 * * Practice using pointers as function parameters. * Implement: @@ -38,9 +38,11 @@ int main(void) { // Implement functions below void swap(int *x, int *y) { - // TODO: swap values using a temporary variable + int val = *x; + *x = *y; + *y = val; } void modify_value(int *x) { - // TODO: multiply value by 2 + *x = (*x)*2; } diff --git a/src/lab3_task3 b/src/lab3_task3 new file mode 100755 index 0000000..17163ad Binary files /dev/null and b/src/lab3_task3 differ diff --git a/src/lab3_task3.c b/src/lab3_task3.c index 55cc20f..975d695 100644 --- a/src/lab3_task3.c +++ b/src/lab3_task3.c @@ -1,6 +1,6 @@ /* * Lab 3, Task 3 - * Student Name, Student ID + * Murad Hashimov, 241ADB148 * * Implement basic string handling functions. * Write your own versions of: @@ -41,10 +41,20 @@ int main(void) { // Implement functions below int my_strlen(const char *str) { - // TODO: count characters until '\0' - return 0; // placeholder + int size = 0; + while(str[size] != '\0'){ + size = size + 1; + } + return size; // placeholder } void my_strcpy(char *dest, const char *src) { - // TODO: copy characters until '\0' + int val = 0; + while (src[val]!='\0'){ + + dest[val] = src[val]; + + val++; + } + dest[val] = '\0'; } diff --git a/src/rfse.txt b/src/rfse.txt new file mode 100644 index 0000000..df7cc2f --- /dev/null +++ b/src/rfse.txt @@ -0,0 +1,2 @@ +Vote for Murad Hashimov! +RTU is top G diff --git a/src/student.txt b/src/student.txt new file mode 100644 index 0000000..9f934f3 --- /dev/null +++ b/src/student.txt @@ -0,0 +1,3 @@ +Alice +21 +3.75 diff --git a/src/students.txt b/src/students.txt new file mode 100644 index 0000000..13860ff --- /dev/null +++ b/src/students.txt @@ -0,0 +1,2 @@ +2425 Murad + 4.500000 diff --git a/src/week4_1_dynamic_array b/src/week4_1_dynamic_array new file mode 100755 index 0000000..1187425 Binary files /dev/null and b/src/week4_1_dynamic_array differ diff --git a/src/week4_1_dynamic_array.c b/src/week4_1_dynamic_array.c index 575567f..e701e59 100644 --- a/src/week4_1_dynamic_array.c +++ b/src/week4_1_dynamic_array.c @@ -1,7 +1,7 @@ /* * week4_1_dynamic_array.c - * Author: [Your Name] - * Student ID: [Your ID] + * Author: [Murad Hashimov] + * Student ID: [241ADB148] * Description: * Demonstrates creation and usage of a dynamic array using malloc. * Students should allocate memory for an integer array, fill it with data, @@ -12,6 +12,7 @@ #include int main(void) { + int summ; int n; int *arr = NULL; @@ -22,17 +23,37 @@ int main(void) { } // TODO: Allocate memory for n integers using malloc + arr = malloc(n * sizeof(int)); // Example: arr = malloc(n * sizeof(int)); // TODO: Check allocation success + if (arr == 0) { + printf("\nMemory allocation is false!"); + return 1; + } // TODO: Read n integers from user input and store in array - + printf("Put %d integers into array - \n", n); + for (int i = 0; i < n; i++) { + if (scanf("%d", &arr[i]) != 1) { + printf("Invalid input.\n"); + free(arr); + return 1; + } + } // TODO: Compute sum and average + for (int i = 0; i < n; i++) { + summ = summ + arr[i]; + } + float average = (float)summ / n; // TODO: Print the results + printf("The sum of values - %d \n", summ); + + printf("The average of values - %f \n", average); // TODO: Free allocated memory + free(arr); return 0; } diff --git a/src/week4_2_struct_student b/src/week4_2_struct_student new file mode 100755 index 0000000..f2da187 Binary files /dev/null and b/src/week4_2_struct_student differ diff --git a/src/week4_2_struct_student.c b/src/week4_2_struct_student.c index b04fc32..3825507 100644 --- a/src/week4_2_struct_student.c +++ b/src/week4_2_struct_student.c @@ -1,7 +1,7 @@ /* * week4_2_struct_student.c - * Author: [Your Name] - * Student ID: [Your ID] + * Author: [Murad Hashimov] + * Student ID: [241ADB148] * Description: * Demonstrates defining and using a struct in C. * Students should define a 'Student' struct with fields like name, id, and grade. @@ -13,18 +13,32 @@ // TODO: Define struct Student with fields: name (char[]), id (int), grade (float) // Example: -// struct Student { -// char name[50]; -// int id; -// float grade; -// }; +struct Student { + char name[50]; + int id; + float grade; +}; int main(void) { + struct Student student_1; + + struct Student student_2; + // TODO: Declare one or more Student variables + strcpy(student_1.name, "Murad Hashimov"); + student_1.id = 241148; + student_1.grade = 80.0; - // TODO: Assign values (either manually or via scanf) + // student_2 + strcpy(student_2.name, "Bombo Clad"); + student_2.id = 241567; + student_2.grade = 95; - // TODO: Print struct contents using printf + + printf("Student 1: Name: %s\n, ID: %d\n, Grade: %f\n", student_1.name, student_1.id, student_1.grade); + // + printf("Student 2: Name: %s\n, ID: %d\n, Grade: %f\n", student_2.name, student_2.id, student_2.grade); + return 0; } diff --git a/src/week4_3_struct_database b/src/week4_3_struct_database new file mode 100755 index 0000000..9da4212 Binary files /dev/null and b/src/week4_3_struct_database differ diff --git a/src/week4_3_struct_database.c b/src/week4_3_struct_database.c index 663b998..fd184ec 100644 --- a/src/week4_3_struct_database.c +++ b/src/week4_3_struct_database.c @@ -13,26 +13,61 @@ #include // TODO: 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; - printf("Enter number of students: "); + printf("Write how many students you want to add: "); + if (scanf("%d", &n) != 1 || n <= 0) { - printf("Invalid number.\n"); + + printf("Error: Invalid number.\n"); + return 1; } // TODO: Allocate memory for n Student structs using malloc + students = malloc( n * sizeof(struct Student) ); + + if (students == NULL) { + + printf("Error: Memory allocation failed. \n"); + + return 1; + } + // TODO: Read student data in a loop + for (int i = 0; i < n; i++) { + printf("Enter data for student %d: ", i + 1); + // Example input format: Name ID Grade + scanf("%s %d %f", students[i].name, &students[i].id, &students[i].grade); + } + // TODO: Display all student records in formatted output + printf("\n%-10s %-20s %-10s\n", "ID", "Name", "Grade"); + for (int i = 0; i < n; i++) { + printf("%-10d %-20s %-6.2f\n", students[i].id, students[i].name, students[i].grade); + } + // Optional: Compute average grade or find top student + float total = 0; + for (int i = 0; i < n; i++) { + total += students[i].grade; + } + printf("\nAverage grade: %f\n", total / n); + // TODO: Free allocated memory + free(students); return 0; } diff --git a/src/week5_task1_file_io b/src/week5_task1_file_io new file mode 100755 index 0000000..db4d3eb Binary files /dev/null and b/src/week5_task1_file_io differ diff --git a/src/week5_task1_file_io.c b/src/week5_task1_file_io.c index 336e21a..3f2eb57 100644 --- a/src/week5_task1_file_io.c +++ b/src/week5_task1_file_io.c @@ -7,21 +7,46 @@ #include int main(void) { - FILE *fp; - char filename[100] = "data.txt"; - char line[256]; + FILE *fp; + char filename[12] = "data.txt"; + char line[256]; + int val_line = 0; - // TODO: 1. Open file for writing (mode = "w") - // TODO: 2. Check if file opened successfully - // TODO: 3. Write 2–3 lines of text to the file using fprintf() - // TODO: 4. Close the file + // BONUS: ask user for filename instead of using default "data.txt" + printf("Write down the name of the file - "); + scanf("%11s", filename); - // TODO: 5. Open file again for reading (mode = "r") - // TODO: 6. Use fgets() in a loop to read and print each line to the console - // TODO: 7. Close the file + // TODO: 1. Open file for writing (mode = "w") + fp = fopen(filename, "w"); + // TODO: 2. Check if file opened successfully + if (fp == NULL) { + return 1; + } + // TODO: 3. Write 2–3 lines of text to the file using fprintf() + fprintf(fp, "Vote for Murad Hashimov! \n"); + fprintf(fp, "RTU is top G \n"); + // TODO: 4. Close the file + fclose(fp); - // BONUS: ask user for filename instead of using default "data.txt" - // BONUS: count number of lines read + // TODO: 5. Open file again for reading (mode = "r") + fp = fopen(filename, "r"); - return 0; + if (fp == NULL) { + return 1; + ; + } + + // TODO: 6. Use fgets() in a loop to read and print each line to the console + for (; fgets(line, sizeof(line), fp) != NULL; val_line++) { + printf("%s", line); + } + + // BONUS: count number of lines read + printf("Amount of lines read - %d \n", val_line); + + // TODO: 7. Close the file + + fclose(fp); + + return 0; } diff --git a/src/week5_task2_struct_save_load b/src/week5_task2_struct_save_load new file mode 100755 index 0000000..276e87a Binary files /dev/null and b/src/week5_task2_struct_save_load differ diff --git a/src/week5_task2_struct_save_load.c b/src/week5_task2_struct_save_load.c index 267dc11..e6b759a 100644 --- a/src/week5_task2_struct_save_load.c +++ b/src/week5_task2_struct_save_load.c @@ -28,8 +28,11 @@ int main(void) { const char *filename = "student.txt"; // TODO: Call save_student() to save student data to file + save_student(s1, filename); // TODO: Call load_student() to read data back into a new struct + Student s2 = load_student(filename); // TODO: Print loaded data to confirm correctness + printf("Name :%s , Age: %d , GPA: %.2f ", s2.name, s2.age, s2.gpa); return 0; } @@ -37,13 +40,48 @@ int main(void) { // TODO: Implement save_student() // Open file for writing, check errors, write fields, then close file void save_student(Student s, const char *filename) { - // ... + FILE *fp = fopen(filename, "w"); + if (fp == NULL) { + printf("Error opening file"); + exit(EXIT_FAILURE); + } + + + fprintf(fp, "%s\n%d\n%.2f\n", s.name, s.age, s.gpa); + fclose(fp); } // TODO: Implement load_student() // Open file for reading, check errors, read fields, then close file Student load_student(const char *filename) { - Student s; - // ... - return s; + Student stud; + FILE *fp = fopen(filename, "r"); + + if (fp == NULL) { + printf("Could not open file\n"); + exit(EXIT_FAILURE); + } + + + if (fgets(stud.name, MAX_NAME_LEN, fp) == NULL) { + fprintf(stderr, "Error name \n"); + fclose(fp); + exit(EXIT_FAILURE); + } + + + if (fscanf(fp, "%d", &stud.age) != 1) { + fprintf(stderr, "Error age \n"); + fclose(fp); + exit(EXIT_FAILURE); + } + + if (fscanf(fp, "%f", &stud.gpa) != 1) { + fprintf(stderr, "Error GPA \n"); + fclose(fp); + exit(EXIT_FAILURE); + } + fclose(fp); + + return stud; } diff --git a/src/week5_task3_student_management_system b/src/week5_task3_student_management_system new file mode 100755 index 0000000..eaa7358 Binary files /dev/null and b/src/week5_task3_student_management_system differ diff --git a/src/week5_task3_student_management_system.c b/src/week5_task3_student_management_system.c index 6ed6dfd..1a4e503 100644 --- a/src/week5_task3_student_management_system.c +++ b/src/week5_task3_student_management_system.c @@ -41,16 +41,16 @@ int main(void) { switch (choice) { case 1: - // TODO: Call list_students() + list_students(students, count); break; case 2: - // TODO: Call add_student() + add_student(students, &count); break; case 3: - // TODO: Call save_students() and exit loop + save_students(students, count); break; default: - printf("Invalid option. Try again.\n"); + printf("Error input data, try again\n"); } } while (choice != 3); @@ -60,24 +60,80 @@ int main(void) { // TODO: Implement load_students() // Open DATA_FILE, read records until EOF, return number of records loaded int load_students(Student arr[]) { - // ... - return 0; + int c = 0; + FILE *fp = fopen(DATA_FILE, "r"); + if(fp ==NULL){ + return 0; + } + + while(c= MAX_STUDENTS){ + printf("Too much students"); + + return; + } + + Student studs; + + printf("Enter student ID: "); + scanf("%d", &studs.id); + getchar(); + + printf("Type student's name: "); + fgets(studs.name, NAME_LEN, stdin); + + printf("Enter GPA: "); + scanf("%f", &studs.gpa); + + arr[*count] = studs; + + (*count)++; + + printf("students were added"); } // TODO: Implement list_students() // Print all students in readable format void list_students(Student arr[], int count) { - // ... + if (count == 0){ + printf("There is no students in the list\n"); + return; + } + + printf("\n%-7s %-10s %-3s\n", "ID", "Name", "GPA"); + + + for (int i =0; i