CS 1160 C/C++ Programming 12 MWF - Spring 2016

February 15th, 17th, and 19th - Week #6


Monday 02/15/2016


  1. Test One announced. Friday, February 26th, 11 days from today.

  2. Created a function to do staggered output of I love C++ message by creating a void spaces function that inserts into the output stream n spaces.
    void spaces(int n)
    {
       for (int sp = 1; sp <= n; sp++)
          cout << " ";
    }
    
    void loveCpp( int n )
    {
       int i;
    
       for (i = 1; i <= n; i++)
       {
          spaces( i );
    
          cout << "I love C++.  It is fun." 
               << endl;
       }
    }
    
    Output when loveCpp is invoked (called) with -->  loveCpp( 10 );
     
     I love C++.  It is fun.
      I love C++.  It is fun.
       I love C++.  It is fun.
        I love C++.  It is fun.
         I love C++.  It is fun.
          I love C++.  It is fun.
           I love C++.  It is fun.
            I love C++.  It is fun.
             I love C++.  It is fun.
              I love C++.  It is fun.
    
  3. Wednesday class: We will develop a program to print out and express numbers using only prime numbers. If it is PRIME, its okay. If not, express it as a product of primes.

  4. Here are all the prime numbers between 2 and 1000:
       2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53   59   61 
      67   71   73   79   83   89   97  101  103  107  109  113  127  131  137  139  149  151 
     157  163  167  173  179  181  191  193  197  199  211  223  227  229  233  239  241  251 
     257  263  269  271  277  281  283  293  307  311  313  317  331  337  347  349  353  359 
     367  373  379  383  389  397  401  409  419  421  431  433  439  443  449  457  461  463 
     467  479  487  491  499  503  509  521  523  541  547  557  563  569  571  577  587  593 
     599  601  607  613  617  619  631  641  643  647  653  659  661  673  677  683  691  701 
     709  719  727  733  739  743  751  757  761  769  773  787  797  809  811  821  823  827 
     829  839  853  857  859  863  877  881  883  887  907  911  919  929  937  941  947  953 
     967  971  977  983  991  997 
    
    There are 168 primes between 2 and 1000
    
    Try out the PERL Prime number program yourself: www.cs.uni.edu/~jacobson/cgi-bin/primes.cgi ...

  5. How do you produce this output?
    [jacobson@weblab ~]$ g++ 212.cpp
    [jacobson@weblab ~]$ ./a.out
    
    -------------------------
    
     I love C++.  1
      I love C++.  2
       I love C++.  3
        I love C++.  4
         I love C++.  5
          I love C++.  6
           I love C++.  7
            I love C++.  8
           I love C++.  7
          I love C++.  6
         I love C++.  5
        I love C++.  4
       I love C++.  3
      I love C++.  2
     I love C++.  1
    
    -------------------------
    
     I love C++.  1
      I love C++.  2
       I love C++.  3
       I love C++.  3
      I love C++.  2
     I love C++.  1
    
    -------------------------
    
  6. Here is the modified code used to get the arrow shaped output created with --> loveCpp(15); and with loveCpp(6);
    // Monday, February 15th - modification to Friday 02/12 212.cpp example.
    #include 
    #include 
    using namespace std;
    
    void spaces(int n)
    {
       for (int sp = 1; sp <= n; sp++)     
          cout << " "; 
    }                  
    
    void loveCpp( int n )
    {
       int i, middle; 
    
       middle = (n + 1) / 2;
       for (i = 1; i <= middle; i++)
       {
          spaces( i );
          cout << "I love C++." << setw(3) << i << endl;
       }
    
       middle = n / 2;
       for (i = middle; i >= 1; i--)
       {
          spaces( i );
          cout << "I love C++." << setw(3) << i << endl;
       }
    }
    
    int main()
    {
       cout << "\n-------------------------\n\n";
       loveCpp(15);
    
       cout << "\n-------------------------\n\n";
       loveCpp(6);
    
       cout << "\n-------------------------\n\n";
    }
    


Wednesday 2/17/2016


  1. What are the Prime numbers? Write a C++ program to print out the every integer from 2 to 103 expressed completely with Prime numbers. If it is not itself Prime, it can be expressed as a product of prime numbers, its prime factors.
     2
     3
     2 2
     5
     2 3
     7
     2 2 2
     3 3                This is a sample of what the output might look
     2 5                like.  We will create a void function that
     11                 prints out the prime factors of the integer,
     2 2 3              or prints the integer if it had no prime
     13                 factors (and thus is itself prime).
     2 7
     3 5
     2 2 2 2 
     17
     2 3 3
     19
     ...
     2 2 2 2 2 3
     97
     3 3 11
     2 2 5 5
     101
     2 3 17
     103
    
  2. The PRIME NUMBER FACTORS and PRIMES programs from the ITTC 134 hands-on computer lab class.

  3. What is wrong with the following code from today's class? If you can't figure it out, go back and look at the previous LI List Item link examples. It is correct (and explained) there.
    bool primeFactors( int number )
    {
       int n, div;
       div = 2;
       n = number;
    
       while (n > 1)
       {
          while ( n % div == 0 )
          {
             cout << div << " ";
             n = n / div;
          }
    
          div = div + 1;
       }
    
       return div == number;
    }
    


Friday 2/19/2016


  1. TEST ONE: On leap year day - Monday, February 29th.

  2. swap.cpp - passing parameters by reference. Input/Output parameters. Swapping two variables values.

  3. Output parameters - pass by reference used to pass back results through parameters.

  4. Introduction to ARRAYS in C++.