0% found this document useful (0 votes)
6 views3 pages

Weather Data Analysis Program in C++

This document outlines a C++ program that collects weather data (temperature, humidity, rainfall) for a specified number of days (up to 50). It includes functions to set, print, and find the maximum values for each category, along with the corresponding day. The main function manages user input and displays the results after processing the data.

Uploaded by

hynpddysn5
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)
6 views3 pages

Weather Data Analysis Program in C++

This document outlines a C++ program that collects weather data (temperature, humidity, rainfall) for a specified number of days (up to 50). It includes functions to set, print, and find the maximum values for each category, along with the corresponding day. The main function manages user input and displays the results after processing the data.

Uploaded by

hynpddysn5
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

Lecture 20

Md. Minhaz Akram


EEE 1109

Write a program that

 collects weather data (temperature, humidity, rainfall) for a set number of days (up to 50).
 It finds and prints the maximum values for each category (temperature, humidity, and rainfall) and
outputs which day those maximums occurred on.

#include<iostream>

using namespace std;

void set(float a[],int n)


{
for(int i=0;i<n;i++)
{
cout << "Enter "<< i+1 << " th member:";
cin >> a[i];
}
}

void print(float a[],int n)


{
for(int i=0;i<n;i++)
{
cout << a[i]<< " , ";
}
cout << endl;
}

float max(float a[],int n)


{
float m=a[0]; int d=0;
for(int i=1;i<n;i++)
{
if(a[i]>m)
{
m=a[i];
d= i;
}
}
return d+1;
}

int main()
{
int n;
float rain[50]={0},humid[50]={0},temp[50]={0};

do
{
cout << "Enter the number of days[max 50]: ";
cin >> n;
}while(n<1 || n>50);

cout << "Enter Temparature for "<< n << " days: ";
set(temp,n);
cout << "Enter Humidity for "<< n << " days: ";
set(humid,n);
cout << "Enter Rainfall for "<< n << " days: ";
set(rain,n);

cout << "Show Temparature for "<< n << " days: ";
print(temp,n);
cout << "Show Humidity for "<< n << " days: ";
print(humid,n);
cout << "Show Rainfall for "<< n << " days: ";
print(rain,n);

cout << "The maximum temparature occured in day:" << max(temp,n) << endl;
cout << "The maximum humidity occured in day:" << max(humid,n) << endl;
cout << "The maximum rainfall occured in day:" << max(rain,n) <<endl;

Enter the number of days[max 50]: 4


Enter Temparature for 4 days: Enter 1 th member:21
Enter 2 th member:23
Enter 3 th member:34
Enter 4 th member:12
Enter Humidity for 4 days: Enter 1 th member:.9
Enter 2 th member:.6
Enter 3 th member:.5
Enter 4 th member:.97
Enter Rainfall for 4 days: Enter 1 th member:10
Enter 2 th member:11.3
Enter 3 th member:12.5
Enter 4 th member:9.7
Show Temparature for 4 days: 21 , 23 , 34 , 12 ,
Show Humidity for 4 days: 0.9 , 0.6 , 0.5 , 0.97 ,
Show Rainfall for 4 days: 10 , 11.3 , 12.5 , 9.7 ,
The maximum temparature occured in:3 day.
The maximum humidity occured in:4 day.
The maximum rainfall occured in:3 day.

--------------------------------
Process exited after 50.85 seconds with return value 0
Press any key to continue . . .

This C++ program handles the input and processing of temperature, humidity, and rainfall data for a specified
number of days. Let's break down each part of the program:

1. Function Definitions:

 set(float a[], int n): This function takes an array a[] and the size n. It prompts the user to enter
values for n elements, which are stored in the array a[].
 print(float a[], int n): This function prints all the elements in the array a[] up to the n-th
element, separated by commas.
 max(float a[], int n): This function calculates the day on which the maximum value occurred in
the array a[]. It iterates through the array and keeps track of the largest value and its index. The
function returns the day number (1-based index) of the maximum value.
2. Main Function:

 The program starts by asking the user for the number of days (n), with a validation to ensure that n is
between 1 and 50.
 Arrays rain[], humid[], and temp[] are initialized with a size of 50 (maximum days).
 The program then prompts the user to input temperature, humidity, and rainfall values for the n days
using the set() function.
 After collecting the data, the print() function is used to display the input values for each category
(temperature, humidity, rainfall).
 Finally, the program calls the max() function for each array (temperature, humidity, and rainfall) and
outputs the day on which the maximum value occurred.

Common questions

Powered by AI

The program identifies the day with the maximum values by calling the max() function for each data category array (temperature, humidity, rainfall). The max() function iterates through each array, determines the maximum value, and tracks the index where this value occurs. Finally, it returns the day by converting the zero-based index to a one-based day count by adding one to the index .

The program could be inefficient due to redundant loops when processing each array separately and always iterating over all 50 elements. It could be improved by dynamically allocating arrays based on the actual number of days. Additionally, consolidating functions to handle multiple arrays concurrently or using data structures like structs to maintain related data could reduce redundancy and enhance efficiency .

The main() function limits the processing to a maximum of 50 days by initializing arrays `rain[]`, `humid[]`, and `temp[]` with a fixed size of 50. It further constrains the number of days through an input validation loop, ensuring the entered number is between 1 and 50 before proceeding with data input and processing .

To extend the program for calculating averages, additional functions or modifications to existing ones could sum the elements in each array and divide by `n` (the number of days). This would involve iterating over each data category's array, updating the computation to include division by `n`, and outputting the results alongside maximum values .

The print() function is responsible for outputting the contents of an array to the console. It takes an array and its size as parameters, iterates through each element, and prints them separated by commas. This function is used to display the data inputted by the user for temperature, humidity, and rainfall .

The max() function iterates through the array while keeping track of the maximum value found and its index. It initializes a variable `m` with the first element and `d` with zero (representing the index). For each element in the array, if a larger value is encountered, both the value and its index `d` are updated. Finally, it returns the day number by adding 1 to the index `d` to convert the zero-based index to a one-based day count .

The program uses a do-while loop to repeatedly prompt the user for the number of days until a valid input is entered. This ensures that the number of days is between 1 and 50 inclusive, as the condition `(n<1 || n>50)` has to be false for the loop to exit .

The program, in its current state, cannot process more than 50 days of data due to fixed array sizes and input validation limiting days to a maximum of 50. Handling more than 50 days would require modifying the array sizes and adjusting the input validation logic to accommodate a higher number of entries .

The program uses three separate arrays, `temp[]`, `humid[]`, and `rain[]`, to store data for temperature, humidity, and rainfall respectively. Each array has a size of 50 (the maximum possible number of days), allowing for efficient separate storage and retrieval of each category's data using the functions defined in the program .

The set() function is designed to populate an array with user-input data. It takes an array and its size as parameters, then iteratively prompts the user to enter values for each element in the array, storing the inputs accordingly. This approach efficiently gathers and stores the weather data such as temperature, humidity, and rainfall .

You might also like