SWAYAM DAY10PTC
SWAYAM DAY10PTC
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
return 0;
}
int main() {
char str[100];
return 0;
}
3. Write a program to dynamically allocate, initialize, and free a 3D array using pointers.
#include <stdio.h>
#include <stdlib.h>
int main() {
int x, y, z;
start = *str;
while (*start) {
while (*start && *start != ' ') {
start++;
}
end = start - 1;
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
int main() {
char str[100] = "This is a sentence";
reverse_words(&str);
printf("Reversed sentence: %s\n", str);
return 0;
}
5. Write a program that uses a function pointer array to implement a simple calculator
(addition, subtraction, multiplication, division).
#include <stdio.h>
int main() {
int (*operations[4])(int, int) = {add, subtract, multiply, divide};
int choice, num1, num2, result;
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("Enter your choice: ");
scanf("%d", &choice);
return 0;
}
6. Write a program to sort an array of integers using a generic comparison function and a
function pointer.
#include <stdio.h>
void bubble_sort(void *arr, int size, size_t element_size, int (*compare)(const void*, const
void*)) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (compare((char*)arr + j * element_size, (char*)arr + (j + 1) * element_size) > 0) {
// Swap elements
void *temp = malloc(element_size);
memcpy(temp, (char*)arr + j * element_size, element_size);
memcpy((char*)arr + j * element_size, (char*)arr + (j + 1) * element_size,
element_size);
memcpy((char*)arr + (j + 1) * element_size, temp, element_size);
free(temp);
}
}
}
}
int main() {
int arr[] = {5, 2, 9, 1, 5};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
7. Write a program that uses void pointers to implement a generic sort function for arrays of
different data types (int, float, char, etc.).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bubble_sort(void *arr, int size, size_t element_size, int (*compare)(const void*, const
void*)) {
free(temp);
int main() {
// ...
return 0;
8.Write a program that demonstrates the use of function pointers to traverse and execute multiple
algorithms on the same data set (e.g., finding the sum, average, and maximum of an array).
#include <stdio.h>
int sum(int arr[], int size) {
int total = 0;
for (int i = 0; i < size; i++) {
total += arr[i];
}
return total;
}
int main() {
int arr[] = {10, 5, 20, 3, 8};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
9. Design a small library management system where function pointers are used to perform
operations like adding a book, searching for a
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char title[100];
char author[50];
int year;
} Book;
// Library operations
int main() {
Book library[100];
int num_books = 0;
library_operation = search_book;
library_operation(library, num_books);
return 0;
10. Write a program that uses a function pointer to implement a state machine, simulating a traffic
light system (Red, Green, Yellow).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char title[100];
char author[50];
int year;
} Book;
// Library operations
int main() {
Book library[100];
int num_books = 0;
library_operation(library, &num_books);
library_operation = search_book;
library_operation(library, num_books);
11. Write a program to dynamically allocate memory for a string, read user input, and reverse the
string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str;
int length;
if (str == NULL) {
return 1;
str[j] = temp;
return 0;