Certainly! I will break down each task into separate small C programs, ensuring clarity and ease of understanding. Each program will focus on a single task with comments to explain its functionality.
Task 1: Array Sorting with Different Methods
1a. Bubble Sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
void bubbleSort(int arr[], int size);
void printArray(int arr[], int size);
int main() {
int arr[SIZE];
// Fill array with random integers
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
arr[i] = rand() % 100; // Random numbers between 0 and 99
}
printf("Original array:\n");
printArray(arr, SIZE);
bubbleSort(arr, SIZE);
printf("Sorted array (Bubble Sort):\n");
printArray(arr, SIZE);
return 0;
}
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// Swap
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
1b. Selection Sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
void selectionSort(int arr[], int size);
void printArray(int arr[], int size);
int main() {
int arr[SIZE];
// Fill array with random integers
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
arr[i] = rand() % 100; // Random numbers between 0 and 99
}
printf("Original array:\n");
printArray(arr, SIZE);
selectionSort(arr, SIZE);
printf("Sorted array (Selection Sort):\n");
printArray(arr, SIZE);
return 0;
}
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
1c. Insertion Sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
void insertionSort(int arr[], int size);
void printArray(int arr[], int size);
int main() {
int arr[SIZE];
// Fill array with random integers
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
arr[i] = rand() % 100; // Random numbers between 0 and 99
}
printf("Original array:\n");
printArray(arr, SIZE);
insertionSort(arr, SIZE);
printf("Sorted array (Insertion Sort):\n");
printArray(arr, SIZE);
return 0;
}
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
1d. Counting Sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
#define MAX_VALUE 100
void countingSort(int arr[], int size);
void printArray(int arr[], int size);
int main() {
int arr[SIZE];
// Fill array with random integers
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
arr[i] = rand() % MAX_VALUE; // Random numbers between 0 and 99
}
printf("Original array:\n");
printArray(arr, SIZE);
countingSort(arr, SIZE);
printf("Sorted array (Counting Sort):\n");
printArray(arr, SIZE);
return 0;
}
void countingSort(int arr[], int size) {
int count[MAX_VALUE] = {0};
int output[SIZE];
for (int i = 0; i < size; i++) {
count[arr[i]]++;
}
for (int i = 1; i < MAX_VALUE; i++) {
count[i] += count[i - 1];
}
for (int i = size - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
for (int i = 0; i < size; i++) {
arr[i] = output[i];
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
Task 2: Find Height of 10th Tallest Student
#include <stdio.h>
#define SIZE 25
int main() {
// Heights of students in descending order
int heights[SIZE] = {185, 182, 180, 178, 177, 175, 174, 173, 171, 170,
169, 168, 167, 166, 165, 162, 160, 158, 155, 150,
148, 145, 143, 140, 138};
// Find the height of the student in 10th place from the top
int height10thTallest = heights[9];
printf("Height of the student ranked 10th from the tallest is: %d cm\n", height10thTallest);
return 0;
}
Task 3: Sixth Fastest Car
#include <stdio.h>
#define SIZE 15
void selectionSort(int arr[], int size);
void printArray(int arr[], int size);
int main() {
int speeds[SIZE] = {220, 180, 160, 200, 240, 150, 175, 245, 155, 210, 235, 195, 185, 230, 165};
selectionSort(speeds, SIZE);
printf("The sixth fastest car has a maximum speed of: %d km/h\n", speeds[SIZE - 6]);
return 0;
}
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
Task 4: Compare Third Top Student’s Average Grades
#include <stdio.h>
#define SIZE 18
int main() {
int classA[SIZE] = {5, 6, 4, 7, 3, 8, 2, 9, 10, 1, 11, 12, 13, 14, 15, 16, 17, 18}; // Average scores
int classB[SIZE] = {1, 3, 2, 5, 4, 6, 7, 8, 9, 10, 12, 11, 13, 15, 14, 16, 18, 17};
// Sort both arrays to find the top students
selectionSort(classA, SIZE);
selectionSort(classB, SIZE);
// Compare the third highest score
int thirdTopA = classA[SIZE - 3];
int thirdTopB = classB[SIZE - 3];
printf("Third top student average in Class A: %d\n", thirdTopA);
printf("Third top student average in Class B: %d\n", thirdTopB);
if (thirdTopA > thirdTopB) {
printf("Class A has the higher average for the third top student.\n");
} else {
printf("Class B has the higher average for the third top student.\n");
}
return 0;
}
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
Task 5: Compare Salaries of Employees in Two Firms
#include <stdio.h>
#define SIZE 15
void selectionSort(int arr[], int size);
int main() {
int salaries1[SIZE] = {40000, 60000, 80000, 20000, 50000, 70000, 90000, 30000, 100000, 150000, 120000, 110000, 130000, 140000, 160000};
int salaries2[SIZE] = {55000, 65000, 75000, 35000, 45000, 85000, 95000, 115000, 135000, 175000, 125000, 102000, 128000, 138000, 148000};
selectionSort(salaries1, SIZE);
selectionSort(salaries2, SIZE);
printf("Fifth highest salary in Firm 1: %d\n", salaries1[SIZE - 5]);
printf("Fifth highest salary in Firm 2: %d\n", salaries2[SIZE - 5]);
if (salaries1[SIZE - 5] > salaries2[SIZE - 5]) {
printf("Firm 1 has a higher fifth salary.\n");
} else {
printf("Firm 2 has a higher fifth salary.\n");
}
return 0;
}
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
Task 6: Find the Fifth Cheapest Television Model
#include <stdio.h>
#define SIZE 12
void selectionSort(int arr[], int size);
int main() {
int tvPrices[SIZE] = {7000, 3000, 12000, 8000, 5000, 2000, 11000, 6500, 10500, 9000, 15000, 10000};
selectionSort(tvPrices, SIZE);
printf("The fifth cheapest TV model costs: %d\n", tvPrices[4]); // Index 4 for the fifth cheapest
return 0;
}
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
Instructions for Running the Programs
- Copy each program into a separate C file (e.g.,
task1.c, task2.c, etc.).
- Compile the C programs using a C compiler like GCC:
gcc task1.c -o task1
- Run the compiled programs:
./task1
Each program addresses a specific task and uses sorted arrays or other straightforward logic to achieve the desired results. Each includes comments to make the code easier to follow.