diff --git a/README.md b/README.md index 728c26f3c29c3f22337e40639faa0063c9c94130..442cdb88d63c801021a3fb8d08b1a7cfc87baf22 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,164 @@ -# year2worksheet0 - - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://gitlab.uwe.ac.uk/c2-hyer/year2worksheet0.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://gitlab.uwe.ac.uk/c2-hyer/year2worksheet0/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README - -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +README: Pointers, Arrays, and Functions in C +This project demonstrates foundational concepts in C programming through a series of tasks focusing on pointers, arrays, file operations, and memory manipulation. Each task explores a unique aspect of low-level programming, providing practical examples and insights. + +Project Tasks Overview +The project is broken down into six tasks, each showcasing specific C programming concepts. Here's a summary of what each task does: + +Task 1: Pointer to a Local Variable +This task shows how to use pointers to manipulate the value of a local variable. + +Key Concept: +Using pointers to access and modify variables. +Code Snippet: +int n = 10; +int *ptr_to_n = &n; +(*ptr_to_n)++; // Increment n through the pointer +printf("Value of n after incrementing: %d\n", n); +Output: +arduino + +Task 1 - Value of n after incrementing: 11 +Task 2: Array and Pointer Iteration +This task demonstrates how to use pointers to iterate through an array and print its elements and their memory addresses. + +Key Concept: +Pointer arithmetic to traverse arrays. +Code Snippet: + +int arr[3] = {10, 30, 2000}; +int *ptr = arr; + +for (int i = 0; i < 3; i++) { + printf("Element %d: %d, Address: %p\n", i, *(ptr + i), (ptr + i)); +} +Output: +yaml + +Task 2 - Array elements and their addresses: +Element 0: 10, Address: 0x... +Element 1: 30, Address: 0x... +Element 2: 2000, Address: 0x... +Task 3: Compare Two Arrays +This task includes a function to compare two arrays and check if they are identical. + +Key Concept: +Comparing array elements using pointers. +Code Snippet: + +int compare_arrays(int *arr1, int *arr2, int length) { + for (int i = 0; i < length; i++) { + if (arr1[i] != arr2[i]) { + return 0; + } + } + return 1; +} +Output: +arduino + +Task 3 - Arrays are equal +Task 4: Reading and Summing Numbers from a File +This task reads integers from a file (foo.txt) and calculates their sum. + +Key Concept: +File I/O and reading integers from a file. +Code Snippet: +c + +FILE *file = fopen("foo.txt", "r"); +int sum = 0, num; + +while (fscanf(file, "%d", &num) != EOF) { + sum += num; +} + +printf("Sum of numbers: %d\n", sum); +fclose(file); +Expected File Content (foo.txt): + +10 +20 +30 +Output: +arduino + +Task 4 - Sum of numbers: 60 +Task 5: Swap Function Using Void Pointers +This task implements a generic swap function using void pointers and memcpy. + +Key Concept: +Generic programming with void pointers. +Code Snippet: + +void swap(void *x, void *y, size_t size) { + char temp[size]; + memcpy(temp, x, size); + memcpy(x, y, size); + memcpy(y, temp, size); +} +Example Usage: + +int a = 10, b = 20; +swap(&a, &b, sizeof(int)); +printf("Swapped values: a = %d, b = %d\n", a, b); +Output: +less + +Task 5 - Swapped values: a = 20, b = 10 +Task 6: Print a 2D Array +This task prints a 2D array using pointer arithmetic. + +Key Concept: +Accessing multi-dimensional arrays through pointers. +Code Snippet: + +void print_array(int *arr, int width, int height) { + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + printf("%d ", *(arr + i * width + j)); + } + printf("\n"); + } +} +Example Usage: + +int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; +print_array((int *)arr, 3, 2); +Output: +arduino + +Task 6 - 2D array: +1 2 3 +4 5 6 +How to Run +Prepare the File +If testing Task 4, ensure the file foo.txt is in the same directory with valid numbers (e.g., 10\n20\n30). + +Compile the Code +Use gcc to compile the code: + +bash + +gcc -o main main.c +Run the Program +Execute the program: + +bash + +./main +Sample Output +Below is an example of the program's output when all tasks are executed: + +yaml + +Task 1 - Value of n after incrementing: 11 +Task 2 - Array elements and their addresses: +Element 0: 10, Address: 0x... +Element 1: 30, Address: 0x... +Element 2: 2000, Address: 0x... +Task 3 - Arrays are equal +Task 4 - Sum of numbers: 60 +Task 5 - Swapped values: a = 20, b = 10 +Task 6 - 2D array: +1 2 3 +4 5 6 diff --git a/year2worksheet0 b/year2worksheet0 new file mode 100755 index 0000000000000000000000000000000000000000..777055a38720fd3eb6c6212a78758b9fb8953d48 Binary files /dev/null and b/year2worksheet0 differ diff --git a/year2worksheet0.cpp b/year2worksheet0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e440630ecbe4dcc3aba589449ca6d7e1def5e597 --- /dev/null +++ b/year2worksheet0.cpp @@ -0,0 +1,97 @@ +#include <stdio.h> +#include <string.h> + +// Task 1: Pointer to a Local Variable +void task1() { + int n = 10; + int *ptr_to_n = &n; // Pointer to n + (*ptr_to_n)++; // Increment n using the pointer + printf("Task 1 - Value of n after incrementing: %d\n", n); +} + +// Task 2: Array and Pointer Iteration +void task2() { + int arr[3] = {10, 30, 2000}; + int *ptr = arr; + + printf("Task 2 - Array elements and their addresses:\n"); + for (int i = 0; i < 3; i++) { + printf("Element %d: %d, Address: %p\n", i, *(ptr + i), (ptr + i)); + } +} + +// Task 3: Function to Compare Two Arrays +int compare_arrays(int *arr1, int *arr2, int length) { + for (int i = 0; i < length; i++) { + if (arr1[i] != arr2[i]) { + return 0; + } + } + return 1; +} + +void task3() { + int arr1[3] = {1, 2, 3}; + int arr2[3] = {1, 2, 3}; + int result = compare_arrays(arr1, arr2, 3); + + printf("Task 3 - Arrays are %s\n", result ? "equal" : "not equal"); +} + +// Task 4: Reading and Summing Numbers from a File +void task4() { + FILE *file = fopen("foo.txt", "r"); + if (file == NULL) { + printf("Task 4 - Error opening file.\n"); + return; + } + + int sum = 0, num; + while (fscanf(file, "%d", &num) != EOF) { + sum += num; + } + + printf("Task 4 - Sum of numbers: %d\n", sum); + fclose(file); +} + +// Task 5: Swap Function Using Void Pointers +void swap(void *x, void *y, size_t size) { + char temp[size]; + memcpy(temp, x, size); + memcpy(x, y, size); + memcpy(y, temp, size); +} + +void task5() { + int a = 10, b = 20; + swap(&a, &b, sizeof(int)); + printf("Task 5 - Swapped values: a = %d, b = %d\n", a, b); +} + +// Task 6: Print a 2D Array +void print_array(int *arr, int width, int height) { + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + printf("%d ", *(arr + i * width + j)); + } + printf("\n"); + } +} + +void task6() { + int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; + printf("Task 6 - 2D array:\n"); + print_array((int *)arr, 3, 2); +} + +// Main function to call each task +int main() { + task1(); + task2(); + task3(); + task4(); + task5(); + task6(); + return 0; +}