Skip to content

Commit

Permalink
use malloc and free for dynamic variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kvedala committed Apr 24, 2020
1 parent dee56f7 commit 4b07f0f
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 196 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)

if(MSVC)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
add_compile_options(/Za)
endif(MSVC)

add_subdirectory(conversions)
Expand Down
22 changes: 14 additions & 8 deletions misc/demonetization.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
//generate notes for an entered amount N.

#include <stdio.h>
#include <stdlib.h>

int ways(int n, int a[], int k)
int ways(int n, int *a, int k)
{
if(n<0 || k<0) return 0;
if(n == 0) return 1;
if(k == 0) return 0;
return ways(n, a, k-1) + ways(n-a[k-1], a, k);
if (n < 0 || k < 0)
return 0;
if (n == 0)
return 1;
if (k == 0)
return 0;
return ways(n, a, k - 1) + ways(n - a[k - 1], a, k);
}

int main()
Expand All @@ -20,15 +24,15 @@ int main()

printf("Number of coins? ");
scanf("%d", &m);
int coin[m], i;
for(i=0; i<m; i++)
int *coin = (int *)malloc(m * sizeof(int)), i;
for (i = 0; i < m; i++)
{
printf("coin? ");
scanf("%d", &coin[i]);
}

printf("---- your requests --- \n");
while(1)
while (1)
{
printf("amount? exit(0) ");
scanf("%d", &n);
Expand All @@ -38,5 +42,7 @@ int main()
}
printf("%d\n", ways(n, coin, m));
}

free(coin);
return 0;
}
43 changes: 26 additions & 17 deletions misc/lexicographic_Permutations.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,61 @@
#include <string.h>
#include <stdlib.h>

void swap(char* left, char* right){
void swap(char *left, char *right)
{
char temp = *left;
*left = *right;
*right = temp;
}

int compare (const void * a, const void * b){
return ( *(char*)a - *(char*)b );
int compare(const void *a, const void *b)
{
return (*(char *)a - *(char *)b);
}

void PrintSortedPermutations(char str[])
void PrintSortedPermutations(char *str)
{
int strSize = strlen(str);
qsort(str, strSize, sizeof(char), compare);

int largerPermFound = 1;
do{
do
{
// 1. Print permutation
printf("%s\n", str);
// 2. Find rightmost char that is smaller than char to its right
int i;
for (i = strSize - 2; i >= 0 && str[i] >= str[i+1]; --i){}
for (i = strSize - 2; i >= 0 && str[i] >= str[i + 1]; --i)
{
}

// if we couldn't find one, we're finished, else we can swap
if (i >= 0){
if (i >= 0)
{
// 3. find character at index j such that str[j] = min(str[k]) && str[k] > str[i] for all k > i
int j = i+1, k;
for(k=j; k<strSize && str[k]; k++){
int j = i + 1, k;
for (k = j; k < strSize && str[k]; k++)
{
if (str[k] > str[i] && str[k] < str[j])
j = k;
}
// 3. Swap chars at i and j
swap(&str[i], &str[j]);
// 4. Sort string to the right of i
qsort(str+i+1, strSize-i-1, sizeof(char), compare);
qsort(str + i + 1, strSize - i - 1, sizeof(char), compare);
}
else largerPermFound = 0;
}
while(largerPermFound);
else
largerPermFound = 0;
} while (largerPermFound);
}

int main() {
int main()
{
int n; //size of string
scanf("%d\n",&n);
char str[n];
scanf("%s",str);
scanf("%d\n", &n);
char *str = (char *)malloc(n * sizeof(char));
scanf("%s", str);
PrintSortedPermutations(str);
free(str);
return 0;
}
86 changes: 51 additions & 35 deletions misc/sudoku_solver.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,86 @@

#include <stdio.h>

const int M=144;
#define M 144
int N, R, C;

int OKrow(int a[M], int x, int y, int v) {
int OKrow(int a[M], int x, int y, int v)
{
int j;
for(j=0; j<N; j++)
if(a[x*N+j] == v)
for (j = 0; j < N; j++)
if (a[x * N + j] == v)
return 0;
return 1;
}
int OKcol(int a[M], int x, int y, int v) {
int OKcol(int a[M], int x, int y, int v)
{
int i;
for(i=0; i<N; i++)
if(a[i*N+y] == v)
for (i = 0; i < N; i++)
if (a[i * N + y] == v)
return 0;
return 1;
}
int OKbox(int a[M], int x, int y, int v) {
int bi=x/R, bj=y/C, i, j;
for(i=0; i<R; i++)
for(j=0; j<C; j++)
if(a[(i + bi*R)*N + (j + bj*C)] == v)
int OKbox(int a[M], int x, int y, int v)
{
int bi = x / R, bj = y / C, i, j;
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
if (a[(i + bi * R) * N + (j + bj * C)] == v)
return 0;
return 1;
}
int OK(int a[M], int x, int y, int v) {
return OKrow(a,x,y,v) && OKcol(a,x,y,v) && OKbox(a,x,y,v);
int OK(int a[M], int x, int y, int v)
{
return OKrow(a, x, y, v) && OKcol(a, x, y, v) && OKbox(a, x, y, v);
}

void print(int a[M]) {
void print(int a[M])
{
int i, j;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
printf("%d%c", a[i*N+j], (j==N-1 ? '\n':' '));
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
printf("%d%c", a[i * N + j], (j == N - 1 ? '\n' : ' '));
}

int solve(int a[M]) {
int i, j, v, rem=0;
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
if(a[i*N+j] == 0) {
int solve(int a[M])
{
int i, j, v, rem = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (a[i * N + j] == 0)
{
rem = 1;
for(v=1; v<=N; v++) {
if(OK(a,i,j,v)) {
a[i*N+j] = v;
if(solve(a)) return 1;
a[i*N+j] = 0;
for (v = 1; v <= N; v++)
{
if (OK(a, i, j, v))
{
a[i * N + j] = v;
if (solve(a))
return 1;
a[i * N + j] = 0;
}
}
}
}
}
if(rem == 0) return 1;
if (rem == 0)
return 1;
return 0;
}

int main() {
int main()
{
scanf("%d%d%d", &N, &R, &C);
int a[M], i, j;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
scanf("%d", &a[i*N+j]);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
scanf("%d", &a[i * N + j]);

if(solve(a)) print(a);
else printf("Invalid\n");
if (solve(a))
print(a);
else
printf("Invalid\n");
return 0;
}
4 changes: 2 additions & 2 deletions numerical_methods/durand_kerner_roots.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* define polynomial function
**/
long double complex function(double *coeffs, unsigned int degree, long double complex x)
long double complex poly_function(double *coeffs, unsigned int degree, long double complex x)
{
long double complex out = 0.;
unsigned int n;
Expand Down Expand Up @@ -154,7 +154,7 @@ int main(int argc, char **argv)

for (n = 0; n < degree - 1; n++)
{
long double complex numerator = function(coeffs, degree, s0[n]);
long double complex numerator = poly_function(coeffs, degree, s0[n]);
long double complex denominator = 1.0;
for (i = 0; i < degree - 1; i++)
if (i != n)
Expand Down
Loading

0 comments on commit 4b07f0f

Please sign in to comment.