Ce Unit-4
Ce Unit-4
int main() {
int matrix[10][10], transposed[10][10];
int rows, cols, i, j;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("\nOriginal Matrix:\n");
displayMatrix(matrix, rows, cols);
transposeMatrix(matrix, rows, cols, transposed);
printf("\nTransposed Matrix:\n");
displayMatrix(transposed, cols, rows);
return 0;
}
OUTPUT:
Enter the number of rows and columns of the matrix: 2 2
Enter the elements of the matrix:
3
4
5
6
Original Matrix:
34
56
Transposed Matrix:
35
46
4. Write a C function to demonstrate numerical integration of
differential equations using Euler’s method
#include <stdio.h>
#include <math.h>
double f(double t, double y) {
return t + y; // dy/dt = t + y
}
void eulerMethod(double (*f)(double, double), double y0, double t0, double h, int n) {
double t = t0;
double y = y0;
int i;
printf("t = %.2f, y = %.4f\n", t, y);
for (i = 1; i <= n; i++) {
y = y + h * f(t, y);
t = t + h;
printf("t = %.2f, y = %.4f\n", t, y);
}
}
int main() {
double t0, y0, h;
int n;
printf("Enter the initial value t0: ");
scanf("%lf", &t0);
printf("Enter the initial value y0: ");
scanf("%lf", &y0);
printf("Enter the step size h: ");
scanf("%lf", &h);
printf("Enter the number of steps (n): ");
scanf("%d", &n);
printf("\nUsing Euler's Method to solve the differential equation:\n");
eulerMethod(f, y0, t0, h, n);
return 0;
}
OUTPUT:
Enter the initial value t0: 0
Enter the initial value y0: 1
Enter the step size h: 1
Enter the number of steps (n): 4
return 0;
}
OUTPUT:
Enter the value of n: 5
Sum of the series 1 + 2 + 3 + ... + 5 is: 15
WEEK – 13
1. Write a C program to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a, int *b) {
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
OUTPUT:
Enter two numbers: 10 20
Before swapping: num1 = 10, num2 = 20
After swapping: num1 = 20, num2 = 10
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char text[100];
char filename[] = "example.txt";
file = fopen(filename, "w");
printf("Enter text to write to the file : ");
scanf("%s", text);
fprintf(file, "%s", text);
fclose(file);
printf("Text successfully written to %s\n", filename);
file = fopen(filename, "r");
fscanf(file, "%s", text);
printf("Reading from file:\n%s", text);
fclose(file);
return 0;
}
OUTPUT:
Filename: example.txt
Enter text to write to the file : ravindracollege
Text successfully written to example.txt
Reading from file:
ravindracollege
2. Write a C program to write and read text into a binary file using
fread() and fwrite()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *file;
char text[100];
char buffer[100];
char filename[] = "example.bin";
file = fopen(filename, "wb");
printf("Enter text to write to the binary file: ");
scanf("%s", text);
int textLength = strlen(text) ;
fwrite(text, sizeof(char), textLength, file);
fclose(file);
printf("Text successfully written to %s\n", filename);
file = fopen(filename, "rb");
fread(buffer, sizeof(char), textLength, file);
printf("Reading from binary file:\n%s", buffer);
fclose(file);
return 0;
}
OUTPUT:
Filename: example.bin
Enter text to write to the binary file: kurnool
Text successfully written to example.bin
Reading from binary file:
kurnool
fclose(sourceFile);
fclose(destinationFile);
return 0;
}
OUTPUT:
File copied successfully.
4. Write a C program to merge two files into the third file using
command-line arguments.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s input_file1 input_file2 output_file\n", argv[0]);
return 1;
}
FILE *file1, *file2, *outputFile;
char text[100];
file1 = fopen(argv[1], "w");
fprintf(file1,"ravindra");
file2 = fopen(argv[2], "w");
fprintf(file2,"college");
file1 = fopen(argv[1], "r");
file2 = fopen(argv[2], "r");
outputFile = fopen(argv[3], "w");
if (file1 == NULL || file2 == NULL || outputFile == NULL) {
printf("File opening failed.\n");
return 1;
}
while (fgets(text, sizeof(text), file1) != NULL) {
fputs(text, outputFile);
}
while (fgets(text, sizeof(text), file2) != NULL) {
fputs(text, outputFile);
}
printf("Files merged successfully");
fclose(file1);
fclose(file2);
fclose(outputFile);
return 0;
}
OUTPUT:
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
int n;
printf("Enter the filename: ");
scanf("%s", filename);
printf("Enter the number of characters to print from the end: ");
scanf("%d", &n);
file = fopen(filename, "r");
if (file == NULL) {
printf("File opening failed.\n");
return 1;
}