Programming Assignment - 1: Program 1
Programming Assignment - 1: Program 1
PROGRAM 1
#include <stdio.h>
#include <stdbool.h>
void countText(const char *text, int *words, int *characters, int *lines) {
*words = *characters = *lines = 0;
bool inWord = false;
int main() {
char text[1000];
1. Initialize Counters:
• Set counters for characters, words, and lines to 0.
• Define a boolean flag inWord to track whether the program is
currently inside a word.
2. Read the Input:
• Accept the input text from the user.
3. Process Each Character:
• For each character in the input:
1. Increment the characters counter.
2. If the character is a newline ('\n'), increment the lines
counter.
3. If the character is a space (' '), tab ('\t'), or newline ('\n'):
• Set inWord to false (indicating the end of a word).
4. If the character is not a space, tab, or newline, and inWord is
false:
• Increment the words counter.
• Set inWord to true (indicating the start of a word).
4. Handle the Last Line:
• If the input text does not end with a newline, increment the lines
counter to account for the last line.
5. Output the Results:
• Print the values of characters, words, and lines.
OUTPUT
PROGRAM 2
Create a structure/class for a group of 50 students holding data for their Regn
no., Name, Branch, CGPA
a) Call linear search function to display data of student with a particular Regn
no..
b) Call bubble sort function to arrange data of students according to Regn no.
c) Apply binary search on the above output (part b) to display data of a student
with a particular Regn no.
d) Use and modify Insertion sort logic to arrange data of students in
descending order of CGPA
#define MAX_STUDENTS 50
// Function prototypes
void linearSearch(Student students[], int n, int regn_no);
void bubbleSort(Student students[], int n);
int binarySearch(Student students[], int n, int regn_no);
void insertionSortDescending(Student students[], int n);
void displayStudent(Student student);
// Main function
int main() {
Student students[MAX_STUDENTS];
int n;
int choice;
do {
printf("\nMenu:\n");
printf("1. Linear Search by Registration Number\n");
printf("2. Bubble Sort by Registration Number\n");
printf("3. Binary Search by Registration Number\n");
printf("4. Insertion Sort by CGPA (Descending)\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
int regn_no;
printf("Enter Registration Number to search: ");
scanf("%d", ®n_no);
linearSearch(students, n, regn_no);
break;
}
case 2:
bubbleSort(students, n);
printf("\nStudents sorted by Registration Number:\n");
for (int i = 0; i < n; i++) {
displayStudent(students[i]);
}
break;
case 3: {
int regn_no;
printf("Enter Registration Number to search: ");
scanf("%d", ®n_no);
int index = binarySearch(students, n, regn_no);
if (index != -1) {
printf("\nStudent found:\n");
displayStudent(students[index]);
} else {
return 0;
}
// Function definitions
void linearSearch(Student students[], int n, int regn_no) {
for (int i = 0; i < n; i++) {
if (students[i].regn_no == regn_no) {
printf("\nStudent found:\n");
displayStudent(students[i]);
return;
}
}
printf("\nStudent not found.\n");
}
1. Initialize Program
• Define a structure Student to hold:
• Registration number (regn_no).
• Name (name).
• Branch (branch).
• CGPA (cgpa).
• Create an array to store data for up to MAX_STUDENTS.
2. Input Student Data
• Ask the user for the number of students (n).
• For each student, input their:
• Registration number.
• Name.
• Branch.
• CGPA.
3. Menu Options
• Provide the user with the following options:
1. Linear Search by Registration Number.
2. Bubble Sort by Registration Number.
3. Binary Search by Registration Number.
4. Insertion Sort by CGPA in Descending Order.
5. Exit.
Program Termination
• Exit the program when the user selects the "Exit" option.
OUTPUT
PROGRAM 3
Develop a C program to multiply two polynomials A(x) and B(x) using arrays
and store the resultant in C(x).
#include <stdio.h>
// Function to multiply two polynomials A(x) and B(x) and store the result in
C(x)
void multiplyPolynomials(int A[], int B[], int C[], int degA, int degB) {
// Initialize all coefficients of C(x) to 0
for (int i = 0; i <= degA + degB; i++) {
C[i] = 0;
}
// Multiply each term of A(x) with each term of B(x) and add to C(x)
for (int i = 0; i <= degA; i++) {
for (int j = 0; j <= degB; j++) {
C[i + j] += A[i] * B[j];
}
}
}
int main() {
int degA, degB;
return 0;
}
ALGORITHM
#include <stdio.h>
#define MAX 100
typedef struct {
int row;
int col;
int value;
} SparseMatrix;
C[0].row = A[0].row;
C[0].col = A[0].col;
while (i <= A[0].value && j <= B[0].value) {
if (A[i].row < B[j].row || (A[i].row == B[j].row && A[i].col < B[j].col)) {
C[k++] = A[i++];
} else if (B[j].row < A[i].row || (B[j].row == A[i].row && B[j].col < A[i].col))
{
C[k++] = B[j++];
} else {
C[k].row = A[i].row;
C[k].col = A[i].col;
C[k++].value = A[i++].value + B[j++].value;
}
}
while (i <= A[0].value) {
C[k++] = A[i++];
}
while (j <= B[0].value) {
C[k++] = B[j++];
}
C[0].value = k - 1;
}
T[0].row = A[0].col;
T[0].col = A[0].row;
T[0].value = A[0].value;
int main() {
SparseMatrix A[MAX], B[MAX], C[MAX], T[MAX];
int n, m, nonZeroA, nonZeroB, choice;
printf("Enter the dimensions of the first sparse matrix (rows and columns):
");
scanf("%d %d", &A[0].row, &A[0].col);
printf("Choose an operation:\n");
printf("1. Add two sparse matrices\n2. Find the fast transpose\n");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter the dimensions of the second sparse matrix (rows and
columns): ");
scanf("%d %d", &B[0].row, &B[0].col);
addSparseMatrices(A, B, C);
printf("Resultant matrix after addition:\n");
for (int i = 0; i <= C[0].value; i++) {
printf("%d %d %d\n", C[i].row, C[i].col, C[i].value);
}
} else if (choice == 2) {
fastTranspose(A, T);
printf("Transpose of the matrix:\n");
for (int i = 0; i <= T[0].value; i++) {
printf("%d %d %d\n", T[i].row, T[i].col, T[i].value);
}
} else {
printf("Invalid choice!\n");
}
return 0;
}
ALGORITHM
OUTPUT
PROGRAM 5
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int vowels, consonants;
return 0;
}
ALGORITHM
OUTPUT
PROGRAM 6
Given a stack of N integers. In one operation, you can either pop an element
from the stack or push any popped element into the stack. You need to
maximize the top element of the stack after performing exactly K operations. If
the stack becomes empty after performing K operations and there is no other
way for the stack to be non-empty, print -1.
#include <stdio.h>
#include <limits.h>
int main() {
int stack[MAX], n, k;
if (result == -1) {
printf("-1\n");
} else {
printf("The maximum top element after %d operations is: %d\n", k,
result);
}
return 0;
}
ALGORITHM
OUTPUT
PROGRAM 7
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
int main() {
int number;
return 0;
}
ALGORITHM
OUTPUT
PROGRAM 8
Given a list, split it into two sublists — one for the front half, and one for the
back half. If the number of elements is odd, the extra element should go in the
front list. So FrontBack Split() on the list {2, 3, 5, 7, 11} should yield the two
lists {2, 3, 5} and {7, 11}.
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = {2, 3, 5, 7, 11}; // Example list
int length = sizeof(arr) / sizeof(arr[0]);
return 0;
}
ALGORITHM
OUTPUT
PROGRAM 9
Write a C program to sort the singly linked list of integers in ascending order
#include <stdio.h>
#include <stdlib.h>
int swapped;
struct Node *ptr1, *ptr2;
// If current node's data is greater than next node's data, swap them
if (ptr1->data > ptr2->data) {
int temp = ptr1->data;
ptr1->data = ptr2->data;
ptr2->data = temp;
swapped = 1;
}
ptr1 = ptr1->next;
}
} while (swapped); // Continue sorting until no swaps are made
}
int main() {
struct Node* head = NULL;
ALGORITHM
The Bubble Sort algorithm for a singly linked list consists of the following steps:
1. Initialize:
• Start with the head of the linked list.
• If the list is empty (head is NULL), there is no need to sort.
2. Outer Loop:
• Perform repeated steps until no more swaps are needed (no
elements are swapped in a full pass through the list).
• Use a flag swapped to indicate whether any nodes were swapped
during the current pass.
3. Inner Loop:
• Traverse the linked list using two pointers, ptr1 and ptr2, where
ptr1 moves through each node, and ptr2 points to the next node.
• Compare the data of ptr1 and ptr2.
• If the data of ptr1 is greater than the data of ptr2, swap their data
(exchange the values of ptr1 and ptr2).
4. Repeat the Inner Loop:
• Continue this process until you reach the end of the list (i.e., the
last node has been compared).
5. Repeat the Outer Loop:
• If any swaps were made in the current pass (swapped is True),
repeat the process to ensure the list is fully sorted.
6. Return the Sorted List:
• Once no swaps are made in a pass, the linked list is sorted.
OUTPUT
PROGRAM 10
Alice and Bob are playing a game of Blobby Volley. In this game, in each turn,
one player is the server and the other player is the receiver. Initially, Alice is
the server, and Bob is the receiver.If the server wins the point in this turn, their
score increases by 1, and they remain as the server for the next turn. But if the
receiver wins the point in this turn, their score does not increase. But they
become the server in the next turn. In other words, your score increases only
when you win a point when you are the server. Please see the Sample Inputs
and Explanation for more detailed explanation. They start with a score of 00
each, and play N turns. The winner of each of those hands is given to you as a
string consisting of 'A's and 'B's. 'A' denoting that Alice won that point, and 'B'
denoting that Bob won that point. Your job is the find the score of both of them
after the N turns.
#include <stdio.h>
#include <string.h>
int main() {
int alice_score = 0, bob_score = 0;
char server = 'A'; // Alice starts as the server
char result[101]; // Array to hold the result string (max 100 turns)
return 0;
}
ALGORITHM
OUTPUT
PROGRAM 11
Your best friend has a very interesting necklace with n pearls. On each of the
pearls of the necklace there is an integer. However, your friend wants to
modify the necklace a bit and asks you for help. She wants to move the first
pearl k spots to the left (and do so with all other pearls). For example: if the
necklace was originally 1,5,3,4,21,5,3,4,2 and =2k=2, now it becomes
3,4,2,1,53,4,2,1,5. Help your best friend determine how the necklace will look
after the modification.
#include <stdio.h>
int main() {
int arr[] = {1, 5, 3, 4, 21, 5, 3, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
rotateLeft(arr, n, k);
return 0;
}
ALGORITHM
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
int N;
int A[N];
return 0;
}
ALGORITHM
PROGRAM 13
Design and implement a food ordering system for a restaurant using a circular
queue data structure. The system should manage incoming food orders from
multiple customers and allocate kitchen staff to prepare the orders efficiently.
The system should allow the user to perform the following operations:
1. Enqueue a food order into the queue with a unique order number,
customer name, and list of food items ordered.
2. Dequeue an order from the queue (when it has been processed by the
kitchen staff).
3. Display the current state of the queue with the order numbers, customer
names, and list of food items ordered.
4. Exit the program.
#include <stdio.h>
#include <string.h>
#define MAX 5
typedef struct {
int orderNumber;
char customerName[30];
char foodItems[100];
} Order;
Order queue[MAX];
int front = -1, rear = -1;
void dequeue() {
if (front == -1) {
printf("Queue is empty! No orders to process.\n");
return;
}
printf("Processing Order #%d\n", queue[front].orderNumber);
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
void displayQueue() {
if (front == -1) {
printf("Queue is empty!\n");
return;
}
printf("Current Orders in Queue:\n");
int i = front;
while (1) {
printf("Order #%d, Customer: %s, Items: %s\n", queue[i].orderNumber,
queue[i].customerName, queue[i].foodItems);
if (i == rear) break;
i = (i + 1) % MAX;
}
}
int main() {
int choice;
Order order;
while (1) {
printf("\n1. Add Order\n2. Process Order\n3. View Orders\n4.
Exit\nChoose: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter Order Number: ");
scanf("%d", &order.orderNumber);
printf("Enter Customer Name: ");
scanf("%s", order.customerName);
printf("Enter Food Items: ");
scanf("%s", order.foodItems);
enqueue(order);
} else if (choice == 2) {
dequeue();
} else if (choice == 3) {
displayQueue();
} else {
break;
}
}
return 0;
}
ALGORITHM
Alice and Bob are playing a game with a binary string S of length N and an
empty string T. They both take turns and Alice plays first.
• In Alice's turn, she picks the first character of string S, appends the
character to either the front or back of string T and deletes the chosen
character from string S.
• In Bob's turn, he picks the last character of string S, appends the
character to either the front or back of string T and deletes the chosen
character from string �S.
The game stops when S becomes empty. Alice wants the resultant string T to
be lexicographically smallest, while Bob wants the resultant string T to be
lexicographically largest possible. Find the resultant string T, if both of them
play optimally
#include <stdio.h>
#include <string.h>
int main() {
char S[101], T[101] = ""; // S for input string, T for result
int N;
N = strlen(S);
int left = 0, right = N - 1; // Pointers for the start and end of S
int turn = 0; // 0 for Alice, 1 for Bob
return 0;
}
ALGORITHM
PROGRAM 15
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* head = NULL;
struct Node* temp = NULL;
int n, value;
return 0;
}
ALGORITHM
OUTPUT