Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program called avg proc.cpp that calculates the average temperature in your living room over a period of time based on the data

Write a C++ program called avg proc.cpp that calculates the average temperature

in your living room over a period of time based on the data from two IoT sensors.

Your program should rst request the name of a temperature text le. A text le

called temp data.txt is provided that contains a number of sensor messages sent, with

each line representing a distinct message. Each message contains a sensor name and

a temperature value which are separated by commas. The program should read the

temperature data into a suitable data structure. The temperatures for sensor 1 should

then be added to a global sum total by the parent process, while a child process should

be spawned to add the temperatures for sensor 2 to the same global sum total. After

all the temperature samples have been processed, the average temperature should be

calculated and printed by dividing the sum total by the number of samples. Assume

that the specied text le is located in the same directory as your program. The average

temperature value should be formatted to two decimal places.

IMPLEMENTATION:

1. Your program should rst request the name of a temperature text le.

2. It then reads the input le line by line and stores the temperatures in a suitable

global data structure.

3. When the reading of the input le is complete, the program creates a new child

process with the fork() function.

4. The parent process then sums up the temperature data from sensor 1 in a global

variable.

5. At the same time, the child process sums up the temperature data from sensor 2

in the same global variable.

6. When the processing of the temperature data is complete, the program calculates

and prints the average temperature by dividing the global sum variable by the

total number of samples in the input file.

temp_data.txt

LivingTemp1,17.8 LivingTemp2,17.9 LivingTemp1,18.1 LivingTemp2,18.2 LivingTemp1,18.5 LivingTemp2,18.6 LivingTemp1,19.0 LivingTemp2,19.0 LivingTemp1,19.5 LivingTemp2,19.6 LivingTemp1,20.0 LivingTemp2,20.1 LivingTemp1,20.6 LivingTemp2,20.6 LivingTemp1,19.8 LivingTemp2,19.8 LivingTemp1,19.4 LivingTemp2,19.5 LivingTemp1,19.0 LivingTemp2,19.1 LivingTemp1,18.5 LivingTemp2,18.6 LivingTemp1,18.0 LivingTemp2,18.1

Example output:

user~$ make run

./avg_proc

Enter file name: temp_data.txt

Average Temperature: 9.29

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

Give your managers opportunities for individual development.

Answered: 1 week ago