The program in C Display calendar using loop user defined fu
The program in C++
(Display calendar using loop, user defined functions, top-down design) Write a program that prompts the user to enter the year and the first day of the year and output the calendar table for the year to a text file. You should follow the steps described below. a) Develop an algorithm in pseudo language that specifies the main steps at the top level. b) Apply the top-down design principle to refine non-trivial steps in the main algorithm into separate algorithms for sub-steps. c) If there are still non-trivial steps in the sub-steps, do further refinement to them as necessary. d) Implement your algorithm in a C++ program. The top-level algorithm will be implemented as the “main( )” function, and other algorithms as user defined functions. e) Answers to a), b) and c) will be put in the program as heading comments for the corresponding functions.
Show the output.
Solution
#include <iostream> // Needed to use IO functions
using namespace std;
int main() {
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int upperbound; // Sum from 1 to this upperbound
int absDiff; // The absolute difference between the two sums
// Prompt user for an upperbound
cout << \"Enter the upperbound: \";
cin >> upperbound;
// Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound
int number = 1;
while (number <= upperbound) {
if (number % 2 == 0) { // Even number
sumEven += number; // Add number into sumEven
} else { // Odd number
sumOdd += number; // Add number into sumOdd
}
++number; // increment number by 1
}
// Compute the absolute difference between the two sums
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// Print the results
cout << \"The sum of odd numbers is \" << sumOdd << endl;
cout << \"The sum of even numbers is \" << sumEven << endl;
cout << \"The absolute difference is \" << absDiff << endl;
return 0;
}

