diff --git a/Practical2/Conversion.c b/Practical2/Conversion.c new file mode 100644 index 0000000..2121b0b --- /dev/null +++ b/Practical2/Conversion.c @@ -0,0 +1,38 @@ +#include +#include + +int main(void) { + +/* Declare variables */ + int i,inum,tmp,numdigits; + float fnum; + char binnum[60]; + +/* Intialise 4-byte integer */ + inum = 33554431; +/* Convert to 4-byte float */ + fnum = (float) inum; + + +/* Convert to binary number (string)*/ + i = 0; tmp = inum; + while (tmp > 0) { + sprintf(&binnum[i],"%1d",tmp%2); + tmp = tmp/2; + i++; + } + +/* Terminate the string */ + binnum[i] = '\0'; + + +/* Complete the expression */ +numdigits = (int) ceil(log2(inum + 1)); +printf("The number of digits is %d\n",numdigits); + + + + printf("inum=%d, fnum=%f, inum in binary=%s\n", + inum,fnum,binnum); + +} diff --git a/Practical2/REAMDE.md b/Practical2/REAMDE.md new file mode 100644 index 0000000..60ab981 --- /dev/null +++ b/Practical2/REAMDE.md @@ -0,0 +1,7 @@ +Command to run Conversion.c +gcc -o conversion Conversion.c -lm +./conversion + +Command to run sum.c +gcc -o sum sum.c -lm +./sum diff --git a/Practical2/Sum.c b/Practical2/Sum.c new file mode 100644 index 0000000..26e346f --- /dev/null +++ b/Practical2/Sum.c @@ -0,0 +1,34 @@ +#include + + +int main(void) { +/* Declare variables */ + int i; + float sum1, sum2, diff; + + +/* First sum */ + sum1 = 0.0; + for (i=1; i<=1000; i++) { + /* Insert here */ +sum1 += 1.0 / i; + } + + +/* Second sum */ + sum2 = 0.0; + for (i=1000; i>0; i--) { + /* Insert the same line as above except use sum2 */ + sum2 += 1.0 / i; + + } + + printf(" Sum1=%f\n",sum1); + printf(" Sum2=%f\n",sum2); + +/* Find the difference */ + diff = sum1 - sum2; + + printf(" Difference between the two is %f\n",diff); + +} diff --git a/Practical3/README.md b/Practical3/README.md new file mode 100644 index 0000000..ba189a4 --- /dev/null +++ b/Practical3/README.md @@ -0,0 +1,28 @@ +# Trapezoidal Rule Approximation for f(x) = tan(x) + +This C program approximates the integral of f(x) = tan(x) from 0 to π/3 using the Trapezoidal Rule. The code calculates the numerical approximation and compares it with the actual value obtained from log(2). + +## Compilation and Running Instructions + +1. **Compile the Program:** + - Open a terminal. + - Navigate to the directory containing the C file (`trapezoidal_rule.c`). + - Compile the program using a C compiler (e.g., gcc): + ```bash + gcc -o trapezoidal_rule trapezoidal_rule.c -lm + ``` + Note: The `-lm` flag is used to link the math library. + +2. **Run the Program:** + - Execute the compiled binary: + ```bash + ./trapezoidal_rule + ``` + +3. **Expected Output:** + - The program will display the approximation of the integral using the Trapezoidal Rule and the actual value of the integral. + + + + + diff --git a/Practical3/trapezoidal_rule.c b/Practical3/trapezoidal_rule.c new file mode 100644 index 0000000..a406a59 --- /dev/null +++ b/Practical3/trapezoidal_rule.c @@ -0,0 +1,35 @@ +#include +#include + +int main() { + // Define variables + int n = 12; + double a = 0; + double b = M_PI / 3; + double w = (b - a) / (double)n; + + // Initialize the sum with the values at the endpoints + double sum = tan(a) + tan(b); +int i; + + // Iterate through equidistant points and update the sum + for (i = 1; i < n; i++) { + sum += 2 * tan(a + w * i); + } + + // Finalize the approximation using the Trapezoidal Rule + sum = sum * w * 0.5; + + // Print the approximation result + printf("The Approximation of f(x) = tan(x) from 0 to pi/3 is %.3f\n", sum); + + // Calculate the actual value using log(2) + double actual_value = log(2.0); + + // Print the actual value + printf("The actual value of f(x) = tan(x) from 0 to pi/3 is %.3f\n", actual_value); + + return 0; +} + + diff --git a/Practical4/README.md b/Practical4/README.md new file mode 100644 index 0000000..c0d948e --- /dev/null +++ b/Practical4/README.md @@ -0,0 +1,23 @@ +# Trapezoidal Rule Approximation for tan(x) + +This C program approximates the integral of tan(x) from 0 to 60 degrees using the Trapezoidal Rule. The code calculates the numerical approximation and compares it with the actual value obtained from log(2). + +## Compilation and Running Instructions + +1. **Compile the Program:** + - Open a terminal. + - Navigate to the directory containing the C file (`practical4.c`). + - Compile the program using gcc: + ```bash + gcc -o prog filename.c -lm + ``` + Note: The `-lm` flag is used to link the math library. + +2. **Run the Program:** + - Execute the compiled binary: + ```bash + ./prog + ``` + +3. **Expected Output:** + - The program will display the values of tan(x) for each degree, the approximation of the integral using the Trapezoidal Rule, and the actual value of the integral. diff --git a/Practical4/practical4.c b/Practical4/practical4.c new file mode 100644 index 0000000..e23f542 --- /dev/null +++ b/Practical4/practical4.c @@ -0,0 +1,46 @@ +#include +#include + +double converttoradians(double degree); +double calarea(int n); + +int main() { + int n = 12; + double area; + area = calarea(n); + int i; +double arr[n+1]; +for(i=0;i<=n;i++){ +arr[i]=tan(converttoradians(5.0*i)); +printf("The value of tan(x) when x=%d is %.2f ",i*5,arr[i]); +printf("\n"); + +} + + printf("The approximation of tan(x) from 0-60 degrees using trapezoidal rule is %.3f", area); +printf("\n"); +printf("The actual value of tan(x) from 0-60 degrees is:%.3f",log(2.0)); + + return 0; +} + +double converttoradians(double degree) { + return (degree * M_PI) / 180.0; +} + +double calarea(int n) { + int i; + double arr[n + 1], rad, w; + for (i = 0; i < n + 1; i++) { + rad = tan(converttoradians(5.0 * i)); + arr[i] = rad; + } + double area = arr[0] + arr[n]; + for (i = 1; i < n; i++) { + area = area + 2.0 * arr[i]; + } + w = converttoradians((60.0 - 0) / (2.0 * n)); + area = area * w; + return area; +} + diff --git a/Practical5/README.md b/Practical5/README.md new file mode 100644 index 0000000..695d306 --- /dev/null +++ b/Practical5/README.md @@ -0,0 +1,44 @@ +# Fibonacci Sequence Program + +## Overview + +This C program generates the Fibonacci sequence up to a specified value of `n`. It uses a function to calculate the next Fibonacci number based on the previous two. The program includes comments for clarity and readability. + +## Instructions + +### 1. Compilation + +Compile the program using a C compiler. For example: + +```bash +gcc -o fibonacci fibonacci.c -lm + +### 2. Execution +Run the compiled program: +./fibonacci + +### 3. Usage +Enter the desired value of n when prompted. The program will then display the Fibonacci sequence up to the specified value. + +# Arctangent Approximation Program + +## Overview + +This C program calculates and compares two approximations of the arctangent function for a range of input values. The approximations are implemented in `archtanx1` and `archtanx2` functions. The program includes comments for clarity and readability. + +## Instructions + +### 1. Compilation + +Compile the program using a C compiler. For example: + +```bash +gcc -o arctan_approx arctan_approx.c -lm + +### 2. Execution +./arctan_approx + +### 3. Usage +Enter the desired value of delta when prompted. The program will then display the difference between the arctangent approximations for a range of input values. + + diff --git a/Practical5/arctan_approx.c b/Practical5/arctan_approx.c new file mode 100644 index 0000000..f026b1a --- /dev/null +++ b/Practical5/arctan_approx.c @@ -0,0 +1,52 @@ +#include +#include +#include + +// Function prototypes +double archtanx1(const double x, const double delta); +double archtanx2(const double x); + +int main() { + double delta, x, tan1[1000], tan2[1000]; + printf("Enter value of delta: "); + scanf("%lf", &delta); + x = -0.9; + int j; + + for (j = 0; x <= 0.9 && j < 1000; j++) { + // Calculate arctangent approximations + tan1[j] = archtanx1(x, delta); + tan2[j] = archtanx2(x); + + // Print the difference between the approximations for each x + printf("The difference between functions archtanx1 and archtanx2 is %.10f for x=%f\n", fabs(tan1[j] - tan2[j]), x); + + x = x + 0.1; + } + + return 0; +} + +// Approximation of arctangent using a power series +double archtanx1(const double x, const double delta) { + double sum = 0; + double current = x; + int i = 0; + int exponent; + + // Calculate terms of the power series until convergence + while (fabs(current) > delta) { + exponent = 2 * i + 1; + current = pow(x, exponent) / exponent; + sum += current; + i++; + } + + return sum; +} + +// Approximation of arctangent using logarithmic functions +double archtanx2(const double x) { + return (log(1 + x) - log(1 - x)) / 2; +} + diff --git a/Practical5/fibonacci.c b/Practical5/fibonacci.c new file mode 100644 index 0000000..780e800 --- /dev/null +++ b/Practical5/fibonacci.c @@ -0,0 +1,46 @@ +#include +#include + +// Function to calculate the next Fibonacci number +void fib(int *fn_1, int *fn_2); + +int main() { + int n; + printf("Enter the value of n: "); + scanf("%d", &n); + + int f0, f1, next, i; + f1 = 1; + f0 = 0; + + // Print Fibonacci sequence for n=1 + if (n == 1) { + printf("The Fibonacci sequence for n=%d is %d", n); + printf("\n"); + } + // Print Fibonacci sequence for n=2 + else if (n == 2) { + printf("The Fibonacci sequence for n=%d is %d", n, 1); + printf("\n"); + } else { + printf("The Fibonacci sequence is %d ", f1, 1); + + // Loop to calculate and print Fibonacci sequence up to n + for (i = 1; i <= n - 1; i++) { + fib(&f0, &f1); + printf("%d", f1); + printf(" "); + } + } + + printf("\n"); + return 0; +} + +// Function to calculate the next Fibonacci number +void fib(int *a, int *b) { + int next; + next = *a + *b; + *a = *b; + *b = next; +} diff --git a/Practical6/.Makefile b/Practical6/.Makefile new file mode 100644 index 0000000..6f5bf7d --- /dev/null +++ b/Practical6/.Makefile @@ -0,0 +1,8 @@ +CC =gcc +CCFLAGS=-03 +matmult: main.o matmul.o + $(CC) -o matmult main.o matmul.o +matmul.o: matmul.c + $(CC) -c $(CCFLAGS) matmul.c +main.o: main.c + $(CC) -c $(CCFLAGS) main.c \ No newline at end of file diff --git a/Practical6/Makefile b/Practical6/Makefile new file mode 100644 index 0000000..0f6ff5d --- /dev/null +++ b/Practical6/Makefile @@ -0,0 +1,8 @@ +CC =gcc +CCFLAGS=-O3 +matmult: main.o matmul.o + $(CC) -o matmult main.o matmul.o +matmul.o: matmul.c + $(CC) -c $(CCFLAGS) matmul.c +main.o: main.c + $(CC) -c $(CCFLAGS) main.c \ No newline at end of file diff --git a/Practical6/README.md b/Practical6/README.md new file mode 100644 index 0000000..d7ee2c8 --- /dev/null +++ b/Practical6/README.md @@ -0,0 +1,12 @@ +# Matrix Multiplication Program +This program performs matrix multiplication of two matrices, Cn×q = An×pBp×q, where n = 5, p = 3, and q = 4. It initializes three arrays A, B, and C of type double, and populates them according to specified rules. + +# Compilation: + 1. make + This will compile the main.c and matmult.c files and generate the executable named matmult. + + 2. ./matmult + This will execute the program, performing matrix multiplication and printing the results. + + + \ No newline at end of file diff --git a/Practical6/main.c b/Practical6/main.c new file mode 100644 index 0000000..7289a8f --- /dev/null +++ b/Practical6/main.c @@ -0,0 +1,54 @@ +#include +#include +#include + +int main(){ + +int n,p,q,i,j,k; +n=5; +p=3; +q=4; +double A[n][p],B[p][q],C[n][q]; +for(i=0;i +void matmul(int n,int p, int q,double A[n][p],double B[p][q],double C[n][q]){ +int i,j,k; +for(i=0;i +#include +#include + +int main(){ + +int n,p,q,i,j,k; +n=5; +p=3; +q=4; +double A[n][p],B[p][q],C[n][q]; +for(i=0;i +#include +#include + +// Function declarations +int factorial(int n); +double* allocateArray(int size); +void fillWithOnes(double* array, int size); +void freeArray(double* array); +void printArray(double* array,int size); +int main() { + int size,order; +printf("Enter the order of Polynomial: "); +scanf("%d",&size); + for (order = 1; order <= size; order++) { + double* arr = allocateArray(order+1); + fillWithOnes(arr, order + 1); + double estimate = 1.0; + double x = 1; + int i; + for (i = 1; i <= order; i++) { + arr[i] = arr[i] / factorial(i); + x += arr[i]; + } +printf("The value of approximated e is %f. The difference between approximated and the actual value of e is %e",x,x-exp(1.0)); +printf("\n"); +freeArray(arr); + } + + return 0; +} + +int factorial(int n) { + if (n == 0) { + return 1; + } else { + return (n * factorial(n - 1)); + } +} + +double* allocateArray(int size) { + return (double*)malloc(size * sizeof(double)); +} + +void fillWithOnes(double* array, int size) { +int i; + for (i = 0; i < size; i++) { + array[i] = 1.0; + } +} + + +// Function to free allocated memory +void freeArray(double* array) { + free(array); +} + diff --git a/Practical8/README.md b/Practical8/README.md new file mode 100644 index 0000000..40ca34e --- /dev/null +++ b/Practical8/README.md @@ -0,0 +1,13 @@ +# GCD Calculator +This program calculates the Greatest Common Divisor (GCD) of two integers using the Euclidean Algorithm. + +# How to Use +1. Compile the program using a C compiler: + gcc -o gcd_calculator gcd_calculator.c -lm + +2. Run the executable: + ./gcd_calculator + +3. Enter two integers when prompted. + +4. The program will calculate and display the GCD of the entered integers. \ No newline at end of file diff --git a/Practical8/gcd_calculator.c b/Practical8/gcd_calculator.c new file mode 100644 index 0000000..496bf47 --- /dev/null +++ b/Practical8/gcd_calculator.c @@ -0,0 +1,25 @@ +#include + +int gcd_recursive(int a, int b) { + if (b == 0) { + return a; + } else { + return gcd_recursive(b, a % b); + } +} + +int main() { + int num1, num2; + + // Input two integers + printf("Enter the first integer: "); + scanf("%d", &num1); + printf("Enter the second integer: "); + scanf("%d", &num2); + + // Calculate and print the GCD + int result = gcd_recursive(num1, num2); + printf("The GCD of %d and %d is: %d\n", num1, num2, result); + + return 0; +} diff --git a/Practical9/README.md b/Practical9/README.md new file mode 100644 index 0000000..6c9b40c --- /dev/null +++ b/Practical9/README.md @@ -0,0 +1,8 @@ +# Magic Square Checker +This program takes a matrix from a file, checks if it's a magic square, and prints the result. + +# Running the Program +* Compile the program using the command "main_stub.c". +* Run the executable. +* Enter the file name when prompted. +* The program will output whether the matrix is a magic square or not \ No newline at end of file diff --git a/Practical9/magic_square.txt b/Practical9/magic_square.txt new file mode 100644 index 0000000..ec3c7cd --- /dev/null +++ b/Practical9/magic_square.txt @@ -0,0 +1,3 @@ +2 7 6 +9 5 1 +4 3 8 diff --git a/Practical9/magic_square_stub.h b/Practical9/magic_square_stub.h new file mode 100644 index 0000000..f4a0323 --- /dev/null +++ b/Practical9/magic_square_stub.h @@ -0,0 +1,49 @@ +#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 i, j; + // TODO: Checking that every row and column add up to M +int diagonalsum,secdiagonalsum; +diagonalsum=0; +secdiagonalsum=0; +for(i=0;i +#include +#define MAX_FILE_NAME 100 +#include "magic_square_stub.h" // Assuming you have a header file for magic_square functions + +int getlines(char filename[MAX_FILE_NAME]); + +int main() { + FILE *f; + char filename[MAX_FILE_NAME]; + printf("Enter file name: "); + scanf("%s", filename); + + int n = getlines(filename); + + // Open the file + f = fopen(filename, "r"); + if (f == NULL) { + fprintf(stderr, "Error opening file.\n"); + return 1; + } +int i; + // Allocating a matrix for storing the magic square + int **magicSquare = (int **)malloc(n * sizeof(int *)); + for (i = 0; i < n; i++) { + magicSquare[i] = (int *)malloc(n * sizeof(int)); + } +int j; + // Inputting integer data into the matrix + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + if (fscanf(f, "%d", &magicSquare[i][j]) != 1) { + fprintf(stderr, "Error reading from file.\n"); + // Additional error handling if needed + return 1; + } + } + } +printf("This square %s magic. \n",isMagicSquare(magicSquare,n)?"is":"is not"); + + // Freeing each row separately before freeing the array of pointers + for (i = 0; i < n; i++) { + free(magicSquare[i]); + } + free(magicSquare); + + // Close the file + fclose(f); + + return 0; +} + +// Rest of the code remains unchanged + +//##! + +int getlines(char filename[MAX_FILE_NAME]) { + FILE *fp; + fp = fopen(filename, "r"); + + int ch_read; + int count = 0; + + while( (ch_read = fgetc(fp)) != EOF) + { + if (ch_read == '\n'){ + count++; + } + } + + printf("No. lines, %d\n", count); + fclose(fp); + return count; +} diff --git a/Practical9/not_magic_square.txt b/Practical9/not_magic_square.txt new file mode 100644 index 0000000..50ecbb5 --- /dev/null +++ b/Practical9/not_magic_square.txt @@ -0,0 +1,3 @@ +1 2 3 +4 5 6 +7 8 9