0% found this document useful (0 votes)
18 views

Lab 6 TO 10

Uploaded by

kvns.19810808
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab 6 TO 10

Uploaded by

kvns.19810808
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lab 6:1D Array manipulation, linear search

i) Find the min and max of a 1-D integer array.


ii) Perform linear search on1D array.
iii) The reverse of a 1D integer array
iv) Find 2’s complement of the given binary number.
v) Eliminate duplicate elements in an array.

1.
//Min and max ofa1-D integer array.
#include<stdio.h>
int main()
{
int A[10], i, max, min, n;
printf("Input the number of elements to be stored in the array :");
scanf ("%d", &n) ;
printf("Input %d elements in the array :\n" ,n);
for (i=0;i<n;i++)
scanf(“%d”,&A[i]);
max = A[0];
min = A[0];
for (i=1 ;i<n; i++)
{
if(A[i]>max)
max = A[i];
if (A[i]<min)
min = A[i];

}
printf("%d is maximum and %d is minimum element in the array",max,min);
}

Output:

Input the number of elements to be stored in the array :5


Input 5 elements in the array :
12
6
-11
-23
13
13 is maximum and -23 is minimum element in the array
2.
//Perform linear search on 1D array.
#include<stdio.h>
int main()
{
int A[100], search, i, number;
printf( "Enter the number of elements in array:") ;
scanf ("%d",&number);
printf( "Enter %d numbers\n", number);
for ( i = 0 ; i< number ; i++ )
scanf ("%d", &A[i]);
printf("Enter the number to search\n");
scanf (" %d" ,&search) ;
for ( i = 0 ; i< number ; i++ )
{
if ( A[i] == search )
{printf( "%d is present in the location %d. \n", search, i+1);
break;
}}
if(i==number)
printf("Element not present in the array");
return 0;
}
Output:
Enter the number of elements in array: 4
Enter 6 numbers:
12
16
9
-3
Enter the number to search
5
Element not present in the array
Output:
Enter the number of elements in array: 4
Enter 6 numbers:
12
16
9
-3
Enter the number to search
9
9 is present in the location 3.
3.
//The reverse of a1D integer array.
#include<stdio.h>
int main()
{
int n, i,j,t, A[100];
printf("Enter the number of elements in the array\n”);
scanf ("%d", &n);
printf("Enter the array elements\n");
for (i = 0; i<n; i++)
scanf ("%d", &A[i]);
for(i=0,j=n-1;i<j;i++,j--)
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
printf("Reverse of the array\n") ;
for (i=0;i<n;i++)
printf("%d ", A[i]);
return 0;
}

Output:

Enter the number of elements in the array :5


Enter the array elements
12
45
52
36
61
Reverse of the array
61 36 52 45 12
4.
//Eliminate duplicate elements in an array.
#include<stdio.h>
int main()
{
int A[100],i,j,k,n;
printf("Enter size of the array :");
scanf ("%d" ,&n);
printf("Enter Elements of the array: \n");
for (i=0;i<n;i++)
scanf("%d", &A[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(A[i]==A[j])
{
for(k=j;k<n;k++)
A[k]=A[k+1];
j--;
n--;
}
}
}
printf("After deleting the duplicate elements, the Array is: \n");
for (i=0; i<n; i++)
printf("%d ",A[i]);
return 0;
}

Output:

Enter size of the array :5


Enter Elements of the array:
1
2
2
1
2
After deleting the duplicate elements, the Array is:
1 2
5.
// 2's complement of the given binary number.
#include<stdio.h>
int main()
{
char num[10], A[10], B[10];
int i,j,k, carry = 1;
printf("Enter the binary number") ;
scanf("%s",num) ;
for (i=0;num[i]!='\0';i++)
{
if (num [i] == '0')
A[i] = '1';
else
A[i] = '0';
}
A[i]='\0';
printf("1's complement ofbinary number %s is %s\n",num,A);
for(j = i - 1; j>= 0; j--)
{
if(A[j] == '1' && carry == 1)
B[j] = '0';
else if (A[j] == '0' && carry == 1)
{
B[j] = '1';
carry = 0;
}
else
B[j] = A[j];
}
B[i]='\0';
printf("Two's complement of binary number %s is %s",num,B);
return 0;
}

Output:
Enter the binary number :00010011
1's complement of binary number 00010011 is 11101100
2's complement of binary number 00010011 is 11101101
Lab 7: Matrix problems, String operations, Bubble sort
i) Addition of two matrices
ii) Multiplication two matrices
iii) Sort array elements using bubble sort
iv) Concatenate two strings without built-in functions
v) Reverse a string using built-in and without built-in string functions

1.
//Addition of two matrices.
#include<stdio.h>
int main()
{
int r, c, A[10][10], B[10][10], sum[10][10], i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
printf("Enter elements of A matrix: \n");
for (i=0; i< r; i++)
for (j=0; j < c; ++j)
scanf("%d", &A[i][j]);
printf("Enter elements of B matrix: \n");
for (i = 0; i< r; i++)
for (j = 0; j < c; ++j)
scanf("%d",&B[i][j]);
for (i = 0; i<r; i++)
for (j = 0; j< c; j++)
sum[i][j] = A[i][j] + B[i][j];
printf("Matrix A is: \n");
for (i=0; i<r; i++)
{
for (j= 0; j < c; j++)
printf("%2d ",A[i][j]);
printf("\n");
}
printf("Matrix B is: \n");
for (i = 0; i<r;++i)
{
for (j= 0; j < c; ++j)
printf( "%2d ", B[i][j]);
printf("\n");
}
printf("Sum of two matrices is: \n");
for (i = 0; i<r;++i)
{
for (j= 0; j < c; ++j)
printf( "%2d ", sum[i][j]);
printf("\n");
}
return 0;
}

Output:

Enter the number of rows: 2


Enter the number of columns: 3
Enter elements of A matrix:
1
2
3
4
5
6
Enter elements of B matrix:
4
11
12
5
7
9
Matrix A is:
1 2 3
4 5 6
Matrix B is:
4 11 12
5 7 9
Sum of two matrices is:
5 13 15
9 12 15
1.//Multiplication of two matrices.
#include<stdio.h>
void main()
{
int A[50] [50],B[50] [50],C[50][50], i, j ,k,r1,c1 ,r2, c2;
printf("Input the rows and columns of first matrix: \n");
scanf ("%d %d", &r1,&c1);
printf("Input the rows and columns of second matrix : \n");
scanf ("%d %d",&r2,&c2);
if(c1!=r2 )
printf("\nColumn of first matrix and row of second matrix must be same. ");
else
{
printf("Enter elements of A matrix: \n");
for (i = 0; i< r1; i++)
for (j= 0; j < c1; ++j)
scanf("%d", &A[i] [j]);
printf("Enter elements of B matrix: \n");
for (i = 0; i< r2; i++)
for (j = 0; j < c2; ++j)
scanf( "%d", &B[i][j] );
for (i = 0; i<r1; i++)
{
for (j= 0; j< c2; j++)
{
C[i][j] =0;
for (k=0; k<c1;k++)
C[i][j] +=A[i][k] *B[k] [j];
}
}
printf("Matrix A is: \n");
for (i = 0; i<r1;++i)
{
for (j= 0; j < c1; ++j)
printf( "%2d ", A[i][j]);
printf("\n");
}
printf("Matrix B is: \n");
for (i = 0; i<r2;++i)
{
for (j= 0; j < c2; ++j)
printf( "%2d ", B[i][j]);
printf("\n");
}
printf("The multiplication of two matrices is :\n");
for (i = 0; i<r1;++i)
{
for (j= 0; j < c2; ++j)
printf( "%2d ", C[i][j]);
printf("\n");
}
}
}

Output:
Input the rows and columns of first matrix:
2
3
Input the rows and columns of second matrix :
3
2
Enter elements of A matrix:
1
2
3
4
5
6
Enter elements of B matrix:
1
2
3
4
5
6
Matrix A is:
1 2 3
4 5 6
Matrix B is:
1 2
3 4
5 6
The multiplication of two matrices is :
22 28
49 64

3.
//Sort 1-D Array
#include<stdio.h>
void main()
{
int arr[100];
int n, i, j, tmp;
printf("Input the size of array :");
scanf("%d", &n );
printf("Input %d elements into the array :\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]) ;

for (i=0; i<n; i++)

for (j=i+1; j<n; j++)


{
if (arr[j] <arr[i])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
printf("\nElements of array in sorted ascending :\n");
for (i=0; i<n; i++)
printf("%d ",arr[i]) ;
}

Output:

Input the size of array :5


Input 5 elements into the array :
5
6
8
4
2
Elements of array in sorted ascending :
2 4 5 6 8
4.
//Concatenate two strings without built-in functions.
#include<stdio.h>
int main()
{
char str1[10] , str2[10] ,str3[20];
int i=0, j=0;
printf("Enter First String:\n");
scanf ("%s",str1);
printf("Enter Second String:\n") ;
scanf("%s",str2);
while(str1[i] !='\0')
{
str3[i]=str1[i];
i++;
}
while(str2[j] !='\0')
{
str3[i]=str2[j];
i++;
j++;
}
str3[j]='\0';
printf("Concatenated String is:\n%s",str3) ;
return 0;
}

Output:
Enter First String:
RISE
Enter Second String:
GANDHI
Concatenated String is:
RISEGANDHI
5.
//Reverse a string using built-in stringfunctions.
#include<stdio.h>
int main( )
{
char str [20] ;
printf ("Enter a string”\n");
gets (str);
strrev (str);
printf("Reversed string = \n%s", str)
return 0;
}

//Reverse a string without using built-in stringfunctions.


#include<stdio.h>
int main()
{
char str1[20] ,str2[20] , temp;
int i,j,k;
printf("Enter String :\n");
scanf("%s",str1);
for(i=0;str1[i] !='\0';i++)
{}
for (j=i-1,k=0; j>=0;j--,k++)
{
str2[k]=str1[j];
}
str2 [k]='\0';
printf("Reverse of the string is: \n%s",str2) ;
return 0;
}

Output:

Enter a string:
CSE-DS
Reverse of the string is:
SD-ESC
Lab 8: Pointers and structures, memory dereference.
i) Write a C program to find the sum of a 1D array using malloc()
ii) Write a C program to find the total, average of n students using structures
iii) Enter n students data using calloc() and display failed students list
iv) Read student name and marks from the command line and display the student details
alongwith the total.
v) Write a C program to implement realloc()

1.
//Sum of n elements entered by using malloc() function
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, n, sum = 0;
int *p;
printf("Enter number of elements : ");
scanf( "%d", &n);
p = (int *)malloc (n * sizeof (int) );
printf("Enter %d elements into array:\n",n);
for(i=0;i<n;i++)
scanf ("%d", p + i);
for (i = 0; i< n; i++)
sum += *(p + i);
printf("sum is %d \n", sum);
free (p);
return 0;
}

Output:

Enter number of elements : 4


Enter 4 elements into array:
12
41
-23
6
sum is 36

2.
// To find the total, average of n students using structures
#include <stdio.h>
#include <stdlib.h>
// Define a structure to store student information
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
int n, i;
float total = 0.0, avg;
// Ask the user to enter the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
// Allocate memory for an array of structures
struct student *students = (struct student *)malloc(n * sizeof(struct student));
// Check if memory allocation was successful
if (students == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
// Ask the user to enter the details of each student
printf("Enter the details of %d students:\n", n);
for (i = 0; i< n; i++)
{
printf("Student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll: ");
scanf("%d", &students[i].roll);
printf("Marks: ");
scanf("%f", &students[i].marks);
// Add the marks to the total
total += students[i].marks;
}
// Calculate the average by dividing the total by n
avg = total / n;
// Print the results
printf("The total marks of %d students are: %.2f\n", n, total);
printf("The average marks of %d students are: %.2f\n", n, avg);
// Free the memory allocated for the array of structures
free(students);
return 0;
}

Output:

Enter the number of students: 4


Enter the details of 4 students:
Student 1:
Name: Roshi
Roll: 45
Marks: 76
Student 2:
Name:
Narayana
Roll: 67
Marks: 72
Student 3:
Name: Anusha
Roll: 23
Marks: 66
Student 4:
Name:
Anuradha
Roll: 5
Marks: 56
The total marks of 4 students are: 270.00
The average marks of 4 students are: 67.50
3.
//Enter n students data using calloc() and display failed students list
#include <stdio.h>
#include <stdlib.h>
// Structure to store student data
struct Student
{
char name[50];
int rollNumber;
float marks;
};
int main()
{
int n;
// Get the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
// Dynamically allocate memory for an array of n student structures
struct Student *students = (struct Student *)calloc(n, sizeof(struct Student));
// Input student data
for (int i = 0; i< n; ++i) {
printf("\nEnter data for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
// Display failed students (assuming passing marks as 40)
printf("\nList of Failed Students:\n");
printf("----------------------------\n");
for (int i = 0; i< n; ++i) {
if (students[i].marks< 40.0) {
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Marks: %.2f\n", students[i].marks);
printf("----------------------------\n");
}
}
free(students);
return 0;
}

Output:

Enter the number of students: 3


Enter data for student 1:
Name: Jessy
Roll Number: 1
Marks: 24
Enter data for student 2:
Name: Rosy
Roll Number: 2
Marks: 35
Enter data for student 3:
Name: Ricky
Roll Number: 3
Marks: 31
List of Failed Students:
----------------------------
Name: Jessy
Roll Number: 1
Marks: 24.00
----------------------------
Name: Rosy
Roll Number: 2
Marks: 35.00
----------------------------
Name: Ricky
Roll Number: 3
Marks: 31.00
----------------------------
4
/* Read student name and marks from the command line and display the student details along
with the total*/
#include <stdio.h>
#include <stdlib.h>
// Structure to store student data
struct Student
{
char name[50];
float marks;
};
int main(int argc, char *argv[])
{
// Check if the correct number of command-line arguments is provided
if (argc< 3 || (argc - 1) % 2 != 0) {
printf("Usage: %s <name1><marks1><name2><marks2> ...", argv[0]);
return 1;
}
// Calculate the number of students
int numStudents = (argc - 1) / 2;
// Dynamically allocate memory for an array of student structures
struct Student *students = (struct Student *)malloc(numStudents * sizeof(struct Student));
// Input student data from command line arguments
for (int i = 0; i<numStudents; ++i) {
sscanf(argv[i * 2 + 1], "%s", students[i].name);
sscanf(argv[i * 2 + 2], "%f", &students[i].marks);
}
// Display student details along with the total marks
printf("\nStudent Details:\n");
printf("----------------------------\n");

float totalMarks = 0.0;


for (int i = 0; i<numStudents; ++i) {
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n", students[i].marks);
totalMarks += students[i].marks;
printf("----------------------------\n");
}
printf("Total Marks: %.2f\n", totalMarks);
// Free the dynamically allocated memory
free(students);
return 0;
}

5.
// realloc() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf ("%d", &n1);
ptr = (int*) malloc (n1 * sizeof (int ) );
printf("Addresses of previous ly allocated memory:\n");
for (i = 0; i< n1; i++)
printf("%u \n",ptr + i);
printf("\nEnter the new size: ") ;
scanf ("%d", &n2) ;
ptr = realloc(ptr, n2 * sizeof (int) ) ;
printf( "Addresses of newly allocated memory:\n");
for (i = 0; i< n2; i++)
printf("%u\n", ptr + i);
free (ptr);
return 0;
}

Output:

Enter size: 5
Addresses of previous ly allocated memory:
37149376
37149380
37149384
37149388
37149392
Enter the new size: 4
Addresses of newly allocated memory:
37149376
37149380
37149384
37149388

Lab 9: Bitfields, linked lists


Read and print a date using dd/mm/yyyy format using bit-fields and differentiate the same
without using bit- fields
i) Create and display a singly linked list using self-referential structure.
ii) Demonstrate the differences between structures and unions using a C program.
iii) Write a C program to shift/rotate using bitfields.
iv) Write a C program to copy one structure variable to another structure of the same type.

1.
//Create and display a singly linked list using self-referential structure.
#include <stdio.h>
#include <stdlib.h>

// Define a node structure for the linked list


struct Node {
int data;
struct Node* next;
};

int main() {
// Initialize an empty linked list
struct Node* head = NULL;

// Insert nodes into the linked list


struct Node* newNode1 = (struct Node*)malloc(sizeof(struct Node));
newNode1->data = 10;
newNode1->next = NULL;

struct Node* newNode2 = (struct Node*)malloc(sizeof(struct Node));


newNode2->data = 20;
newNode2->next = NULL;
struct Node* newNode3 = (struct Node*)malloc(sizeof(struct Node));
newNode3->data = 30;
newNode3->next = NULL;

struct Node* newNode4 = (struct Node*)malloc(sizeof(struct Node));


newNode4->data = 40;
newNode4->next = NULL;

// Link the nodes to form the linked list


head = newNode1;
newNode1->next = newNode2;
newNode2->next = newNode3;
newNode3->next = newNode4;

// Display the linked list


printf("Linked List: ");
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");

// Free the memory used by the linked list


free(newNode1);
free(newNode2);
free(newNode3);
free(newNode4);

return 0;
}

Output:

Linked List: 10 -> 20 -> 30 -> 40 -> NULL


2.
//Differences between structures and unions.
#include <stdio.h>
// Define a structure to represent a person
struct Person
{
char name[50];
int age;
float height;
};
// Define a union to represent a storage unit that can hold an integer or a float
union StorageUnit
{
int intValue;
float floatValue;
};
int main() {
// Example of using a structure
struct Person person1;
strcpy(person1.name, "John");
person1.age = 25;
person1.height = 5.9;
printf("Person:\n");
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);
printf("\n");
// Example of using a union
union StorageUnit storage;
storage.intValue = 42;

// Accessing the same memory location as a different type (float)


printf("Storage Unit (Integer): %d\n", storage.intValue);
storage.floatValue = 3.14;
printf("Storage Unit (Float): %.2f\n", storage.floatValue);
printf("\n");

// Size comparison
printf("Size of Person structure: %zu bytes\n", sizeof(struct Person));
printf("Size of StorageUnit union: %zu bytes\n", sizeof(union StorageUnit));

return 0;
}
Output:
Person:
Name: John
Age: 25
Height: 5.90

Storage Unit (Integer): 42


Storage Unit (Float): 3.14

Size of Person structure: 60 bytes


Size of StorageUnit union: 4 bytes
3.
//Shift/rotate using bitfields.
#include <stdio.h>
// Define a bitfield structure with 8 bits
struct BitFieldExample
{
unsigned int data : 8;
};
int main()
{
// Initialize a bitfield structure
struct BitFieldExample example;
example.data = 0b11011011; // Initial value: 219 (in decimal)

// Display the initial value


printf("Initial Value: %u\n", example.data);

// Perform left rotation


example.data = (example.data<< 1) | (example.data>> 7);
printf("After Left Rotation: %u\n", example.data);

// Perform right rotation


example.data = (example.data>> 1) | (example.data<< 7);
printf("After Right Rotation: %u\n", example.data);

return 0;
}

Output:
Initial Value: 219
After Left Rotation: 183
After Right Rotation: 219
4.
//To copy one structure variable to another structure of the same type.
#include <stdio.h>
#include <string.h>
// Define a structure
struct Person
{
char name[50];
int age;
};
int main()
{
// Declare and initialize a structure variable
struct Person person1;
strcpy(person1.name, "John");
person1.age = 25;

// Declare another structure variable


struct Person person2;

// Copy the contents of person1 to person2


person2 = person1;

// Display the contents of both structure variables


printf("Person 1:\n");
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("\nPerson 2 (copied from Person 1):\n");
printf("Name: %s\n", person2.name);
printf("Age: %d\n", person2.age);
return 0;
}

Output:
Person 1:
Name: John
Age: 25

Person 2 (copied from Person 1):


Name: John
Age: 25

Lab 10: Simple functions using call by value, solving differential equations using Eulers
theorem.
i) Write a C function to calculate NCR value.
ii) Write a C function to find the length of a string.
iii) Write a C function to transpose of a matrix.
iv) Write a C function to demonstrate numerical integration of differential equations using
Euler’smethod

1.
// To calculate NCR value.
#include <stdio.h>
// Function to calculate the factorial of a number
int fact(int num)
{
int i,f=1;
if (num == 0 || num == 1)
return 1;
else
for(i=2;i<=num;i++)
f=f*i;
return f;
}
int main()
{
int n, r, ncr;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the value of r: ");
scanf("%d", &r);
ncr = fact(n)/(fact(r)*fact(n-r));
printf("The value of %dC%d is: %d\n", n, r, ncr);
return 0;
}

Output:
Enter the value of n: 5
Enter the value of r: 3
The value of 5C3 is: 10

2.
//Length of a string using function.
#include <stdio.h>
// Function to calculate the length of a string
int calculateStringLength(const char *str)
{
int length = 0;
while (str[length] != '\0')
length++;
return length;
}
int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%[^\n]s", inputString);
int length = calculateStringLength(inputString);
printf("Length of the string: %d\n", length);
return 0;
}

Output:
Enter a string: rise krishnasaigandhi
Length of the string: 23
3.
//Transpose of a matrix using function.
#include <stdio.h>
// Function to calculate the transpose of a matrix using pointers
void calculateTranspose(int rows, int cols, int (*matrix)[cols], int (*result)[rows])
{
for (int i = 0; i< rows; ++i)
for (int j = 0; j < cols; ++j)
result[j][i] = matrix[i][j];
}
// Function to display a matrix using pointers
void displayMatrix(int rows, int cols, int (*matrix)[cols])
{
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
printf("%d\t", matrix[i][j]);
printf("\n");
}
}
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
printf("Enter matrix elements:\n");
for (int i = 0; i< rows; ++i)
for (int j = 0; j < cols; ++j)
scanf("%d", &matrix[i][j]);
printf("Original Matrix:\n");
displayMatrix(rows, cols, matrix);
int transposeMatrix[cols][rows];
calculateTranspose(rows, cols, matrix, transposeMatrix);
printf("Transpose Matrix:\n");
displayMatrix(cols, rows, transposeMatrix);
return 0;
}

Output:

Enter the number of rows: 2


Enter the number of columns: 3
Enter matrix elements:
1
2
3
4
5
6
Original Matrix:
1 2 3
4 5 6
Transpose Matrix:
1 4
2 5
3 6
4.
/*Function to demonstrate numerical integration of differential equations using Euler’s method*/
#include <stdio.h>
// Function to represent the differential equation dy/dx
double differentialEquation(double x, double y)
{
return x + y; // Example: dy/dx = x + y
}
// Function to perform numerical integration using Euler's method
void eulerMethod(double x0, double y0, double h, int steps)
{
double x = x0;
double y = y0;
printf("Step\tX\tY\n");
for (int i = 0; i<= steps; ++i) {
printf("%d\t%.4lf\t%.4lf\n", i, x, y);

// Euler's method formula


y = y + h * differentialEquation(x, y);
x = x + h;
}
}
int main() {
double x0, y0, h;
int steps;
printf("Enter initial value of x (x0): ");
scanf("%lf", &x0);
printf("Enter initial value of y (y0): ");
scanf("%lf", &y0);
printf("Enter step size (h): ");
scanf("%lf", &h);
printf("Enter the number of steps: ");
scanf("%d", &steps);
// Perform numerical integration using Euler's method
eulerMethod(x0, y0, h, steps);
return 0;
}

Output:

Enter initial value of x (x0): 5


Enter initial value of y (y0): 1
Enter step size (h): .2
Enter the number of steps: 4
Step X Y
0 5.0000 1.0000
1 5.2000 2.2000
2 5.4000 3.6800
3 5.6000 5.4960
4 5.8000 7.7152

You might also like