Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Part C

Write a C program for student mark sheet using structure

#include <stdio.h>
struct student {
char name[50];
int roll;
float marks[3];
float total;
};
int main() {
struct student s;

printf("Enter student name: ");


scanf("%s", s.name);
printf("Enter student roll number: ");
scanf("%d", &s.roll);

for (int i = 0; i < 3; i++) {


printf("Enter marks for subject %d: ", i + 1);
scanf("%f", &s.marks[i]);
}
s.total = s.marks[0] + s.marks[1] + s.marks[2];
printf("\nStudent mark sheet:");
printf("\nName: %s", s.name);
printf("\nRoll number: %d", s.roll);
printf("\nMarks:");
for (int i = 0; i < 3; i++) {
printf("\t%.2f", s.marks[i]);
}
printf("\nTotal: %.2f", s.total);
return 0;
}

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

Student mark sheet:


Name: Rayean
Roll number: 67875
Marks: 77.00 67.00 98.00
Total: 242.00

Write a C program for book details using structure and pointers


#include <stdio.h>
#include <stdlib.h>
struct book {
char title[50];
char author[50];
int pages;
float price;
};

void print_book(struct book *book) {


printf("Title: %s\n", book->title);
printf("Author: %s\n", book->author);
printf("Pages: %d\n", book->pages);
printf("Price: %.2f\n", book->price);
}
int main() {
struct book *book1 = malloc(sizeof(struct book));
strcpy(book1->title, "The Alchemist");
strcpy(book1->author, "Paulo Coelho");
book1->pages = 208;
book1->price = 12.99;
print_book(book1);
free(book1);
return 0;
}

Output
Title: The Alchemist
Author: Paulo Coelho
Pages: 208
Price: 12.99

3. Calculating the area of a rectangle using structures in C language.


#include <stdio.h>
struct Rectangle {
float length;
float width;
float area;
};
void calculateArea(struct Rectangle *rect) {
rect->area = rect->length * rect->width;
}
int main() {
struct Rectangle rect;
printf("Enter the length of the rectangle: ");
scanf("%f", &rect.length);
printf("Enter the width of the rectangle: ");
scanf("%f", &rect.width);

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

Explain self-referential structures with suitable examples.


A self-referential structure is a structure that contains a pointer to itself. This means that the
structure can refer to itself, creating a linked data structure. Self-referential structures are a
powerful tool for creating complex data structures in C and are commonly used in
algorithms such as trees, graphs, and linked lists.
Here is an example of a self-referential structure in C:
struct Node {
int data;
struct Node *next;
};
The `Node` structure has two members: `data` and `next`. The `data` member stores the
data of the node, and the `next` member stores a pointer to the next node in the linked list.
The `next` member of a `Node` structure is a self-referential pointer, because it points to
another `Node` structure. This allows the `Node` structure to refer to itself, creating a linked
list.
Self-referential structures can be used to create a variety of complex data structures. Here
are a few examples:
 Linked lists: A linked list is a data structure that consists of a sequence of nodes.
Each node in a linked list contains data and a pointer to the next node in the list. The
last node in the list has a pointer to NULL.

 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

The product of the matrices is:


7 10
15 22

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;
}

for (; i < *n - 1; i++) {


books[i] = books[i + 1];
}

(*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;
}

printf("Enter the new book name: ");


scanf("%s", books[i].book_name);
printf("Enter the new author name: ");
scanf("%s", books[i].author);
printf("Enter the new number of pages: ");
scanf("%d", &books[i].no_of_pages);
printf("Enter the new price: ");
scanf("%f", &books[i].price);
}
void display_books(struct book *books, int n) {
for (int i = 0; i < n; i++) {
printf("Book ID: %d\n", books[i].book_id);
printf("Book name: %s\n", books[i].book_name);
printf("Author: %s\n", books[i].author);
printf("Number of pages: %d\n", books[i].no_of_pages);
printf("Price: %.2f\n", books[i].price);
}
}
int main() {
int n;
struct book *books;
printf("Enter the number of books: ");
scanf("%d", &n);
books = malloc(n * sizeof(struct book));
int choice;
while (1) {
printf("\nSelect an operation:\n");
printf("1. Add book\n");
printf("2. Delete book\n");
printf("3. Modify book\n");
printf("4. Display books\n");
printf("5. Exit\n");
scanf("%d", &choice);

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

You might also like