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

조회 수: 1 (최근 30일)
ghyd ri
ghyd ri 2017년 11월 20일
답변: BhaTTa 2024년 10월 23일 4:40
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

답변 (1개)

BhaTTa
BhaTTa 2024년 10월 23일 4:40
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 CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by