0% found this document useful (0 votes)
17 views

QC101: Calculate A Bike's Average Consumption: Sure, I'll Provide The Solutions For Each of The Tasks Mentioned

The solutions provide code snippets for various tasks including calculating average bike consumption and electricity bill, implementing a simple calculator using functions, printing patterns using functions, finding permutations of an array, manipulating matrices, finding the merge point of linked lists, inserting nodes in doubly linked lists, sorting an array of numbers, and implementing a library management system.

Uploaded by

Sujithra sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

QC101: Calculate A Bike's Average Consumption: Sure, I'll Provide The Solutions For Each of The Tasks Mentioned

The solutions provide code snippets for various tasks including calculating average bike consumption and electricity bill, implementing a simple calculator using functions, printing patterns using functions, finding permutations of an array, manipulating matrices, finding the merge point of linked lists, inserting nodes in doubly linked lists, sorting an array of numbers, and implementing a library management system.

Uploaded by

Sujithra sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sure, I'll provide the solutions for each of the tasks mentioned.

QC101: Calculate a bike’s average consumption

c Copy code

#include <stdio.h> int main() { int distance; float fuel; printf("Enter total distance
traveled (in km): "); scanf("%d", &distance); printf("Enter spent fuel (in liters): ");
scanf("%f", &fuel); float averageConsumption = distance / fuel; printf("Average
consumption (km/lt): %.2f\n", averageConsumption); return 0; }

QC102: Calculate and print the electricity bill

c Copy code

#include <stdio.h> int main() { int units; float bill; printf("Enter the number of
units consumed: "); scanf("%d", &units); if (units <= 199) { bill = units * 1.20; } else
if (units >= 200 && units <= 399) { bill = units * 1.50; } else if (units >= 400 &&
units <= 599) { bill = units * 1.80; } else { bill = units * 2.00; } printf("Electricity
Bill: %.2f Rs\n", bill); return 0; }

QC201: Simple Calculator using functions

c Copy code

#include <stdio.h> void add(int num1, int num2) { printf("Result: %d\n", num1 + num2);
} void sub(int num1, int num2) { printf("Result: %d\n", num1 - num2); } void mul(int
num1, int num2) { printf("Result: %d\n", num1 * num2); } void div(int num1, int num2) {
if (num2 != 0) { printf("Result: %.2f\n", (float)num1 / num2); } else { printf("Division
by zero is not allowed.\n"); } } int main() { int num1, num2; char op; printf("Enter two
numbers: "); scanf("%d %d", &num1, &num2); printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op); switch (op) { case '+': add(num1, num2); break; case '-': sub(num1,
num2); break; case '*': mul(num1, num2); break; case '/': div(num1, num2); break;
default: printf("Invalid operator.\n"); break; } return 0; }

QC202: Print the given pattern using function

c Copy code

#include <stdio.h> void printPattern() { for (int i = 0; i < 7; i++) { for (int j = 0;
j < 7; j++) { if (i == 0 || i == 6 || j == 0 || j == 6) { printf("*"); } else { printf("
"); } } printf("\n"); } } int main() { printPattern(); return 0; }
QC301: Permutations of an array of strings in lexicographical order

c Copy code

#include <stdio.h> #include <string.h> #include <stdlib.h> int cmpfunc(const void *a,
const void *b) { return strcmp(*(const char **)a, *(const char **)b); } void swap(char
**a, char **b) { char *temp = *a; *a = *b; *b = temp; } void permute(char *arr[], int l,
int r) { if (l == r) { for (int i = 0; i <= r; i++) { printf("%s ", arr[i]); }
printf("\n"); } else { for (int i = l; i <= r; i++) { swap((arr + l), (arr + i));
permute(arr, l + 1, r); swap((arr + l), (arr + i)); // backtrack } } } int main() { char
*arr[] = {"wx", "xy", "yz"}; int n = sizeof(arr) / sizeof(arr[0]); qsort(arr, n,
sizeof(char *), cmpfunc); printf("Permutations:\n"); permute(arr, 0, n - 1); return 0;
}

QC302: Read and print reverse, rank, and transpose of the matrix

c Copy code

#include <stdio.h> void printMatrix(int mat[][100], int n) { for (int i = 0; i < n;


i++) { for (int j = 0; j < n; j++) { printf("%d ", mat[i][j]); } printf("\n"); } } void
transposeMatrix(int mat[][100], int n) { for (int i = 0; i < n; i++) { for (int j = i +
1; j < n; j++) { int temp = mat[i][j]; mat[i][j] = mat[j][i]; mat[j][i] = temp; } } }
void reverseMatrix(int mat[][100], int n) { for (int i = 0; i < n; i++) { for (int j =
0; j < n / 2; j++) { int temp = mat[i][j]; mat[i][j] = mat[i][n - j - 1]; mat[i][n - j -
1] = temp; } } } int rankMatrix(int mat[][100], int n) { int rank = n; for (int row = 0;
row < rank; row++) { if (mat[row][row]) { for (int col = 0; col < n; col++) { if (col !=
row) { double mult = (double)mat[col][row] / mat[row][row]; for (int i = 0; i < rank;
i++) { mat[col][i] -= mult * mat[row][i]; } } } } else { int reduce = 1; for (int i =
row + 1; i < n; i++) { if (mat[i][row]) { for (int j = 0; j < rank; j++) { int temp =
mat[row][j]; mat[row][j] = mat[i][j]; mat[i][j] = temp; } reduce = 0; break; } } if
(reduce) { rank--; for (int i = 0; i < n; i++) { mat[i][row] = mat[i][rank]; } } row--;
} } return rank; } int main() { int n; printf("Enter the size of the matrix: ");
scanf("%d", &n); int mat[100][100]; printf("Enter the elements of the matrix:\n"); for
(int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &mat[i][j]); } }
printf("Original Matrix:\n"); printMatrix(mat, n); transposeMatrix(mat, n);
printf("Transposed Matrix:\n"); printMatrix(mat, n); reverseMatrix(mat, n);
printf("Reversed Matrix:\n"); printMatrix(mat, n); int rank = rankMatrix(mat, n);
printf("Rank of the Matrix: %d\n", rank); return 0; }

QC401: Find the node where two linked lists merge

c Copy code

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; };
struct Node* findMergeNode(struct Node *headA, struct Node *headB) { struct Node
*currentA = headA; struct Node *currentB = headB; while (currentA != currentB) {
currentA = (currentA == NULL) ? headB : currentA->next; currentB = (currentB == NULL) ?
headA : currentB->next; } return currentA; } struct Node* newNode(int data) { struct
Node *node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next =
NULL; return node; } int main() { struct Node *newNode; struct Node *head1 =
newNode(10); head1->next = newNode(20); head1->next->next = newNode(30); head1->next-
>next->next = newNode(40); head1->next->next->next->next = newNode(50); struct Node
*head2 = newNode(15); head2->next = head1->next->next; struct Node *mergeNode =
findMergeNode(head1, head2); if (mergeNode != NULL) { printf("The merge point is %d\n",
mergeNode->data); } else { printf("No merge point found\n"); } return 0; }

QC402: Insert a new node at the head and tail of a doubly linked list

c Copy code

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next;
struct Node *prev; }; struct Node* createNode(int data) { struct Node *newNode = (struct
Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = newNode->prev =
NULL; return newNode; } void insertAtHead(struct Node **head, int data) { struct Node
*newNode = createNode(data); if (*head == NULL) { *head = newNode; return; } newNode-
>next = *head; (*head)->prev = newNode; *head = newNode; } void insertAtTail(struct Node
**head, int data) { struct Node *newNode = createNode(data); if (*head == NULL) { *head
= newNode; return; } struct Node *temp = *head; while (temp->next != NULL) { temp =
temp->next; } temp->next = newNode; newNode->prev = temp; } void printList(struct Node
*node) { while (node != NULL) { printf("%d -> ", node->data); node = node->next; }
printf("NULL\n"); } int main() { struct Node *head = NULL; insertAtHead(&head, 10);
insertAtHead(&head, 20); insertAtTail(&head, 30); insertAtTail(&head, 40);
printf("Doubly Linked List:\n"); printList(head); return 0; }

QC501: Sort an array of 0s, 1s, and 2s

c Copy code

#include <stdio.h> void sortArray(int arr[], int n) { int count0 = 0, count1 = 0,


count2 = 0; for (int i = 0; i < n; i++) { switch (arr[i]) { case 0: count0++; break;
case 1: count1++; break; case 2: count2++; break; } } int i = 0; while (count0 > 0) {
arr[i++] = 0; count0--; } while (count1 > 0) { arr[i++] = 1; count1--; } while (count2 >
0) { arr[i++] = 2; count2--; } } int main() { int arr[] = {0, 1, 2, 0, 1, 2}; int n =
sizeof(arr) / sizeof(arr[0]); sortArray(arr, n); printf("Sorted Array: "); for (int i =
0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }

QC502: Library management system for book queries

c Copy code

#include <stdio.h> #include <stdlib.h> #define MAX_SHELVES 1000 #define MAX_BOOKS 1000
int books[MAX_SHELVES][MAX_BOOKS]; int bookCount[MAX_SHELVES] = {0}; void insertBook(int
shelf, int pages) { books[shelf][bookCount[shelf]++] = pages; } void printBookPages(int
shelf, int book) { if (book < bookCount[shelf]) { printf("%d\n", books[shelf][book]); }
else { printf("Invalid book number\n"); } } void printBookCount(int shelf) {
printf("%d\n", bookCount[shelf]); } int main() { int queries, type, x, y; printf("Enter
number of queries: "); scanf("%d", &queries); while (queries--) { printf("Enter query
type: "); scanf("%d", &type); if (type == 1) { printf("Enter shelf number and pages: ");
scanf("%d %d", &x, &y); insertBook(x, y); } else if (type == 2) { printf("Enter shelf
number and book number: "); scanf("%d %d", &x, &y); printBookPages(x, y); } else if
(type == 3) { printf("Enter shelf number: "); scanf("%d", &x); printBookCount(x); } else
{ printf("Invalid query type\n"); } } return 0; }

You might also like