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

7 ビュー (過去 30 日間)
ghyd ri
ghyd ri 2017 年 11 月 20 日
回答済み: BhaTTa 2024 年 10 月 23 日
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 件のコメント
M
M 2017 年 11 月 20 日
ghyd ri
ghyd ri 2017 年 11 月 20 日
i didn't know how to do this activity!

サインインしてコメントする。

回答 (1 件)

BhaTTa
BhaTTa 2024 年 10 月 23 日
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.

カテゴリ

Help Center および File ExchangeAudio Plugin Creation and Hosting についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by