asking about c++(array)--(need some help).how can i solve the problem?

1 visualizzazione (ultimi 30 giorni)
National Metrology department has stored 6 cities temperature in a 2 dimensional matrix as follows. Cities Row 0 ------- (Max temp) 32 33 30 18 19 25 Row 1 -------- (Min temp) 23 23 25 11 13 12 Row 2 -------- (Rainfall ) 50 45 27 11 38 33 Write a single C++ program to do the following 1. Store the above data in a 2 Dimensional array called temperature 2. Display the maximum temperature Display the lowest rainfall 3. Display the difference between maximum and minimum temperature for the last city

Risposte (1)

BhaTTa
BhaTTa il 23 Ott 2024
Hey @ghyd ri, I have used C++ stl for the implementation, refer to the code below:
#include <iostream>
#include <vector>
#include <algorithm> // For max_element and min_element
int main() {
// Step 1: Store the data in a 2D vector
std::vector<std::vector<int>> temperature = {
{32, 33, 30, 18, 19, 25}, // Max temperatures
{23, 23, 25, 11, 13, 12}, // Min temperatures
{50, 45, 27, 11, 38, 33} // Rainfall
};
// Step 2: Display the maximum temperature
int maxTemp = *std::max_element(temperature[0].begin(), temperature[0].end());
std::cout << "Maximum temperature: " << maxTemp << std::endl;
// Step 3: Display the lowest rainfall
int minRainfall = *std::min_element(temperature[2].begin(), temperature[2].end());
std::cout << "Lowest rainfall: " << minRainfall << std::endl;
// Step 4: Display the difference between max and min temperature for the last city
int lastCityIndex = temperature[0].size() - 1; // Index for the last city
int maxMinDifference = temperature[0][lastCityIndex] - temperature[1][lastCityIndex];
std::cout << "Difference between max and min temperature for the last city: "
<< maxMinDifference << std::endl;
return 0;
}
Hope it helps.

Categorie

Scopri di più su Data Types in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by