IN C++
5.14 PRACTICE: Loops* - Biggest difference Write a program that outputs the biggest difference (absolute value) between any successive pair of numbers in a list. Such a list might represent daily stock market prices or daily temperatures, so the difference represents the biggest single-day change. The input is the list size, followed by the numbers. If the input is 5 60 63 68 61 59, the output is 7. Hints: • Declare a variable for the current number, and another for the previous number. At the start of each loop iteration, set prevNum = currNum, then get the next number into currnum. • Maintain a max difference variable. Initialize it with O. In each loop iteration, check if the difference between currNum and prevNum exceeds maxDiff; if so, update maxDiff with that difference. • Don't forget to take the absolute value of the difference before the above comparison. • Don't try to check the max difference for the first number in the list, since no previous number exists. 390600.2579638.qx3zqy7 LAB ACTIVITY 5.14.1: PRACTICE: Loops* - Biggest difference 0/10 main.cpp Load default template. 1 #include <iostream> 2 using namespace std; 4 int main() { 5 6 int prevNum; 7 int currNum; 8 int maxDiff = 0; 9 cin >> prevNum; 10 11 for(int i = 2; i <= currNum ; i++) { 12 13 if (maxDiff <abs(currum - prevNum)) 14 maxDiff = abs(currNum - prevNum); 15 16 prevNum = currNum; 17 cin >> currNum; 18 } 19 20 cout << maxDiff << endl; 21 22 return; 23 }
IN C++
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am