Spring 2025 CS 31

Homework 2 Solution

  1. endlendl
    grendel
    
  2. It prints a diagonal line of the specified length. [The problem said to write a brief, simple English sentence; we did not want a paragraph-length blow-by-blow account of what the program was doing.]

  3. #include <iostream>
    using namespace std;
    
    int main()
    {
        int len;
    
        cout << "Enter a number: ";
        cin >> len;
    
        for (int i = 0; i < len; i++)
        {
    	int j = i+1;
    	while (j < len)
    	{
    	    cout << ' ';
    	    j++;
    	}
    	cout << "#" << endl;
        }
    }
    
  4. switch (month)
    {
      case 6:
        cout << "summer solstice";
        break;
    
      case 12:
        cout << "winter solstice";
        break;
    
      case 3:
      case 9:
        cout << "equinox";
        break;
    
      default:
        cout << "nothing special";
    }