diff --git a/src/hello.c b/src/hello.c index 9d6338f..7f5a152 100644 --- a/src/hello.c +++ b/src/hello.c @@ -2,8 +2,8 @@ #include int main(int argc, char *argv[]) { - // This is your first C program my friend - printf("Hello, RTU World from C Lab in 2025!\n"); + // My first C program! + printf("Hello, RTU World from C Lab in September 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..7236e09 Binary files /dev/null and b/src/lab2_1 differ diff --git a/src/lab2_1.c b/src/lab2_1.c index 699339a..8e8bdb6 100644 --- a/src/lab2_1.c +++ b/src/lab2_1.c @@ -12,8 +12,11 @@ */ 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; // placeholder } int main(void) { @@ -22,7 +25,11 @@ int main(void) { printf("Enter a positive integer n: "); scanf("%d", &n); - // TODO: validate input, call function, and print result + if (n<1) { + printf("error\n"); + } else { + printf("The sum is: %d\n", sum_to_n(n)); + } return 0; } diff --git a/src/lab2_2 b/src/lab2_2 new file mode 100755 index 0000000..614930a Binary files /dev/null and b/src/lab2_2 differ diff --git a/src/lab2_2.c b/src/lab2_2.c index d88ef6d..65b005a 100644 --- a/src/lab2_2.c +++ b/src/lab2_2.c @@ -12,8 +12,11 @@ */ long long factorial(int n) { - // TODO: compute factorial iteratively - return 1; // placeholder + long long res = 1; + for (int i=1; i<=n; i++) { + res *= i; + } + return res; // placeholder } int main(void) { @@ -22,7 +25,11 @@ int main(void) { printf("Enter a non-negative integer n: "); scanf("%d", &n); - // TODO: validate input, call function, print result + if (n<0) { + printf("Error\n"); + } else { + printf("The result is: %d\n", factorial(n)); + } return 0; } diff --git a/src/lab2_3 b/src/lab2_3 new file mode 100755 index 0000000..e5b3571 Binary files /dev/null and b/src/lab2_3 differ diff --git a/src/lab2_3.c b/src/lab2_3.c index ddc8af6..bc5f577 100644 --- a/src/lab2_3.c +++ b/src/lab2_3.c @@ -12,8 +12,13 @@ */ int is_prime(int n) { - // TODO: check if n is prime using loop up to sqrt(n) - return 0; // placeholder + if (n<2) return 0; + for (int i=2; i*i <= n; i++) { + if (n % i == 0) { + return 0; + } + } + return 1; // placeholder } int main(void) { @@ -22,7 +27,17 @@ int main(void) { 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"); + } else { + 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; } diff --git a/src/lab3_task1 b/src/lab3_task1 new file mode 100755 index 0000000..6067f56 Binary files /dev/null and b/src/lab3_task1 differ diff --git a/src/lab3_task1.c b/src/lab3_task1.c index f0d73ee..8b444c0 100644 --- a/src/lab3_task1.c +++ b/src/lab3_task1.c @@ -23,7 +23,10 @@ #include #include +<<<<<<< HEAD +======= +>>>>>>> upstream/main // Function prototypes int array_min(int arr[], int size); int array_max(int arr[], int size); @@ -44,8 +47,12 @@ int main(void) { // Implement functions below int array_min(int arr[], int size) { +<<<<<<< HEAD + int my_min = INT_MAX; +======= // TODO: return smallest element int my_min = INT_MAX; //set to max infinity +>>>>>>> upstream/main for (int i=0;i arr[i]) { my_min = arr[i]; @@ -55,16 +62,25 @@ int array_min(int arr[], int size) { } int array_max(int arr[], int size) { - // TODO: return largest element - return 0; // placeholder + int my_max = INT_MIN; + for (int i=0;imy_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 sum = 0; + for (int i=0; i // TODO: Define struct Student with fields name, id, grade +struct Student { + char name[50]; + int id; + float grade; +}; int main(void) { int n; @@ -25,14 +30,43 @@ int main(void) { } // TODO: Allocate memory for n Student structs using malloc + students = (struct Student *)malloc(n * sizeof(struct Student)); + if (students == NULL) { + printf("Error"); + return 1; + } // TODO: Read student data in a loop + for (int i = 0; i < n; i++) { + printf("\nEnter data for student %d:\n", i + 1); + printf(" Name: "); + scanf(" %[^\n]", students[i].name); + + printf(" ID: "); + scanf("%d", &students[i].id); + + printf(" Grade: "); + scanf("%f", &students[i].grade); + } // TODO: Display all student records in formatted output + printf("\n Student Records: \n"); + for (int i = 0; i < n; i++) { + printf("Student %d:\n", i + 1); + printf("Name: %s\n", students[i].name); + printf("ID: %d\n", students[i].id); + printf("Grade: %.2f\n\n", students[i].grade); + } // Optional: Compute average grade or find top student + float sum = 0.0; + for (int i = 0; i < n; i++) { + sum += students[i].grade; + } + printf("Average grade: %.2f\n", sum / 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..1c9c605 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..741020a 100644 --- a/src/week5_task1_file_io.c +++ b/src/week5_task1_file_io.c @@ -1,3 +1,5 @@ +//Leyli Iskandarli 241ADB073 + // week5_task1_file_io.c // Task 1: Read and write data from text files // Week 5 – Files & Modular Programming @@ -8,20 +10,49 @@ int main(void) { FILE *fp; - char filename[100] = "data.txt"; + char filename[100]; char line[256]; + int line_count = 0; + + // BONUS: ask user for filename instead of using default "data.txt" + printf("Enter filename: "); + scanf("%s", filename); // TODO: 1. Open file for writing (mode = "w") + fp = fopen(filename, "w"); + // TODO: 2. Check if file opened successfully + if (fp == NULL) { + printf("Error\n"); + return 1; + } + // TODO: 3. Write 2–3 lines of text to the file using fprintf() + fprintf(fp, "Meow meow 1\n"); + fprintf(fp, "Meow meow 2\n"); + fprintf(fp, "Meow meow 3\n"); + // TODO: 4. Close the file + fclose(fp); // TODO: 5. Open file again for reading (mode = "r") + fp = fopen(filename, "r"); + if (fp == NULL) { + printf("Error\n"); + return 1; + } + // TODO: 6. Use fgets() in a loop to read and print each line to the console - // TODO: 7. Close the file + while (fgets(line, sizeof(line), fp) != NULL) { + printf("%s", line); + line_count++; + } - // BONUS: ask user for filename instead of using default "data.txt" + // TODO: 7. Close the file + fclose(fp); + // BONUS: count number of lines read + printf("\nNumber of lines: %d\n", line_count); return 0; } diff --git a/src/week5_task2_struct_save_load b/src/week5_task2_struct_save_load new file mode 100755 index 0000000..f34cb0b 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..ef636ea 100644 --- a/src/week5_task2_struct_save_load.c +++ b/src/week5_task2_struct_save_load.c @@ -1,3 +1,5 @@ +//Leyli Iskandarli 241ADB073 + // week5_task2_struct_save_load.c // Task 2: Save and load structured records from a file // Week 5 – Files & Modular Programming @@ -28,8 +30,14 @@ 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 loaded = load_student(filename); // TODO: Print loaded data to confirm correctness + printf("Loaded data:\n"); + printf("Name: %s\n", loaded.name); + printf("Age: %d\n", loaded.age); + printf("GPA: %.2f\n", loaded.gpa); return 0; } @@ -37,13 +45,30 @@ 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 \n"); + exit(1); + } + + 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; - // ... + FILE *fp = fopen(filename, "r"); + if (fp == NULL) { + printf("Error \n"); + exit(1); + } + + fscanf(fp, "%s", s.name); + fscanf(fp, "%d", &s.age); + fscanf(fp, "%f", &s.gpa); + + fclose(fp); return s; } diff --git a/src/week5_task3_student_management_system b/src/week5_task3_student_management_system new file mode 100755 index 0000000..fb01587 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..f33096f 100644 --- a/src/week5_task3_student_management_system.c +++ b/src/week5_task3_student_management_system.c @@ -1,3 +1,5 @@ +//Leyli Iskandarli 241ADB073 + // week5_task3_student_management_system.c // Task 3: Mini-project – Student management system with file persistence // Week 5 – Files & Modular Programming @@ -29,7 +31,7 @@ int main(void) { int choice; // TODO: Load existing data from file using load_students() - + count = load_students(students); do { printf("\n=== Student Management System ===\n"); printf("1. List students\n"); @@ -42,12 +44,15 @@ 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"); @@ -60,24 +65,67 @@ 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; + FILE *fp = fopen(DATA_FILE, "r"); + int count = 0; + + if (fp == NULL) { + printf("Error\n"); + return 0; + } + + while (fscanf(fp, "%s %d %f", arr[count].name, &arr[count].id, &arr[count].gpa) == 3) { + count++; + if (count >= MAX_STUDENTS) break; + } + + fclose(fp); + printf("%d records loaded\n", count); + return count; } // TODO: Implement save_students() // Write all students to DATA_FILE void save_students(Student arr[], int count) { - // ... + FILE *fp = fopen(DATA_FILE, "w"); + if (fp == NULL) { + printf("Error\n"); + return; + } + + for (int i = 0; i < count; i++) { + fprintf(fp, "%s %d %.2f\n", arr[i].name, arr[i].id, arr[i].gpa); + } + + fclose(fp); } // TODO: Implement add_student() // Read input from user and append to array void add_student(Student arr[], int *count) { - // ... + if (*count >= MAX_STUDENTS) { + printf("Limit reached.\n"); + return; + } + + Student s; + printf("Enter name: "); + scanf("%s", s.name); + + printf("Enter ID: "); + scanf("%d", &s.id); + + printf("Enter GPA: "); + scanf("%f", &s.gpa); + + arr[*count] = s; + (*count)++; } // TODO: Implement list_students() // Print all students in readable format void list_students(Student arr[], int count) { - // ... + printf("\n%-20s %-10s %-5s\n", "Name", "ID", "GPA"); + for (int i = 0; i < count; i++) { + printf("%-20s %-10d %-5.2f\n", arr[i].name, arr[i].id, arr[i].gpa); + } }