Homework 1

Due Wednesday, February 5, at the start of class. Compute the sum of the number of times the statements in the following code fragments are executed, expressing your answer as indicated (show your work). Count your answer parts carefully--There are 15 parts to the assignment--for full credit.
  1. Four answers: four integers for initial values of n = 1, n = 2, n = 4, n = 100
          int i = 0;
    s1    while (i < n)
    s2    {  for (int j = 0; j < n; j++)
    s3          cout << "Hello world";
    s4       i = i + 1;
          }
    
  2. One answer: an integer
    s1    for (int i = 1; i <= 6; i++)
    s2      for (int j = 1; j <= 10; j++)
    s3        cout << i * j << endl;
    s4    for (int k = 0; k < 1000; k++)
    s5      cout << "Hello" << end;
    
  3. One answer: in terms of n
    s1    cin >> n;
    s2    for (int i = 1; i <= n + 6; i++)
    s3      for (int j = 1; j <= n + 10; j++)
    s4        cout << i * j << endl;
    s5    for (int k = 1; k <= n + 1000; k++)
    s6      cout << "Hello" << end;
    
  4. Five answers: five integers for initial values of n = 1, n = 4, n = 6, n = 7, n = 8
    s1    while (n > 1)
    s2    {  cout <<  n << endl;
    s3       n = n / 2;
          }
    
  5. One answer: in terms of n
    s1    cin >> n;
    s2    for (i = 1; i <= n; i++)
    s3    {  temp = n;
    s4       while (temp > 0)
    s5       {  cout << n * temp << endl;
    s6          temp = temp / 2;
             }
          }
    
  6. Three answers: three integers based on x = {3, 6, 9, 4, 18, 10, 12, 1, 0, 16} and a) q = 3, b) q = 2, c) q = 10
    s1    for ( i = 0; i < 10; i++)
    s2    {  if (q == x[i])
    s3       {  cout << "q is in array at position " << i;
    s4          exit(0);
             }
          }
    s5    cout << "q is not in array"