asking about c++(array)--(need some help).how can i solve the problem?
1 次查看(过去 30 天)
显示 更早的评论
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
2 个评论
回答(1 个)
BhaTTa
2024-10-23,4:40
#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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!