CSCE 2004 - Laboratory Assignment 2

    The objective of this laboratory assignment is to give students exposure to input/output operations, data types and arithmetic operations in C++. This assignment has the following steps:

  1. Login to Turing via the Terminal
  2. Use your UARK user name and UARK password to login to one of the iMac machines in JBHT 235. Then start up the "Terminal" program on the iMac, and login to the department's Linux server by typing "ssh turing.csce.uark.edu" in the Terminal window. You will be prompted for your UARK password again.

  3. Create a Subdirectory for this Lab
  4. This semester you will be creating a lot of programs and output files for this class. To keep everything organized, it is a good idea to create a separate subdirectory for each lab and homework assignment.

    To create a subdirectory for lab2 type:
    mkdir 2004/labs/lab2

    To move into this subdirectory type:
    cd 2004/labs/lab2

  5. Create Simple Program
  6. The program below demonstrates how C++ variables are declared, and illustrates a number of arithmetic operations, and some simple input / output commands.

    #include <iostream>
    using namespace std;
    
    int main ()
    {
      // Variable declarations 
      const int x = 100;
      int y = 0;
      int sum = 0;
      int difference = 0;
      int quotient = 0;
      int remainder = 0;
      int product = 0;
    
      // Get user input
      cout << "Enter an integer value for y: " << endl;
      cin >> y;
    
      // Perform arithmetic operations
      sum = x + y;
      difference = x - y;
      quotient = x / y;	
      remainder = x % y;
      product = x * y;	
    
      // Print results 
      cout << "The sum (x + y) is:  " << sum << endl;
      cout << "The difference (x - y) is: " << difference << endl;
      cout << "The quotient (x / y) is: " << quotient << endl;
      cout << "The remainder (x % y) is: " << remainder << endl;
      cout << "The product (x * y) is: " << product << endl;
      return 0;
    }
    

    Type in "nano -c prog1.cpp" in the Terminal window. Cut and paste the program above into the nano window, and save (^O) and exit (^X) from nano. Now compile your program by typing "g++ prog1.cpp -o prog1.exe". Hopefully you will not get any error messages.

  7. Test the Program
  8. To run the program you just compiled type in "./prog1.exe". You should be prompted to enter an integer. Type in the number "3" and you should see the following:

    The sum (x + y) is:  103
    The difference (x - y) is: 97
    The quotient (x / y) is: 33
    The remainder (x % y) is: 1
    The product (x * y) is: 300
    

    Since x and y are integers, (x / y) is calculated using integer division, and the fractional part of the quotient is discarded. In this case, (100 / 3) equals 33, not 33.333 as you might expect. C++ does NOT do rounding here, so even if the fractional part was 0.999 it would be ignored.

    The C++ modulo operator % is used to calculate the remainder after integer division. In this case, (100 % 3) is equal to 1. The modulo operator is very handy for checking if one integer is a multiple of another. When we have (x % y) equal to zero, we know that y is a factor of x.

    Test the program by running it several times using a range of input values. What happens if you type in a number bigger than 100? What happens if you type in a negative number? See if you can "break the program" by typing in something that causes an error.

    Copy and paste TWO samples of your program output below.

  9. Change the Data Types
  10. Variables in C++ all have a "data type" and a "name". The most commonly used data types in C++ are "char", "short", "int", "long", "float" and "double".

    Edit your prog1.cpp program and go to the variable declaration section and change all of the "int" variables into "float" variables.

    Now compile your program by typing "g++ prog1.cpp -o prog1.exe". This time, you will see several error messages. These messages should tell you the line number with the problem, and some description of the error.

    Edit the program again to "comment out" the lines with errors on them by adding "//" at the beginning of the line. When you recompile the program, the error messages should go away.

    Copy and paste your revised program below.

  11. Test the Program Again
  12. To run the program you just compiled type in "./prog1.exe". You should be prompted to enter an integer. Type in a variety of input values to see what you get.

    Notice that this time you get fractional results for the quotient when x=3. What happens if you enter 0 as the input value?

    Copy and paste TWO samples of your program output below.

  13. Create a Second Program
  14. As you might guess, different types of variables take different amounts of memory when used in a C++ program. The program below illustrates how you can use the "sizeof" command to find out how many bytes of memory a variable takes.

    #include <iostream>
    using namespace std;
    
    int main ()
    {
      // Variable declarations 
      char c;
      float f;
      double d;
      int i;
      long l;
      short s;
      float sum;
      float mean;
    
      // Print the size of variables
      cout << "Sizeof c is: " << sizeof(c) << endl;
      cout << "Sizeof f is: " << sizeof(f) << endl;
      cout << "Sizeof d is: " << sizeof(d) << endl;
      cout << "Sizeof i is: " << sizeof(i) << endl;
      cout << "Sizeof l is: " << sizeof(l) << endl;
      cout << "Sizeof s is: " << sizeof(s) << endl;
    
      // Arithmetic operations
      sum = sizeof (c) + sizeof(f) + sizeof (d) 
          + sizeof (i) + sizeof(l) + sizeof(s);
      mean = sum / 6.0;
    
      // Print the output
      cout << "The sum of bytes needed to store the variables is: " 
           << sum << endl;
      cout << "The average number of bytes needed to store each variable is: " 
           << mean << endl;
      return 0;
    }
    

    Type in "nano -c prog2.cpp" in the Terminal window. Cut and paste the program above into the nano window, and save (^O) and exit (^X) from nano. Now compile your program by typing "g++ prog2.cpp -o prog2.exe". Hopefully you will not get any error messages.

    Run the second program and cut and paste your output below.

  15. Create a Third Program
  16. Computers are ideal for numerical calculations, so in this section we will work to develop a program to compute the mean and variance of several input values. The mathematical definition of the mean of N values is:

    mean = (value1 + value2 + ... + valueN) / N
    

    The variance is a measure of the average distance squared between N values and their mean. It is calculated using the following formula:

    variance = ((value1-mean)*(value1-mean) 
             + (value2-mean)*(value2-mean) + ... 
             + (valueN-mean)*(valueN-mean)) / N
    

    The C++ program below calculates the mean and variance of the values (8, 10, 12, 14). Notice that we have introduced four variables d1, d2, d3, d4 that contain the difference between each value and the mean. This simplifies the statement used to calculate the variance.

    #include <iostream>
    using namespace std;
    
    int main ()
    {
      // Variable declarations 
      float sum, mean, variance;
      float d1, d2, d3, d4;
    
      // Calculate mean of 4 values
      sum = 8 + 10 + 12 + 14;
      mean = sum / 4.0;
    
      // Calculate variance of 4 values
      d1 = 8 - mean;
      d2 = 10 - mean;
      d3 = 12 - mean;
      d4 = 14 - mean;
      variance = (d1 * d1 + d2 * d2 + d3 * d3 + d4 * d4) / 4.0;
    
      // Print the output
      cout << "Mean = " << mean << endl;
      cout << "Variance = " << variance << endl;
      return 0;
    }
    

    Type in "nano -c prog3.cpp" in the Terminal window. Cut and paste the program above into the nano window, and save (^O) and exit (^X) from nano. Now compile your program by typing "g++ prog3.cpp -o prog3.exe". Hopefully you will not get any error messages.

    Run the third program and cut and paste your output below.

  17. Modify your Third Program
  18. Edit your prog3.cpp file and change the program so it calculates the mean and variance of the values (1, 5, 17, 21). Recompile your program and see what it generates. Notice that the variance is much larger this time. This is because there is a larger range of values than with (8, 10, 12, 14).

    In general, it would be painful to edit a program every time we want to change the input values. There is a lot of typing, and we have to recompile the program after every edit. A better solution is to prompt the user to type in the numbers when the program executes. This way, the user only enters each number once, and we do not need to recompile the code.

    Copy and paste your revised program below.

    Copy and paste your program output for TWO tests below.

  19. Submit Work
  20. When you are finished programming, logout of turing:
    exit (or control-D)

    This lab assignment will be submitted electronically to the TAs once you fill in the fields below and click on the "submit" button. Your lab work will also be mailed to your email address so you a copy for your records.

    Raise your hand and show the output screen to the TA so they can verify that you have completed the lab and enter your name on the lab attendance sheet. See you next week!

    Your UAID number:
    Your website PASSWORD: