diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d4a224b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "ctype.h": "c" + } +} \ No newline at end of file diff --git a/Assignment 6/abc.c b/Assignment 6/abc.c index 9d96b7f..5ea4a08 100644 --- a/Assignment 6/abc.c +++ b/Assignment 6/abc.c @@ -15,17 +15,17 @@ static float discriminant(){ void abc(){ float D = discriminant(); - if (D>0){ - x1real = ((-b+sqrt(D))/(2.0*a)); + if (D>0){ /*Calculate x1real and x2real if D>0*/ + x1real = ((-b+sqrt(D))/(2.0*a)); /*Use 2.0 instead of 2 so a float is calculated instead of an int*/ x2real = ((-b-sqrt(D))/(2.0*a)); ximag = 0; } - else if(D<0){ + else if(D<0){ /*Calculate x1real and ximag if D<0*/ x1real = -b/(2.0*a); x2real = x1real; ximag = (sqrt(-D)/(2.0*a)); } - else{ + else{ /*Calculate x1real if D=0*/ x1real = -b/(2.0*a); x2real = x1real; ximag = 0; diff --git a/Assignment 7/main.c b/Assignment 7/main.c index e17ef98..0e5c30a 100644 --- a/Assignment 7/main.c +++ b/Assignment 7/main.c @@ -9,23 +9,23 @@ #include int main(){ - int m,n,i,j,c; - double price[100] = {0}; - int matrix[100][100]; - scanf("%d",&n); - for(i = 0; i +#include +#include + +void swap(double *p, double *q){ /*Swapping*/ + double temp; + temp = *p; + *p = *q; + *q = temp; + } + +void sort(double a[], double n){ /*Bubble Sort*/ + int i,j; + + for (i = 0; i < n - 1; i++){ + for (j = n - 1; j > i; --j){ + if (a[j-1] > a[j]){ + swap(&a[j-1], &a[j]); + } + } + } +} + +int main(){ + int dim, num; + int i,j; + double **w; + double *length, temp; + + scanf ("%d %d", &dim, &num); /*read N and M*/ + w = calloc (num, sizeof (double *)); /*allocate array of M pointers*/ + for (i = 0; i < num; i++){ + /*allocate space for N dimensional vector*/ + w[i] = calloc (dim, sizeof (double)); + /*read the vector*/ + for (j = 0; j < dim; j++){ + scanf ("%le", &w[i][j]); + } + } + + //length = calloc (num, sizeof (double)); + for (i = 0; i < num; i++){ + temp = 0; + for (j = 0;j < dim; j++){ + temp = temp + pow(w[i][j], 2); + length[i] = sqrt(temp); + } + } + sort(length, num); + for (i = 0; i < num; i++){ + printf("length = %e \n", length[i]); + } + return 0; +} \ No newline at end of file diff --git a/Assignment 9/main.c b/Assignment 9/main.c new file mode 100644 index 0000000..64ea7e7 --- /dev/null +++ b/Assignment 9/main.c @@ -0,0 +1,50 @@ +/* + * Student: S.K. Soekhlal + * Number: 4860632 + * Assignment: 4.8 + */ + +#include +#include +#include + +char str[1024]; +int slen, wlen, count, i, j; + +void next(){ + while(isspace(str[i]) == 0 && ispunct(str[i] == 0 && str[i] != '\0')) + i++; +} + +int main(int argc, char **argv){ + gets(str); + slen = strlen(str); + printf("%d\n", slen); + wlen = strlen(argv[1]); + printf("%d\n", wlen); + for (i = 0; i < slen; i++){ + } + i = j = count = 0; + while(i < slen){ + if (str[i] == *argv[1]){ + for (j = 0; j < wlen; j++){ + if(str[i+j] != argv[1][j]){ + next(); + break; + } + } + if (j == wlen){ + if (isspace(str[i+j]) || ispunct(str[i+j]) || str[i+j] == '\0'){ + count++; + i +=j; + } + } + } + else{ + next(); + } + i++; + } + printf("%d\n", count); + return 0; +} \ No newline at end of file