Day12 Assignment
Day12 Assignment
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
char op;
printf("Enter i to increase the size and d to decrease the size of the array:
");
scanf(" %c", &op);
int newSize;
switch(op) {
case 'i':
printf("Enter the new size: ");
scanf("%d", &newSize);
if(newSize <= size) {
printf("New size is equal to or less than the current size\n");
return 1;
}
arr = realloc(arr, newSize * sizeof(int));
if (arr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
printf("Enter new elements of the array:\n");
for (int i = size; i < newSize; i++) {
scanf("%d", &arr[i]);
}
size = newSize;
break;
case 'd':
printf("Enter the new size: ");
scanf("%d", &newSize);
if(newSize >= size) {
printf("New size is equal to or greater than the current
size\n");
return 1;
}
arr = realloc(arr, newSize * sizeof(int));
if (arr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
size = newSize;
break;
default:
printf("Invalid option\n");
return 1;
}
free(arr);
return 0;
}
2.
/*Problem 2: String Concatenation Using Dynamic Memory
Objective: Create a program that concatenates two strings using dynamic memory
allocation.
Description:
Accept two strings from the user.
Use malloc to allocate memory for the first string.
Use realloc to resize the memory to accommodate the concatenated string.
Concatenate the strings and print the result.
Free the allocated memory.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str1 = (char*)malloc(100 * sizeof(char));
if (str1 == NULL) {
printf("Memory allocation failed\n");
return 1;
}
char str2[100];
str1 = (char*)realloc(str1,(strlen(str2)+1));
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
free(str1);
return 0;
}
3.
/*Problem 3: Sparse Matrix Representation
Objective: Represent a sparse matrix using dynamic memory allocation.
Description:
Accept a matrix of size m×nm \times nm×n from the user.
Store only the non-zero elements in a dynamically allocated array of
structures (with fields for row, column, and value).
Print the sparse matrix representation.
Free the allocated memory at the end.*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int m, n;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int matrix[m][n];
int non_zero = 0;
printf("\nOriginal Matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j]!= 0) {
rows[count] = i;
cols[count] = j;
values[count] = matrix[i][j];
count++;
}
}
}
free(rows);
free(cols);
free(values);
return 0;
}
4.
/*Problem 5: Dynamic 2D Array Allocation
Objective: Write a program to dynamically allocate a 2D array.
Description:
Accept the number of rows and columns from the user.
Use malloc (or calloc) to allocate memory for the rows and columns dynamically.
Allow the user to input values into the 2D array.
Print the array in matrix format.
Free all allocated memory at the end.*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
return 0;
5.
//Student Record Management
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student {
char name[50];
int rollNumber;
float marks;
};
int main() {
struct student students[100];
int choice;
int numStudents = 0;
while(1) {
printf("\n1. Add Student\n2. Display All Students\n3. Find Student by
Roll Number\n4. Calculate Average Marks\n5. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
AddStudent(students, &numStudents);
break;
case 2:
DisplayAll(students, numStudents);
break;
case 3:
FindStudent(students, numStudents);
break;
case 4:
AverageMarks(students, numStudents);
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
float sum = 0;
for(int i = 0; i < numStudents; i++) {
sum += students[i].marks;
}
printf("Average marks: %0.2f\n", sum/numStudents);
}
6.
/*Problem 1: Employee Management System
Objective: Create a program to manage employee details using structures.
Description:
Define a structure Employee with fields:
int emp_id: Employee ID
char name[50]: Employee name
float salary: Employee salary
Write a menu-driven program to:
Add an employee.
Update employee salary by ID.
Display all employee details.
Find and display details of the employee with the highest salary.*/
#include <stdio.h>
#include <string.h>
struct Employee {
int emp_id;
char name[50];
float salary;
};
int main() {
struct Employee employees[100];
int employeeCount = 0;
int choice;
while (1) {
printf("\n==============================\n");
printf(" Employee Management System \n");
printf("==============================\n");
printf("1. Add Employee\n");
printf("2. Update Employee Salary\n");
printf("3. Display All Employee Details\n");
printf("4. Find and Display Details of Employee with Highest Salary\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee(employees, &employeeCount);
break;
case 2:
updateSalary(employees, employeeCount);
break;
case 3:
displayAll(employees, employeeCount);
break;
case 4:
highestSalary(employees, employeeCount);
break;
case 5:
printf("\nExiting... Goodbye!\n");
return 0;
default:
printf("\nInvalid choice. Please try again.\n");
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct Book {
int book_id;
char title[100];
char author[50];
int copies;
};
int main() {
struct Book books[100];
int noOfBooks = 0;
int choice;
while(1) {
printf("\nLibrary Management System\n");
printf("1. Add book\n");
printf("2. Issue book\n");
printf("3. Return book\n");
printf("4. Search book\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addBook(books, &noOfBooks);
break;
case 2:
issueBook(books, noOfBooks);
break;
case 3:
returnBook(books, noOfBooks);
break;
case 4:
searchBook(books, noOfBooks);
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
(*noOfBooks)++;
printf("Book added successfully.\n");
}
int found = 0;
for (int i = 0; i < noOfBooks; i++) {
char titleLower[100], authorLower[50];
for (int j = 0; books[i].title[j]; j++) {
titleLower[j] = tolower(books[i].title[j]);
}
titleLower[strlen(books[i].title)] = '\0';
if (!found) {
printf("No books found matching the search query.\n");
}
}
8.
/*Problem 3: Cricket Player Statistics
Objective: Store and analyze cricket player performance data.
Description:
Define a structure Player with fields:
char name[50]: Player name
int matches: Number of matches played
int runs: Total runs scored
float average: Batting average
Write a program to:
Input details for n players.
Calculate and display the batting average for each player.
Find and display the player with the highest batting average.*/
#include <stdio.h>
#include <string.h>
struct Player {
char name[50];
int matches;
int runs;
float average;
};
int main() {
int noOfPlayers;
printf("Enter the number of players: ");
scanf("%d", &noOfPlayers);
int choice;
while(1) {
printf("\n1. Input player details\n2. Calculate batting average\n3. Find
player with highest batting average\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addDetails(players, noOfPlayers);
break;
case 2:
battingAverage(players, noOfPlayers);
break;
case 3:
highestAverage(players, noOfPlayers);
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
struct Student {
int roll_no;
char name[50];
float marks[5];
char grade;
};
int main() {
int noOfStudents;
printf("Enter the number of students: ");
scanf("%d", &noOfStudents);
int choice;
while(1) {
printf("\n1. Input student details\n2. Calculate grades\n3. Display
student details with grades\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addDetails(students, noOfStudents);
break;
case 2:
calculateGrade(students, noOfStudents);
break;
case 3:
displayDetails(students, noOfStudents);
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
struct Flight {
char flight_number[10];
char destination[50];
int available_seats;
};
int main() {
struct Flight flights[100];
int noOfFlights = 0;
int choice;
while(1) {
printf("\nFlight Reservation System\n");
printf("1. Add flight\n");
printf("2. Book ticket\n");
printf("3. Cancel ticket\n");
printf("4. Display flights by destination\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addFlight(flights, &noOfFlights);
break;
case 2:
bookTicket(flights, noOfFlights);
break;
case 3:
cancelTicket(flights, noOfFlights);
break;
case 4:
displayFlights(flights, noOfFlights);
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
(*noOfFlights)++;
printf("Flight added successfully.\n");
}
int found = 0;
for (int i = 0; i < noOfFlights; i++) {
if (strcmp(flights[i].destination, destination) == 0) {
printf("\nFlight Number: %s\n", flights[i].flight_number);
printf("Destination: %s\n", flights[i].destination);
printf("Available Seats: %d\n", flights[i].available_seats);
found = 1;
}
}
if (!found) {
printf("No flights available to %s.\n", destination);
}
}