0% found this document useful (0 votes)
3 views4 pages

Sujal2 1

The document outlines a worksheet for a computer science student focusing on an IoT device that collects and sorts temperature readings. It includes a C++ code implementation using the merge sort algorithm to sort temperature data and trigger alerts based on specified thresholds. The worksheet specifies the student's details, the subject, and the requirements for the assignment.
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)
3 views4 pages

Sujal2 1

The document outlines a worksheet for a computer science student focusing on an IoT device that collects and sorts temperature readings. It includes a C++ code implementation using the merge sort algorithm to sort temperature data and trigger alerts based on specified thresholds. The worksheet specifies the student's details, the subject, and the requirements for the assignment.
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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Worksheet2

Student Name:Sujal UID: 23BCS11856


Branch: BE. CSE Section/Group: 706/ B
Semester: 3RD Date of Performance:06/09/24
Subject Name: Advance Data Structure
and Algorithms Subject Code: 23CSH-204

Aim: An IoT device collects temperature reading from varioussensors in real time these reading
needs to be sorted efficiently to monitor temperature changes, trigger alerts when necessary.
choose an appropriate sorting technique for scenario.

Requirements (Hardware/Software):
Online code compileror Visual Studio

Code/ Output:Code:
#include <iostream>
using namespace std;

void triggerAlert(int temp) {


const int maxTemperature = 45;
const int minTemperature = 15;

if (temp > maxTemperature) {


cout << "Alert: High temperature (" << temp << "°C) exceeded the safe temperature!" <<
endl;
} else if (temp < minTemperature) {
cout << "Alert: Low temperature (" << temp << "°C) below the safe temperature!" << endl;
}
}

void merge(int readings[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
int leftArray[n1], rightArray[n2];

for (int i = 0; i < n1; i++)


leftArray[i] = readings[left + i];
for (int i = 0; i < n2; i++)
rightArray[i] = readings[mid + 1 + i];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
readings[k] = leftArray[i];
i++;
} else {
readings[k] = rightArray[j];
j++;
}
k++;
}

while (i < n1) {


readings[k] = leftArray[i];
i++;
k++;
}

while (j < n2) {


readings[k] = rightArray[j];
j++;
k++;
}
}

void mergeSort(int readings[], int left, int right) {


if (left >= right) return;

int mid = left + (right - left) / 2;


mergeSort(readings, left, mid);
mergeSort(readings, mid + 1, right);
merge(readings, left, mid, right);
}

int main() {
int size;
cout << "Enter the number of temperature readings: ";
cin >> size;

int temperatureReadings[size];
cout << "Enter the temperature readings: ";

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


cin >> temperatureReadings[i];
triggerAlert(temperatureReadings[i]);
}

cout << "Temperature readings before sorting: ";


for (int i = 0; i < size; i++)
cout << temperatureReadings[i] << " ";
cout << endl;

mergeSort(temperatureReadings, 0, size - 1);

cout << "Temperature readings after sorting: ";


for (int i = 0; i < size; i++)
cout << temperatureReadings[i] << " ";
cout << endl;

return 0;
}
Output:

You might also like