From 8a55b8a69a7750f251adbffdce688b6b5857d280 Mon Sep 17 00:00:00 2001 From: edwinwalela Date: Wed, 28 Oct 2020 13:18:13 +0300 Subject: [PATCH 1/3] add diagonal difference --- Maths/diagonal-difference.c | 87 +++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Maths/diagonal-difference.c diff --git a/Maths/diagonal-difference.c b/Maths/diagonal-difference.c new file mode 100644 index 0000000..bebb14f --- /dev/null +++ b/Maths/diagonal-difference.c @@ -0,0 +1,87 @@ +#include +#include +/* + Print the absolute difference between the sums of the matrix's two diagonals + + Sample input: + 3 + 11 2 4 + 4 5 6 + 10 8 -12 + + Expected output: + 15 + + Explanation: + The first line of input contains a single integer, n , which represents + the number of rows and columns in the matrix + + Each of the next (n) lines describes a row, arr[i], and consists of (n) + '/n' separated integers arr[i][j]. + +*/ + +int diagonalDifference(int n,int** arr){ + int primaryDiag[n]; + int secondaryDiag[n]; + + // Get integers in primary diagonal + for(int i = 0; i < n; i++){ + primaryDiag[i] = arr[i][i]; + } + int i = 0; + int j = n-1; + + // Get integers in secondary diagonal + while(i < n){ + secondaryDiag[i] = arr[i][j]; + i++; + j--; + } + + int mainSum = 0; + int subSum = 0; + + // Sum primary diagonal + for(int i = 0; i < n; i++){ + mainSum += primaryDiag[i]; + } + + // Sum secondary diagonal + for(int i = 0; i < n; i++){ + subSum += secondaryDiag[i]; + } + + int diff = mainSum - subSum; + + if(diff < 0){ // If negative multiply by -1 to get absolute value + diff *= -1 ; + } + + return diff; +} + +int main(){ + int n; // + scanf("%d",&n); + + // Initialize an array of pointers to represent matrix + int** matrix = (int**) malloc(n*sizeof(int*)); + for(int i = 0; i < n; i++){ + matrix[i] = (int *) malloc(n*sizeof(int*)); + } + + // Populate array with input + for(int i = 0; i < n; i++){ + for(int k = 0; k < n; k++){ + scanf("%d",&matrix[i][k]); + } + } + + int diff = diagonalDifference(n,matrix); + + // Print diff + printf("diagonal difference = %d\n",diff); + + return 1; +} \ No newline at end of file From 5cd001febee2a58d8d1b7f8552371fb6e0adc795 Mon Sep 17 00:00:00 2001 From: edwinwalela Date: Wed, 28 Oct 2020 13:18:51 +0300 Subject: [PATCH 2/3] add gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adb36c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file From 0e2cda88e5e92fa4dbeffa98449907981737f945 Mon Sep 17 00:00:00 2001 From: edwinwalela Date: Wed, 28 Oct 2020 13:39:55 +0300 Subject: [PATCH 3/3] add iterative factorial --- Maths/Factorial-iterative.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Maths/Factorial-iterative.c diff --git a/Maths/Factorial-iterative.c b/Maths/Factorial-iterative.c new file mode 100644 index 0000000..4856a27 --- /dev/null +++ b/Maths/Factorial-iterative.c @@ -0,0 +1,35 @@ +#include +#include + +/* + Compute factorial using iteration + + e.g Factorial of 3: + 3 * 2 * 1; +*/ + +int factorial(int n){ + unsigned long fact = 1; + if (n < 0){ + printf("Factorial of a negative number doesn't exist."); + exit(0); + }else { + for (int i = 1; i <= n; ++i) { + fact *= i; + } + return fact; + } +} + +int main() { + int n; + unsigned long x; + + printf("Enter an integer: "); + scanf("%d", &n); + x = factorial(n); + printf("Factorial of %d = %lu", n, x); + + + return 0; +} \ No newline at end of file