diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..cea4d3f --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e69de29 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0e0ebdf --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc build active file", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}", + "" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/Exam.zip b/Exam.zip new file mode 100644 index 0000000..ab44f6e Binary files /dev/null and b/Exam.zip differ diff --git a/README.md b/README.md index ddf588a..d9e30b3 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,84 @@ -# Scientific Programming Concepts PH502/ACM40660 Autumn 2023 +# ReadMe +This repository contains the practical for the module ACM40660 - Scientific Programming (ICHEC). -For this assignment, you need to have an account on *[GitHub.com](https://github.com)*. After logged in to your account, fork this repository into your github account. From there; +## Practical03 +### Print.c +This file contains a simple code to print some variables using the function *printf*. The integers are printed using *%d* and the double using *%f*. -- Clone the repo onto your local machine/sciprog using `git clone` -- Create your own branch using `git branch` with the format `{FirstName}_{LastName}_{Brightspace/Canvas}`. Eg. `John_Smith_Brightspace`, then use `git checkout branch_name` to work on that branch -- Create, edit and upload files using the `git add`, `commit` and `push` commands - - You will need to generate a “*[personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)*” to push changes to this repo. Follow the instructions in the link above carefully, and when prompted for username and password, use your “personal access token” as your password. - - For the duration of the semester you will need to update the repository regularly with the practical codes which will be covered in the live demo sessions on Tuesdays. Remember the differences between a local repository and the online version, so you will need to ‘push’ your changes rather than just committing them. - -This assignment will contribute 20% of your total marks to this module, so to ensure that you get full marks; + gcc -o Print Print.c + ./Print -- All practicals from Week 2 onwards should be included in the repo and the code from each practical should be able to be run and compiled (on sciprog) once cloned by another user. You can use your own solution or the material covered in the demos. +### Area.c +This file calculates the area under the curve for the function *tan(x)* using the Trapezodial rule. A loop between the two values of interest is created using a small increment. It is possible to calculate the area more accuratly by reducing the increment, but the time of comutation will therefore be increased. The output value is compared with the real value of the area to ensure our calculation is correct. The command to compile this file changes slightly as we use some functions of the *math.h* library. -- Each practical is clearly commented and the repository should be well structured and laid out (i.e. have a folder for each practical). This includes having sufficient README markdown or .txt files explaining the contents of each folder, and instructions on how to use/run the programs (these do not need to be exhaustive, enough information so that the reader understands your code). + gcc -o Area Area.C -lm + ./Area -- Although there is no “weekly deadline”, you should update this repo weekly following each practical with clear and concise commit messages. +## Practical04 +### Tang.c +This file uses two functions to calculate the values of the tangeante of an angle. The first one converts degrees into radians. The second uses the same method as Area.c file. The main part creates a loop over the range of angles we are interested in. -- At the end, capture the output of the `git status` and `git log` commands ( eg. `git log > gitlog_output.txt` will write the output to a file called `gitlog_output.txt`), and **submit only these files to Brightspace/Canvas**. + gcc -o Tang Tang.c -lm + ./Tang -We are looking for stepwise development by committing code (and comments) on a regular basis. Marks will be deducted for; -- Not commenting your code and poorly maintained/updated repositories -- Uploaded code does not compile/unable to run -- Ambiguous commit messages -- All-in-one commit/push (i.e. uploading everything at once) -- This repository should only be used for the practical sessions covered in the live demo sessions ONLY. **Assignments 1 & 2 should not be added to the repo**. If the regular assignments are uploaded to your repository then you will receive 0% for this assignment. +## Practical05 +### Fibon.c +This file calls a function to generate the next number in the Fibonnaci sequence. The main part let us enter a positive number *N*. If the number is not greater than 1, then we ask the user again. We then use our function to print each number of the sequence until the *N-th* element. -There are plentiful resources on git introductions, such as https://guides.github.com/introduction/git-handbook/ + gcc -o Fibon Fibon.c -lm + ./Fibon + +### Hyper.c +This file contains two method to calulate the values for inverse hyperbolic tangent function. The first one uses a loop over n elements until the value reaches a degree of accuracy set by the user. The second uses a formula using the natural logarithm. In the main part, we compare these two results by printing the values and the difference. + + gcc -o Hyper Hyper.c -lm + ./Hyper + +## Practical06 +### Makefile +This file contains informations to compile the next two file at the same time. + + make + make clean (to remove the unnecessary files) + +### matmult.c +This file contains one function which reads three integers and three matrices. The matrices are the size of the three integers. We compute the matrix multiplucation of the third one using the first two. + +### main.c +This file contains a main part which initialize the the values for three matrices using loops. It then calls the function created in the *matmult.c* file to compute the multiplication. The next three loops are used to print the three matrices. + +## Practical07 +### exp.c +This file calcultes the approximate value of *e* using a loop and the factorial function. We use dynamical array in this sequence to change the value of the number of order of polynomials. It is essential to allocate memory for the pointers at the begining and free the memory allocated for this pointer. + + gcc -o exp exp.c -lm + ./exp + +### array.c +This file uses multiple function to modify the values inside a array using pointers. The first one allocates memory for the pointer. The second calls the array and fills its values with ones. The third prints the values of the array. The forth frees the memory of the pointer. All of the functions are called inside the main part. + + gcc -o array array.c -lm + ./array + +## Practical08 +### gcd.c +This file uses two functions to calculate the greatest common divisor between two numbers given by the user. The code does not stop asking for two number until they are integers using a function. The first one uses a loop. The second calls the same functions. They both stop once a condition is filled *b=0*. + + gcc gcd gcd.c -lm + ./gcd + +## Practical09 +### magic_square_.txt +This file contains a 3x3 magic square matrix. + +### not_magic_square.txt +This file contains 3x3 matrix but it is not a magic square. + +### magic_square.h +This file contains a function to check if a given matrix is magic or not. It first checks if the sum of the elements of each rows and columns are equal to the magic number. Then, it checks if the sum of the elements of the two diagonals are equal to the magic number.It returns one if all these conditions are passed. Returns zero otherwise. + +### main.c +This file opens the content of a given *.txt* file entered by the user. If the file does not exist, an error is printed. Memory is allocated to each row of the matrix. Loops are implemented to read each element of the file and store it inside pointers. The function defined in the other file is called and the results is printed. The memory is freed for each row corresponding to each pointer. + + gcc -o main main.c -lm + ./main \ No newline at end of file diff --git a/gitlog_output.txt b/gitlog_output.txt new file mode 100644 index 0000000..21ca152 --- /dev/null +++ b/gitlog_output.txt @@ -0,0 +1,290 @@ +commit ee3d7ee8f2b0364e1e5f5e79e443554d9426597d +Author: BorisM202 +Date: Tue Nov 28 16:10:04 2023 +0000 + + Final Check + +commit 9622eea366a066c2edeeb7604218b41fb79d438b +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 27 20:56:13 2023 +0000 + + Final Check 3 + +commit c678dadb6528a230759342d22529bcf71c635f55 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 27 20:23:20 2023 +0000 + + Final Check 2 + +commit 5add7a395c50862809e283778313be89838a0b54 +Merge: 626948a d84c9fd +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 27 17:28:41 2023 +0000 + + Merge branch 'Boris_Morin_Brightspace' of https://github.com/BorisM202/sciprog_23 into Boris_Morin_Brightspace + +commit 626948af53589136b0f167d308b23fe199b0422b +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 27 17:28:38 2023 +0000 + + Final Check + +commit d84c9fdbc526719c8c5f3aaf32c1e31e0a6d8a41 +Merge: 9e63c81 54974c8 +Author: BorisM202 +Date: Wed Nov 22 14:57:07 2023 +0000 + + Merge branch 'Boris_Morin_Brightspace' of https://github.com/BorisM202/sciprog_23 into Boris_Morin_Brightspace + +commit 54974c8ed3be7999ac385d228095aa88a92b1cac +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Nov 21 11:40:34 2023 +0000 + + Modified practical09 + +commit 9473a3075766abb515cabff31d4e30a85989ecc5 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 20 13:32:22 2023 +0000 + + Begin practical09 + +commit 9e63c811cea5792fa90f5a9b1ebe561645b59761 +Author: BorisM202 +Date: Tue Nov 14 10:34:25 2023 +0000 + + Finished practical08 + +commit 0b5ca0b2a5ae1d5ddd2cd3c9b787cdd7c5a5a835 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 13 18:34:43 2023 +0000 + + Finish practical08 + +commit efe67b98ca88623e382333fa060a881b6ee6cca8 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 13 18:25:51 2023 +0000 + + Started Practical08 + +commit e295febdcd302e69b2166e4878d083badefeb26c +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 13 17:23:17 2023 +0000 + + Finished practical07 + +commit 06df4d10d91b8ebab9b14d13627b143ca0190fb6 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Nov 7 12:12:39 2023 +0000 + + Started Sxo2 of Practical07 + +commit 81a83132014c062c8e8f3c6255e0d6ab00b4a317 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Nov 7 10:44:04 2023 +0000 + + Strated practical07 + + Still some stuff to finish + +commit 49bab20a8673082967f320800d33c6199cb4b559 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 6 20:29:33 2023 +0000 + + Finished Practical05 + +commit df9e870febdc4a8230de20e2f18765ad707f324a +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 6 19:43:46 2023 +0000 + + Finished Practical04 + +commit e0c9d91a841a32fe13839b1dda57459a201437d2 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 6 19:30:47 2023 +0000 + + Change Practical04 + + Still one compilation mistake + +commit d2b960743f72a298a37e6d8e7853e73b4143f1e7 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 6 18:57:31 2023 +0000 + + Change Practical03 + +commit b294109db880625c6c3b76c8a18be48fc6efd4a4 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 6 15:33:08 2023 +0000 + + Exercises 1 and 2 of the assignment 1 + +commit 422cd3a012fd0d18229a8fb77a02ce20bcc66acd +Merge: 000ce03 4484ec3 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 6 10:41:26 2023 +0000 + + Merge branch 'Boris_Morin_Brightspace' of https://github.com/BorisM202/sciprog_23 into Boris_Morin_Brightspace + +commit 000ce030ba6d79073e5a62da2b28aa77a4014f1e +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Nov 6 10:40:55 2023 +0000 + + Creation File + +commit 4484ec30c8fe82f2d154c8540910ca5fb716a43c +Author: BorisM202 +Date: Mon Nov 6 10:39:19 2023 +0000 + + VScode + +commit 816cee85cfea0772f6e8887203f8ea5eb2efc9f5 +Merge: 07655ca b182264 +Author: BorisM202 +Date: Mon Nov 6 10:38:54 2023 +0000 + + Merge branch 'Boris_Morin_Brightspace' of https://github.com/BorisM202/sciprog_23 into Boris_Morin_Brightspace + +commit 07655cae08408812f98f6bb3db456f3088442f9f +Author: BorisM202 +Date: Tue Oct 31 16:33:04 2023 +0000 + + Created Practical06 files + +commit b1822643195c0b89a523989490cd6c9c9e196748 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Oct 24 11:49:36 2023 +0100 + + Modify practical05 + +commit d03d6b5fdf5f69e4a4d2130d20c8a5222c8728c5 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Oct 24 11:46:28 2023 +0100 + + Modifying practical05 + +commit aec758d163e0c68022d5cf2e1e7b7fb5130e6850 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Oct 24 11:16:26 2023 +0100 + + System Error + +commit 4baad2754d3a530ca64ddc1c47c35dbd2efd4db3 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Oct 24 10:57:51 2023 +0100 + + Modifying practical05 + +commit d9c2183b184f6e1e4c64c94886b369b74a0e0b2d +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Oct 24 10:42:32 2023 +0100 + + Modifying practical05 + +commit 9150fb614e8c5dc4cb93eeaccb63c6bc104baf3e +Author: BorisM202 +Date: Mon Oct 23 18:07:57 2023 +0100 + + Continuing practical05 + +commit 5cc033c8e8806ed23b8d04a8300168b6cc6020f2 +Author: BorisM202 +Date: Mon Oct 23 17:11:04 2023 +0100 + + Started Fibonacci Series + +commit cdac020d91651c292bf3d01ca30e53967683abda +Author: BorisM202 +Date: Mon Oct 23 16:36:53 2023 +0100 + + Finished practical04 + +commit f27d122191693d8ad8120788aeed2769cdc7661f +Merge: dd02f4a dd8f1cb +Author: BorisM202 +Date: Mon Oct 23 16:15:44 2023 +0100 + + Merge branch 'Boris_Morin_Brightspace' of https://github.com/BorisM202/sciprog_23 into Boris_Morin_Brightspace + +commit dd02f4a757de81bcc59e8b89387e6dec0aac8fcd +Author: BorisM202 +Date: Mon Oct 23 16:15:37 2023 +0100 + + Finished practical03 + +commit dd8f1cb218b594851cf57a031327c64790f6d5b6 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Tue Oct 10 20:19:32 2023 +0100 + + Fixed the code so it does compile and find the right values. But cannot print the full array + +commit 7f31c3c4ec52901298b40cd037334dd802928c0e +Author: BorisM202 +Date: Tue Oct 10 09:09:22 2023 +0100 + + Comment and change loop + +commit 32340c84bbd57fc8d89d86c933c511b82d6c9209 +Author: BorisM202 +Date: Tue Oct 10 08:56:10 2023 +0100 + + Print the value of log(2) + +commit ef9652c0e121ba14dc184c724f1697bc66ed6b99 +Author: BorisM202 <145043616+BorisM202@users.noreply.github.com> +Date: Mon Oct 9 18:51:35 2023 +0100 + + Beginning of tan evaluation + +commit e79a66603b116e4376e384ef7a406b586c6783f5 +Author: Boris Morin <145043616+BorisM202@users.noreply.github.com> +Date: Mon Oct 9 18:42:27 2023 +0100 + + Add files via upload + +commit 4692ab067f0770b5b6dfe670a77980159c9a4599 +Author: Buket Benek Gursoy +Date: Fri Sep 29 09:12:05 2023 +0100 + + Update README.md + +commit cd1f3539715bb7505a462bca3747943b77a36bab +Author: Buket Benek Gursoy +Date: Thu Sep 28 15:55:04 2023 +0100 + + Update README.md + +commit fb2f93729881b2f90f310fc663cc6efd1073017b +Author: Buket Benek Gursoy +Date: Thu Sep 28 15:54:24 2023 +0100 + + Update README.md + +commit f7edcd0c9b27b4274913af43d7b53b379b0ed4de +Author: Buket Benek Gursoy +Date: Thu Sep 28 14:51:49 2023 +0100 + + Update README.md + +commit 26f18819fe128ba33aac685c327ab3769f7f1efe +Author: Buket Benek Gursoy +Date: Thu Sep 28 13:45:54 2023 +0100 + + Update README.md + +commit 28f1724422f6823ce758640815d12816bd7a92f1 +Author: Buket Benek Gursoy +Date: Thu Sep 28 13:42:38 2023 +0100 + + Update README.md + +commit 12c68e1b6ee16ac5c4fe31c683f42e900b93d64e +Author: Buket Benek Gursoy +Date: Thu Sep 28 13:41:56 2023 +0100 + + Update README.md + +commit 25cbd308e636fcc641d18f37f3d0dc139d953832 +Author: Buket Benek Gursoy +Date: Wed Sep 27 14:11:53 2023 +0100 + + Initial commit diff --git a/gitstatus_output.txt b/gitstatus_output.txt new file mode 100644 index 0000000..a9178ab --- /dev/null +++ b/gitstatus_output.txt @@ -0,0 +1,8 @@ +On branch Boris_Morin_Brightspace +Your branch is up to date with 'origin/Boris_Morin_Brightspace'. + +Untracked files: + (use "git add ..." to include in what will be committed) + gitstatus_output.txt + +nothing added to commit but untracked files present (use "git add" to track) diff --git a/practical03/Area b/practical03/Area new file mode 100755 index 0000000..fbaf867 Binary files /dev/null and b/practical03/Area differ diff --git a/practical03/Area.c b/practical03/Area.c new file mode 100644 index 0000000..7546408 --- /dev/null +++ b/practical03/Area.c @@ -0,0 +1,33 @@ +#include +#include + +int main(void) { + // Declare and Initialize variables + int N = 12; // Number of iterations + int i = 0; + double x_i = 0.0; // Lower limit + double x_f = M_PI / 3.0; // Upper limit + double width = (x_f - x_i) / (double)N; // Width + double value = tan(x_i) + tan(x_f); // Initial sum + double x, y = 0.0; // Variables for loop + + // Loop over the N values + for (i = 1; i < N; i++) { + x = x_i + width * i; + y = tan(x); + value += 2 * y; + } + + // Compute the integral approximation + double integral_approx = value * width * 0.5; + + // Exact value of the integral + double integral_exact = log(2.0); + + // Print the results + printf("The area under the curve is equal to: %lf\n", integral_approx); + printf("The value of log(2) is equal to: %lf\n", integral_exact); + printf("The difference between the approximation and the exact value is: %lf\n", integral_approx - integral_exact); + + return 0; +} diff --git a/practical03/Print b/practical03/Print new file mode 100755 index 0000000..2d7ac22 Binary files /dev/null and b/practical03/Print differ diff --git a/practical03/Print.c b/practical03/Print.c new file mode 100644 index 0000000..2ee77f4 --- /dev/null +++ b/practical03/Print.c @@ -0,0 +1,12 @@ +#include + +int main() { + int i1 = 10; + int i2 = 200; + float fl1 = 1.11; + float fl2 = 2.222; + + printf("Two ints %d %d and two floats %.2f %.4f\n", i1, i2, fl1, fl2); + + return 0; +} diff --git a/practical04/Tang b/practical04/Tang new file mode 100755 index 0000000..44642ad Binary files /dev/null and b/practical04/Tang differ diff --git a/practical04/Tang.c b/practical04/Tang.c new file mode 100644 index 0000000..ff4809e --- /dev/null +++ b/practical04/Tang.c @@ -0,0 +1,55 @@ +#include +#include + + +// Global variables +const int N = 12; +double TanVal[13]; + +// Functions +double degtorad(double deg); +double traprule(int n); + +int main(void) { + int i = 0; + double degang = 0, radang = 0; + + for (i = 0; i < N; i++) { + degang = i * 5.0; + radang = degtorad(degang); + TanVal[i] = tan(radang); + } + + // Calculate the integral using two methods + double integ_approx = traprule(N); + double integ_exact = log(2.0); + + // Print the values + printf("The approximate value is : %lf\n", integ_approx); + printf("The exact value is : %lf\n", integ_exact); + printf("The difference between the two values is : %lf\n", integ_approx-integ_exact); + + return 0; +} + +// Function to convert angle from degrees to radians +double degtorad(double deg) { + return (M_PI * deg) / 180.0; +} + +// Function to calculate the tan values +double traprule(int N) { + // Declare variables + int i = 0; + double width = 0; + double area = TanVal[0] + TanVal[N]; + + for (i = 1; i < N; i++) { + area += 2. * TanVal[i]; + } + + width = degtorad(60.0) / (2.0 * N); + area *= width; + + return area; +} diff --git a/practical05/Fibon b/practical05/Fibon new file mode 100755 index 0000000..4eecc7c Binary files /dev/null and b/practical05/Fibon differ diff --git a/practical05/Fibon.c b/practical05/Fibon.c new file mode 100644 index 0000000..2ade3f6 --- /dev/null +++ b/practical05/Fibon.c @@ -0,0 +1,44 @@ +#include + +// Function declarations +void generateNextFibonacciNumber(int *current, int *previous); + +int main(void) { + // Variable declarations and initialization + int N = 0; // Initialize to a default value + int i = 0; + int f0 = 0, f1 = 1; + + // Enter a number, loop until the number is less than or equal to 1 + do { + printf("Enter an integer larger than 1: "); + scanf("%d", &N); + } while (N <= 1); + + // Print the first two initial numbers + printf("The Fibonacci sequence is:\n"); + printf("%d, %d, ", f0, f1); + + // Loop for generating the next numbers + for (i = 2; i < N; i++) { + generateFibonacci(&f1, &f0); + printf("%d", f1); + + if (i % 10 == 0) { + printf("\n"); + } else { + printf(", "); + } + } + + printf("\n"); + + return 0; +} + +// Function next Fibonacci number +void generateFibonacci(int *a, int *b) { + int tmp = *a + *b; + *b = *a; + *a = tmp; +} diff --git a/practical05/Hyper b/practical05/Hyper new file mode 100755 index 0000000..055f1eb Binary files /dev/null and b/practical05/Hyper differ diff --git a/practical05/Hyper.c b/practical05/Hyper.c new file mode 100644 index 0000000..02f3163 --- /dev/null +++ b/practical05/Hyper.c @@ -0,0 +1,56 @@ +#include +#include + +// Functions declarations +double arctanh1(const double x, const double delta); +double arctanh2(const double x); + +int main(void) { + + // Variables declarations and initialization + double delta = 1e-6; // Default precision + double x = -0.9; + int N = 1000; + double T1[N], T2[N]; + int j = 0; + + // User Input for delta + printf("Enter the precision for the MacLaurin Series (positive value): "); + scanf("%lf", &delta); + + if (delta <= 0) { + printf("Error: Please enter a positive value for precision.\n"); + return 1; // Exit with an error code + } + + // Calculate and compare results + while (j < N) { + T1[j] = arctanh1(x, delta); + T2[j] = arctanh2(x); + printf("The difference at x=%lf between them is %.10lf.\n", x, fabs(T1[j] - T2[j])); + x += 0.1; + j++; + } + + return 0; +} + +double arctanh1(const double x, const double delta) { + double sum = 0; + double elem, value; + int n = 0; + + do { + value = 2 * n + 1; + elem = pow(x, value) / value; + sum += elem; + n++; + + } while (fabs(elem) >= delta); + + return sum; +} + +double arctanh2(const double x) { + return 0.5 * (log(1 + x) - log(1 - x)); +} diff --git a/practical06/Makefile b/practical06/Makefile new file mode 100644 index 0000000..c816843 --- /dev/null +++ b/practical06/Makefile @@ -0,0 +1,17 @@ +CC = gcc +CFLAGS = -O3 +LDFLAGS = -lm + +all: matmult + +matmult: main.o matmult.o + $(CC) -o $@ $^ $(LDFLAGS) + +matmult.o: matmult.c + $(CC) -c $(CFLAGS) $< + +main.o: main.c + $(CC) -c $(CFLAGS) $< + +clean: + rm -f *.o matmult diff --git a/practical06/main.c b/practical06/main.c new file mode 100644 index 0000000..38ace43 --- /dev/null +++ b/practical06/main.c @@ -0,0 +1,55 @@ +#include +#include + +int main() { + // Declare Variables + int n = 5, p = 3, q = 4; + double A[n][p], B[p][q], C[n][q]; + int i, j, k; + + // Initialize the matrices + for(i=0; i,n; i++) { + for(j=0; j +#include + +// Declare the functions +int *allocate(int n); +void fillwithones(int *array, int n); +void printarray(int *array, int n); +void freearray(int *array); + + +int main() { + // Declare variables + int n, *a; + + // Ask user + printf("Size of the array : "); + scanf("%d", &n); + + // Use the function developped above + a = allocate(n); + + fillwithones(a, n); + printarray(a, n); + + freearray(a); + + return 0; +} + + +// Allocate array +int *allocate(int n) { + int *p; + p = (int *) malloc(n * sizeof(int)); + return p; +} + +// Fill with ones +void fillwithones(int *array, int n) { + int i; + for (i=0;i +#include +#include + +int factorial(int n); + +int main(void) { + int i=0, order=0; + double e=1., *terms; + + printf("Enter the required polynomial order\n"); + if (scanf("%d", &order) !=1) { + printf("Error : Did not enter a number \n"); + return 1; + } + + terms = malloc(order * sizeof(double)); + for (i=0; i +#include + + +bool areint(int x, int y); +int iteration_gcd(int x, int y); +int recursive_gcd(int x, int y); + +int main() { + int a, b; + + printf("Give two integers : "); + scanf("%d %d", &a, &b); + + bool result = areint(a, b); + if (result != 1) { + printf("Error : The numbers given are not integers"); + return 1; + } + + printf("Using iteration, GCD between %d, and %d, is : %d \n", a, b, iteration_gcd(a,b)); + printf("Using recursion, GCD between %d, and %d, is : %d \n", a, b, recursive_gcd(a,b)); + +} + + +int iteration_gcd(int x, int y) { + int temp; + + do { + temp = y; + y = x % y; + x = temp; + } while (y != 0); + + return x; +} + +int recursive_gcd(int x, int y) { + if (y == 0) { + return x; + } + else { + recursive_gcd(b, a%b); + } +} + + + +bool areint(int x, int y) { + return true; +} + + diff --git a/practical08/gcd.md b/practical08/gcd.md new file mode 100644 index 0000000..11e27e8 --- /dev/null +++ b/practical08/gcd.md @@ -0,0 +1,23 @@ +1. Iterative + +``` +IterativeGCD(a,b) +while b $\not=$ 0 do + temp = b + b = a % b + a = temp + +return a +``` + +2. Recursive + +``` +RecursiveGCD(a,b) +if b = 0 then + return a +else + return RecursiveGCD(a,a%b) +end if + +``` diff --git a/practical09/magic_square.h b/practical09/magic_square.h new file mode 100644 index 0000000..6879619 --- /dev/null +++ b/practical09/magic_square.h @@ -0,0 +1,47 @@ +#include +// Checks if a matrix is a magic square. +// A magic square is an n-sided matrix whose sum of values for each +// row, column, main and secondary diagonals equals to n(n^2 + 1)/2. +// The function takes as input a matrix 'square' and its side length 'n' +// and outputs 0 if 'n' is negative or 'square' is NOT a magic square; +// otherwise, outputs a non-zero value + + +int isMagicSquare(int ** square, const int n) { + + // Eliminate the case where 'n' is negative + if(n < 0) { + return 0; + } + + // M is the sum of every row, column, + // and the main and secondary diagonals + int M = (n * (n*n + 1))/2; + + int sum1 = 0; + int i, j; + // Checking that every row and column add up to M + for (i=0; i +#include +#include "magic_square.h" + +#define MAX_FILE_NAME 100 + +int getlines(char filename[MAX_FILE_NAME]); + +int main(){ + + //define our file variable + FILE *f; + char filename[MAX_FILE_NAME]; + + printf("Enter file name: "); + scanf("%s", filename); + + // n function which gets the number of lines + int n = getlines(filename); + + // Open the file + f = fopen(filename, "r"); + if (f == NULL) { + printf("Error : Can't open the file\n"); + exit(1); + } + + // Allocating a matrix for storing the magic square + // as an array of pointers, where each pointer is a row + int i; + int **magicSquare = malloc(n*sizeof(int*)); + for(i=0; i