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

February 1st, 3rd, and 5th - Week #4


Monday


  1. HOMEWORK SUBMISSION IS READY for programming assignment #2.
    Login with your UNI CatID and passphrase.
    Choose CS 1160, C++, Jacobson and then click the Continue button.

    HOMEWORK SUBMIT system: C++ Homework SUBMISSION for C++ Assignments for Jacobson

      square.cpp
      pattern.cpp
    

  2. ASSIGNMENT HANDED OUT: Programming Assignment #2 - 22. Square Display and 23. Pattern Display - both on page 298.
    Two programs from chapter five of the textbook.

    1. The two C++ programs are turned in by using a user friendly web browser based submit system.
    2. You learned how to do that in a hands-on class during week #3.
    3. The programs will be due and can be submitted anytime before 11 AM on Monday, February 8th.
      square.cpp           Page 298 - 22. Square Display
      pattern.cpp          Page 298 - 23. Pattern Display
      

    4. Each of your two programs will have a comment block that indicates who wrote the program (your name) and what the name of the program file is.

      The program file names will be:

      square.cpp           Page 298 - 22. Square Display
      pattern.cpp          Page 298 - 23. Pattern Display
      
  3. Estimating the square root of a number using Newton's method. Class exercise of tracing the code was done.

    // Note the weblab.uni.edu syntax for 
    //                 #include <iostream>    differs from
    //                 #include <math.h>   for the .h suffix.
    
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main()
    {
       double x;
    
       cout << "Enter a value for x: ";
       cin >> x;
    
       cout << "\nSquare root of " << x 
            << " is " << sqrt(x) << endl;
    }
    


Wednesday


Programming Challenge of the Day - Feb 3rd, 2016 
   Page 294 of the textbook.

      2. Characters for the ASCII Codes

           Write a program that uses a loop to display the characters
                                    for the ASCII codes 0 through 127.  
           Display 16 characters on each line.
           Note: Oops, I changed it to display 8 characters per line in the SASP.
                                                  Solve A Simpler Problem = SASP

  1. Where do I find the information on the remainder or modulus operator for integers? day3.pdf shows 13 different operators, 7 arithmetic and 6 relational. Which one is the remainder operator?

  2. Pages 34 to 40 of tutorial.pdf cover Control Structures such as while loops, for loops, if else statements and switch statement.

  3. Here is a solution for the SASP, a SIMPLER PROBLEM is solved first. The simpler problem builds up the framework and experience for solving the more complicated, complete problem. VIP strategy to use, and very useful too.
    // SASP = Solve A Simpler Problem - Wednesday, Feb 3rd, 2016
    // Simpler Problem is:  Output the number 0, 1, 2, 3, ..., 126, 127 
    //                                               8 numbers per line.
    // Example of the output pattern:
    //                                 0 1 2 3 4 5 6 7 
    //                                 8 9 10 11 12 13 14 15
    //                                 16 17 18 19 20 21 22 23 
    //                                 24 25 26 27 28 29 30 31
    //                                 32 ...
    //                                 ...
    //                                 ...                         119
    //                                 120 121 122 123 124 125 126 127 
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int i;
    
       for (i = 0; i <= 127; i++)
       {
          if (i % 8 == 0)
              cout << endl;
    
          cout << i << " ";
       }
    }
    
    Note: Solve the SIMPLEST PROBLEM would be to just write the program
          to output the numbers 0, 1, 2, ... 126, 127 to the screen.
    
          That would be SASP applied to the above SASP.
    
    int main()
    {
       for (int i = 0; i <= 127; i++)
       {
          cout << i << endl;        // simplest problem of all, but 128 lines of output!  :-(
       }                           
    }
          
          Then solve the more complicated problem of getting 8 (or 16) numbers per line.
    

  4. See page 236 of the textbook (or last page of this PDF). Don't Forget the Braces with a Block of Statements.
       for (i = 0; i <= 127; i++)
       {                               
          if (i % 8 == 0)             // VIP!
              cout << endl;           // This works but it is VERY VERY VERY BAD.
              cout << i << " ";       // Indentation is very important for showing
       }                              //                 the logic of your program!!!
    

  5. Here is the solution to displaying the ASCII characters for the codes 0 to 127. Note that ASCII codes 0 to 32 are UNPRINTABLE and cannot be displayed! ASCII 32 is a space, as in spacebar produced character.
    #include <iostream>
    #include <iomanip>                // We will cover iomanip on Friday - see setw(3) below
    using namespace std;              //               -------  
    
    int main()
    {
       int asciiVal;
       char asciiChar;
    
       for (asciiVal = 0; asciiVal <= 127; asciiVal++)
       {
          if (asciiVal % 8 == 0)
          {
              cout << endl;
              cout << " ----- " << setw(3) << asciiVal 
                   << "  "; 
          }
    
          asciiChar = asciiVal;
          cout << asciiChar << " ";
       }
       
       cout << endl 
            << "------------------------------\n\n";
    }
    
  6. Output from the above code (messy because of special ASCII codes for LINE FEED and CARRIAGE RETURN that exist in the 0-31 unprintable codes.
     -----   0          
     -----   8       
     
      
       
     -----  16          
     -----  24        
     -----  32    ! " # $ % & ' 
     -----  40  ( ) * + , - . / 
     -----  48  0 1 2 3 4 5 6 7 
     -----  56  8 9 : ; < = > ? 
     -----  64  @ A B C D E F G 
     -----  72  H I J K L M N O 
     -----  80  P Q R S T U V W 
     -----  88  X Y Z [ \ ] ^ _ 
     -----  96  ` a b c d e f g 
     ----- 104  h i j k l m n o 
     ----- 112  p q r s t u v w 
     ----- 120  x y z { | } ~  
    ------------------------------
    
    
  7. ASCII Table with Decimal, Hexadecimal, Octal and Character.

    You only care about the Chr and the Dec - Decimal value is column 1 and Chr is column 4.
    As you can see, decimal values 0 to 31 have a DESCRIPTION in the Chr column, instead of a character like "0" or "9" or "A" or "a" or "$".

  8. Final Version: ASCII 32 through 127 - 16 per line C++ code and output.
       for (asciiVal = 32; asciiVal <= 127; asciiVal++)
       {
          if (asciiVal % 16 == 0)   // SWEET SIXTEEN instead of ELITE EIGHT
          {                            -------------            -----------
              cout << endl;
              cout << setw(10) << asciiVal
                   << " ---> ";
          }
          ...
    

Friday


Nested Loops and Programming Assignment #2 hints/suggestions, if needed.

ASSIGNMENT: Programming Assignment #2...

See textbook - 22. Square Display and 23. Pattern Display - both on page 298.
Two programs from chapter five of the textbook.
Due on Monday, February 8th by 11:00 am or before...