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
4 changes: 2 additions & 2 deletions src/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <stdio.h>

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]);
Expand Down
Binary file added src/lab2_1
Binary file not shown.
13 changes: 10 additions & 3 deletions src/lab2_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Binary file added src/lab2_2
Binary file not shown.
13 changes: 10 additions & 3 deletions src/lab2_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Binary file added src/lab2_3
Binary file not shown.
21 changes: 18 additions & 3 deletions src/lab2_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Binary file added src/lab3_task1
Binary file not shown.
28 changes: 22 additions & 6 deletions src/lab3_task1.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

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

>>>>>>> upstream/main
// Function prototypes
int array_min(int arr[], int size);
int array_max(int arr[], int size);
Expand All @@ -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<size;i++) {
if (my_min > arr[i]) {
my_min = arr[i];
Expand All @@ -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;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 sum = 0;
for (int i=0; i<size; i++) {
sum += arr[i];
}
return sum; // placeholder
}

float array_avg(int arr[], int size) {
// TODO: return average as float
return 0.0f; // placeholder
if (size == 0) return 0.0f;
int sum = array_sum(arr, size);
return (float) sum/size;
}
Binary file added src/lab3_task2
Binary file not shown.
6 changes: 4 additions & 2 deletions src/lab3_task2.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ int main(void) {

// Implement functions below
void swap(int *x, int *y) {
// TODO: swap values using a temporary variable
int t = *x;
*x = *y;
*y = t;
}

void modify_value(int *x) {
// TODO: multiply value by 2
*x = *x * 2;
}
Binary file added src/lab3_task3
Binary file not shown.
14 changes: 11 additions & 3 deletions src/lab3_task3.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ int main(void) {

// Implement functions below
int my_strlen(const char *str) {
// TODO: count characters until '\0'
return 0; // placeholder
int len = 0;
while (str[len] != '\0') {
len++;
}
return len; // placeholder
}

void my_strcpy(char *dest, const char *src) {
// TODO: copy characters until '\0'
int i = 0;
while (src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
3 changes: 3 additions & 0 deletions src/meow
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Meow meow 1
Meow meow 2
Meow meow 3
3 changes: 3 additions & 0 deletions src/student.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Alice
21
3.75
2 changes: 2 additions & 0 deletions src/students.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
leyli 241 -nan
db 121 5.80
Binary file added src/week4_1_dynamic_array
Binary file not shown.
23 changes: 19 additions & 4 deletions src/week4_1_dynamic_array.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* week4_1_dynamic_array.c
* Author: [Your Name]
* Student ID: [Your ID]
* Author: Leyli Iskandarli
* Student ID: 241ADB073
* Description:
* Demonstrates creation and usage of a dynamic array using malloc.
* Students should allocate memory for an integer array, fill it with data,
Expand All @@ -14,6 +14,8 @@
int main(void) {
int n;
int *arr = NULL;
int sum = 0;
double avg;

printf("Enter number of elements: ");
if (scanf("%d", &n) != 1 || n <= 0) {
Expand All @@ -23,16 +25,29 @@ int main(void) {

// TODO: Allocate memory for n integers using malloc
// Example: arr = malloc(n * sizeof(int));
arr = (int *)malloc(n * sizeof(int));

// TODO: Check allocation success
if (arr == NULL) {
printf("Error");
return 1;
}

// TODO: Read n integers from user input and store in array

// TODO: Compute sum and average
//Compute sum and average
printf("Enter %d integers: \n", n);
for (int i=0; i<n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
avg = (double)sum/n;

// TODO: Print the results
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", avg);

// TODO: Free allocated memory
free(arr);

return 0;
}
Binary file added src/week4_2_struct_student
Binary file not shown.
31 changes: 24 additions & 7 deletions src/week4_2_struct_student.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* week4_2_struct_student.c
* Author: [Your Name]
* Student ID: [Your ID]
* Author: Leyli Iskandarli
* Student ID: 241ADB073
* Description:
* Demonstrates defining and using a struct in C.
* Students should define a 'Student' struct with fields like name, id, and grade.
Expand All @@ -13,18 +13,35 @@

// 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) {
// TODO: Declare one or more Student variables
struct Student student1, student2;

// TODO: Assign values (either manually or via scanf)
strcpy(student1.name, "Leyli");
student1.id = 1;
student1.grade = 8.9;

strcpy(student2.name, "Meow");
student2.id = 2;
student2.grade = 8.5;

// TODO: Print struct contents using printf
printf("Student 1:\n");
printf(" Name: %s\n", student1.name);
printf(" ID: %d\n", student1.id);
printf(" Grade: %.2f\n", student1.grade);

printf("\nStudent 2:\n");
printf(" Name: %s\n", student2.name);
printf(" ID: %d\n", student2.id);
printf(" Grade: %.2f\n", student2.grade);

return 0;
}
Binary file added src/week4_3_struct_database
Binary file not shown.
38 changes: 36 additions & 2 deletions src/week4_3_struct_database.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* week4_3_struct_database.c
* Author: [Your Name]
* Student ID: [Your ID]
* Author: Leyli Iskandarli
* Student ID: 241ADB073
* Description:
* Simple in-memory "database" using an array of structs.
* Students will use malloc to allocate space for multiple Student records,
Expand All @@ -13,6 +13,11 @@
#include <string.h>

// TODO: Define struct Student with fields name, id, grade
struct Student {
char name[50];
int id;
float grade;
};

int main(void) {
int n;
Expand All @@ -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;
}
Binary file added src/week5_task1_file_io
Binary file not shown.
Loading