Lab program
Lab program
int main() {
char op;
int a, b, res;
#include <stdio.h>
int main() {
char op;
int a, b, res;
if (res != -1) {
// Display the result
printf("Result: %d\n", res);
}
return 0;
}
2. Write a C program to implement the Binary Search algorithm on an
array of integers.
2a) Variant 1: Iterative binary search for Binary Search
#include <stdio.h>
int main() {
int n, target, result;
int arr[n];
return 0;
}
int main() {
int n, target, result;
int arr[n];
return 0;
}
2c) Variant 2: Using a Separate Function to Handle Input
#include <stdio.h>
// Function to perform Binary Search
int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;
int main() {
int n, target, result;
return 0;
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
return 0;
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
return 0;
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
bubbleSort(arr, n);
return 0;
}
void multiplyMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int r1, int c1, int c2) {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0; // Initialize result cell
for (int k = 0; k < c1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
int main() {
int mat1[10][10], mat2[10][10], result[10][10];
int r1, c1, r2, c2;
// Input dimensions of matrices
printf("Enter rows and columns of first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d%d", &r2, &c2);
// Input matrices
inputMatrix(mat1, r1, c1, "A");
inputMatrix(mat2, r2, c2, "B");
// Perform multiplication
multiplyMatrices(mat1, mat2, result, r1, c1, c2);
// Display results
printf("Matrix A:\n");
displayMatrix(mat1, r1, c1);
printf("Matrix B:\n");
displayMatrix(mat2, r2, c2);
printf("Resultant Matrix (A x B):\n");
displayMatrix(result, r1, c2);
return 0;
}
void inputMatrix(int **matrix, int rows, int cols, const char *name) {
printf("Enter elements of matrix %s (%d x %d):\n", name, rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
void multiplyMatrices(int **mat1, int **mat2, int **result, int r1, int c1, int c2) {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
for (int k = 0; k < c1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
int main() {
int **mat1, **mat2, **result;
int r1, c1, r2, c2;
// Input matrices
inputMatrix(mat1, r1, c1, "A");
inputMatrix(mat2, r2, c2, "B");
// Perform multiplication
multiplyMatrices(mat1, mat2, result, r1, c1, c2);
// Display results
printf("Matrix A:\n");
displayMatrix(mat1, r1, c1);
printf("Matrix B:\n");
displayMatrix(mat2, r2, c2);
printf("Resultant Matrix (A x B):\n");
displayMatrix(result, r1, c2);
4c) Variant 3: Matrix Multiplication with Function Pointer for Operation Customization
#include <stdio.h>
void inputMatrix(int matrix[10][10], int rows, int cols, const char *name) {
printf("Enter elements of matrix %s (%d x %d):\n", name, rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
void multiplyMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int r1, int c1, int c2, int
(*operation)(int, int)) {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
for (int k = 0; k < c1; k++) {
result[i][j] += operation(mat1[i][k], mat2[k][j]);
}
}
}
}
int main() {
int mat1[10][10], mat2[10][10], result[10][10];
int r1, c1, r2, c2;
// Input matrices
inputMatrix(mat1, r1, c1, "A");
inputMatrix(mat2, r2, c2, "B");
// Display results
printf("Matrix A:\n");
displayMatrix(mat1, r1, c1);
printf("Matrix B:\n");
displayMatrix(mat2, r2, c2);
printf("Resultant Matrix (A x B):\n");
displayMatrix(result, r1, c2);
return 0;
}
5. An electricity board charges the following rates for the use of electricity:
for the first 200 units 80 paise per unit: for the next 100 units 90 paise
per unit: beyond 300 units Rs 1 per unit. All users are charged a
minimum of Rs. 100 as meter charge. If the total amount is more than
Rs 400, then an additional surcharge of 15% of total amount is charged.
Write a program to read the name of the user, number of units
consumed and print out the charges.
5a) Variant 1: Using Function for Surcharge Calculation
#include <stdio.h>
#include <string.h>
void main() {
int units;
float charge, surcharge = 0, amt, total_amt;
char name[25];
void main() {
int units;
float charge, surcharge = 0, amt, total_amt;
char name[25];
void main() {
int units, continue_flag;
float charge, surcharge = 0, amt, total_amt;
char name[25];
do {
// Input customer details
printf("Enter the customer Name: ");
scanf("%s", name);
// Compare strings
if (compareStrings(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
// Concatenate strings
concatenateStrings(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
6b) Variant 2: Using Structs for String Operations
#include <stdio.h>
#include <string.h>
typedef struct {
char str1[200];
char str2[100];
} StringOperations;
// Compare strings
if (compareStrings(&strOps) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
// Concatenate strings
concatenateStrings(&strOps);
printf("Concatenated string: %s\n", strOps.str1);
return 0;
}
6c) Variant 3: Using Dynamic Memory Allocation for String Inputs
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
if (result == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
int main() {
char *str1, *str2;
size_t size1 = 200, size2 = 100;
// Concatenate strings
char *concatenated = concatenateStrings(str1, str2);
printf("Concatenated string: %s\n", concatenated);
return 0;
}
7. Write a c program to implement structures to read, write and compute
average- marks of the students, list the students scoring above and below the
average marks for a class of N students.
7a) Variant 1: Using Dynamic Memory Allocation for Student Records
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
int n;
return 0;
}
7b) Variant 2: Using Linked List to Store Student Records
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int marks;
struct Student *next;
};
int main() {
int n;
if (head == NULL) {
head = newStudent;
} else {
temp->next = newStudent;
}
temp = newStudent;
}
return 0;
}
7c) Variant 3: Using Separate Functions for Student Input and Output
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
int n;
// Compute average
float average = computeAverage(students, n);
// Output results
printf("\nAverage Marks: %.2f\n", average);
printStudentResults(students, n, average);
return 0;
}
8. Write a C program to copy a text file to another, read both the input file
name and target file name.
8a)Variant 1: Using fread and fwrite for Binary File Copying
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *sourceFile, *targetFile;
char sourceFileName[100], targetFileName[100];
char ch;
return 0;
}
int main() {
FILE *sourceFile, *targetFile;
char sourceFileName[100], targetFileName[100];
char ch;
long position;
// Prompt the user to enter the starting position in the source file
printf("Enter the position to start copying from (in bytes): ");
scanf("%ld", &position);
// Copy the contents from the current position in the source file to the target file
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, targetFile);
}
printf("File content copied successfully from %s to %s starting from position %ld\n", sourceFileName,
targetFileName, position);
return 0;
}