This week focuses on using pointers for dynamic memory allocation, creating and using structures, and combining both ideas to build a small in-memory database.
All programs should compile cleanly without warnings using the provided Makefile.
Write a C program that:
- Asks the user for the number of elements.
- Dynamically allocates memory for an integer array using
malloc. - Reads integers from user input and stores them in the array.
- Calculates and prints the sum and average of the entered numbers.
- Frees all allocated memory before the program exits.
The file should be named:
src/week4_1_dynamic_array.c
- Always check if
mallocreturned a valid pointer. - Use
sizeof(int)when allocating memory. - Avoid memory leaks: every successful
mallocshould have a correspondingfree. - Use clear, formatted output with
printf.
Enter number of elements: 5
Enter 5 integers: 10 20 30 40 50
Sum = 150
Average = 30.00
Write a C program that defines and uses a structure called Student.
Each student has:
char name[50]int idfloat grade
The program should:
- Declare at least two
Studentvariables. - Assign values to their fields (either manually or through user input).
- Print the information of each student in a formatted way.
The file should be named:
src/week4_2_struct_student.c
- Use
structkeyword for the definition. - Access fields using the dot (
.) operator. - You may use
strcpy()to assign a string to thenamefield. - Keep output readable and labeled.
Student 1: Alice Johnson, ID: 1001, Grade: 9.1
Student 2: Bob Smith, ID: 1002, Grade: 8.7
Create a C program that:
- Defines a
struct Student(same as in Task 2). - Dynamically allocates memory for an array of Student records using
malloc. - Prompts the user for the number of students.
- Reads each student’s name, ID, and grade from the user.
- Prints all student records in a formatted table.
- Frees all allocated memory before exit.
The file should be named:
src/week4_3_struct_database.c
- Reuse your
struct Studentdefinition from Task 2. - Use
malloc(n * sizeof(struct Student))for allocation. - Remember to use
scanf("%s", student[i].name)for names (no spaces) orfgets()for full names. - Free the allocated memory before program termination.
- Consider sorting or computing averages as optional bonus.
Enter number of students: 3
Enter data for student 1: Alice 1001 9.1
Enter data for student 2: Bob 1002 8.7
Enter data for student 3: Carol 1003 9.5
ID Name Grade
1001 Alice 9.1
1002 Bob 8.7
1003 Carol 9.5
Students can submit their work in either of two ways or BOTH:
- Ensure your GitHub repository is up to date:
git add . git commit -m "Week 4 completed" git push
- Submit the URL to your GitHub repository in Moodle.
Example:https://github.com/yourusername/RTU_Programming_Languages_C_Lab_Fall_2025
- Download your source files from the Codespaces
src/folder:week4_1_dynamic_array.cweek4_2_struct_student.cweek4_3_struct_database.c
- Upload these
.cfiles directly to Moodle.
Again, I prefer option A, but you can do option B if you are having trouble with your git repository.
Note: Some of these are not formal, but should provide a reasonable overview.
- GeeksforGeeks — Dynamic Memory Allocation in C (malloc, calloc, realloc, free)
- Programiz — C Dynamic Memory Allocation
- Learn-C.org — Dynamic Allocation (interactive)
- cppreference —
malloc(C) (see alsofreeandrealloclinked on that page)
✅ Reminder:
- Your code must compile without warnings using the provided Makefile.
- Comment your code clearly — explain why each important step is done.
- Always free dynamically allocated memory.
- Programs that crash or leak memory will lose points.