Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagonal difference #101

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.exe
35 changes: 35 additions & 0 deletions Maths/Factorial-iterative.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>

/*
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;
}
87 changes: 87 additions & 0 deletions Maths/diagonal-difference.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
/*
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;
}