DSA Part C Answers
DSA Part C Answers
DSA Part C Answers
#include <stdio.h>
struct student {
char name[50];
int roll;
float marks[3];
float total;
};
int main() {
struct student s;
Output
Enter student name: Rayean
Enter student roll number: 67875
Enter marks for subject 1: 77
Enter marks for subject 2: 67
Enter marks for subject 3: 98
Output
Title: The Alchemist
Author: Paulo Coelho
Pages: 208
Price: 12.99
calculateArea(&rect);
printf("The area of the rectangle is: %.2f\n", rect.area);
return 0;
}
Output
Enter the length of the rectangle: 3
Enter the width of the rectangle: 4
The area of the rectangle is: 12.00
Trees: A tree is a data structure that consists of a root node and a set of child nodes.
The root node is the top node of the tree, and the child nodes are connected to the
root node by pointers. Each child node can also have its own child nodes, and so on.
Graphs: A graph is a data structure that consists of a set of vertices and a set of
edges. The vertices are the nodes of the graph, and the edges are the connections
between the nodes.
Self-referential structures are a powerful tool for creating complex data structures. They are
used in a variety of applications, such as operating systems, databases, and compilers.
Matrix Multiplication
#include <stdio.h>
#include <stdlib.h>
void matrix_multiplication(int *a, int *b, int *c, int m, int n, int p) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
int sum = 0;
for (int k = 0; k < n; k++) {
sum += a[i * n + k] * b[k * p + j];
}
c[i * p + j] = sum;
}
}
}
int main() {
int m, n, p;
int *a, *b, *c;
printf("Enter the number of rows in the first matrix : ");
scanf("%d", &m);
printf("Enter the number of columns in the first matrix : ");
scanf("%d", &n);
printf("The number of rows in the second matrix : ");
scanf("%d", &p);
a = malloc(m * n * sizeof(int));
b = malloc(n * p * sizeof(int));
c = malloc(m * p * sizeof(int));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("Enter element a[%d][%d]: ", i, j);
scanf("%d", &a[i * n + j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
printf("Enter element b[%d][%d]: ", i, j);
scanf("%d", &b[i * p + j]);
}
}
matrix_multiplication(a, b, c, m, n, p);
printf("The product of the matrices is: \n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
printf("%d ", c[i * p + j]);
}
printf("\n");
}
free(a);
free(b);
free(c);
return 0;
}
Output
Enter the number of rows in the first matrix : 2
Enter the number of columns in the first matrix : 2
The number of rows in the second matrix : 2
Enter element a[0][0]: 1
Enter element a[0][1]: 2
Enter element a[1][0]: 3
Enter element a[1][1]: 4
Enter element b[0][0]: 1
Enter element b[0][1]: 2
Enter element b[1][0]: 3
Enter element b[1][1]: 4
Write a C program for employee details using pointers and display the details of
employees who have less than Rs.5000/- salary.
#include <stdio.h>
#include <stdlib.h>
struct employee {
char name[50];
int id;
int salary;
};
void print_employees(struct employee *employees, int n) {
for (int i = 0; i < n; i++) {
if (employees[i].salary < 5000) {
printf("Name: %s\n", employees[i].name);
printf("ID: %d\n", employees[i].id);
printf("Salary: %d\n", employees[i].salary);
}
}
}
int main() {
int n;
printf("Enter the number of employees: ");
scanf("%d", &n);
struct employee *employees = malloc(n * sizeof(struct employee));
for (int i = 0; i < n; i++) {
printf("Enter the name of employee %d: ", i + 1);
scanf("%s", employees[i].name);
printf("Enter the ID of employee %d: ", i + 1);
scanf("%d", &employees[i].id);
printf("Enter the salary of employee %d: ", i + 1);
scanf("%d", &employees[i].salary);
}
print_employees(employees, n);
free(employees);
return 0;
}
Output
Enter the number of employees: 2
Enter the name of employee 1: Rayean
Enter the ID of employee 1: 65
Enter the salary of employee 1: 4899
Enter the name of employee 2: Sanjana
Enter the ID of employee 2: 85
Enter the salary of employee 2: 5005
Name: Rayean
ID: 65
Salary: 4899
Explain the various asymptotic notations that are helpful for analysing the efficiency of an
algorithm.
Asymptotic notations are mathematical expressions that are used to describe the running
time of an algorithm as the input size grows. They are helpful for analyzing the efficiency of
an algorithm because they allow us to compare the running times of different algorithms
without having to worry about the specific details of the implementation.
There are three main asymptotic notations:
Big O notation (O): This notation describes the upper bound of the running time of
an algorithm. In other words, it represents the worst-case running time of the
algorithm.
Omega notation (Ω): This notation describes the lower bound of the running time of
an algorithm. In other words, it represents the best-case running time of the
algorithm.
Theta notation (Θ): This notation describes the average-case running time of an
algorithm. In other words, it represents the running time of the algorithm for most
inputs.
For example, the running time of the bubble sort algorithm is O(n^2), where n is the size of
the input array. This means that the worst-case running time of bubble sort is quadratic, i.e.
it grows as the square of the input size.
The running time of the quicksort algorithm is Ω(n log n), Θ(n log n), where n is the size of
the input array. This means that the best-case, worst-case, and average-case running times
of quicksort are all logarithmic, i.e. they grow as the logarithm of the input size.
Asymptotic notations are a powerful tool for analysing the efficiency of algorithms. They
allow us to compare the running times of different algorithms without having to worry
about the specific details of the implementation. This makes them a valuable tool for
computer scientists and software engineers.
Create a C structure for the library management system. Write a C program to perform the
following operations (i) adding a record. (ii) deleting a record (iii) modifying a record (iv)
displaying the records for the above application.
#include <stdio.h>
#include <stdlib.h>
struct book {
int book_id;
char book_name[50];
char author[50];
int no_of_pages;
float price;
};
void add_book(struct book *books, int *n) {
struct book book;
printf("Enter the book ID: ");
scanf("%d", &book.book_id);
printf("Enter the book name: ");
scanf("%s", book.book_name);
printf("Enter the author name: ");
scanf("%s", book.author);
printf("Enter the number of pages: ");
scanf("%d", &book.no_of_pages);
printf("Enter the price: ");
scanf("%f", &book.price);
books[*n] = book;
(*n)++;
}
void delete_book(struct book *books, int *n, int book_id) {
int i;
for (i = 0; i < *n; i++) {
if (books[i].book_id == book_id) {
break;
}
}
if (i == *n) {
printf("Book not found!\n");
return;
}
(*n)--;
}
void modify_book(struct book *books, int *n, int book_id) {
int i;
for (i = 0; i < *n; i++) {
if (books[i].book_id == book_id) {
break;
}
}
if (i == *n) {
printf("Book not found!\n");
return;
}
switch (choice) {
case 1:
add_book(books, &n);
break;
case 2:
printf("Enter the book ID to delete: ");
int book_id;
scanf("%d", &book_id);
delete_book(books, &n, book_id);
break;
case 3:
printf("Enter the book ID to modify: ");
scanf("%d", &book_id);
modify_book(books, &n, book_id);
break;
case 4:
display_books(books, n);
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice!\n");
}
}
free(books);
return 0;
}
Output
Enter the number of books: 2
Select an operation:
1. Add book
2. Delete book
3. Modify book
4. Display books
5. Exit
1
Enter the book ID: 65
Enter the book name: Romeo Juliet
Enter the author name: Shakespeare
Enter the number of pages: 120
Enter the price: 100