CS 150 - Intro to Programming - Fall 2001
Lab Exercise 3

Topics:

Part 1 - Storage Sizes and Range Limits

a.) Use the following program to investigate the storage size of C++ simple data types. Create a CodeWarrior project (YourLastNameLab3)and add the source file given below (YourLastNameLab3Part1a.cpp). Run the program and record the storage size for each data type. Note the sizeof(data type) operator as used below - "returns" the number of bytes used for storage on this computer (Try running this a home on in another lab and see if the results are the same).

/* filename: YourLastNameLab3Part1a.cpp
   author: Your Name
   date last modified: Today's Date

   purpose: show storage size for basic data types
*/
#include <iostream>
using namespace std;
int main()
  {
  //storage sizes for simple C++ Data Types
  cout << "size of C++ Fundamental Data Types\n";
  cout << "__________________________________\n\n";
  cout << "size of bool   :  " << sizeof(bool)   <<endl;
  cout << "size of char   :  " << sizeof(char)   <<endl;
  cout << "size of short  :  " << sizeof(short)  <<endl;
  cout << "size of int    :  " << sizeof(int)    <<endl;
  cout << "size of long   :  " << sizeof(long)   <<endl;
  cout << "    long int   :  " << sizeof(long int) <<endl;
  cout << "size of float  :  " << sizeof(float)    <<endl;
  cout << "size of double :  " << sizeof(double)   <<endl;
  cout << "   long double :  " << sizeof(long double) <<endl<<endl;
  return 0;
  }

b.) Add the source file given below (YourLastNameLab3Part1b.cpp)to your project (be sure to remove part a) for listing the limits associated with the C++ simple data types. Then run the program and record the results. Note the line #include <climits>. This "includes" the file that contains the definitions of the various values listed below. For example SCHAR_MIN contains the minimum value that a signed char type may assume, while INT_MAX contains the maximum value that a signed integer may assume.

/* filename: YourLastNameLab3Part1b.cpp
   author: Your Name
   date last modified: Today's Date

   purpose: Lists the ranges of integer types
*/
#include <iostream>
#include <climits>

using namespace std;
int main()
  {
  //values from climits
  cout << "\n----- Integral values -----\n";
  cout << "minimum signed char value:    " << SCHAR_MIN << "\n";
  cout << "maximum signed char value:    " << SCHAR_MAX << "\n"; 
  cout << "maximum unsigned char value:  " << UCHAR_MAX << "\n\n";
  cout << "minimum (signed) short value: " << SHRT_MIN  << "\n";
  cout << "maximum (signed) short value: " << SHRT_MAX  << "\n";
  cout << "maximum unsigned short value: " << USHRT_MAX << "\n\n";
  cout << "minimum (signed) int value:   " << INT_MIN   << "\n";
  cout << "maximum (signed) int value:   " << INT_MAX   << "\n";
  cout << "maximum unsigned int value:   " << UINT_MAX  << "\n\n";
  cout << "minimum (signed) long value:  " << LONG_MIN  << "\n";
  cout << "maximum (signed) long value:  " << LONG_MAX  << "\n";
  cout << "maximum unsigned long value:  " << ULONG_MAX << "\n";
  return 0;
  }

c.) Add the source file given below (YourLastNameLab3Part1c.cpp) to your project (be sure to remove part b) for listing the range and degrees of precision of C++ floating point data types. Then run the program and record the results. Note the #include<cfloat> statement

/* filename: YourLastNameLab3Part1c.cpp
   author: Your Name
   date last modified: Today's Date

   purpose: List the ranges for floating point types
*/
#include <iostream>
#include <cfloat>
using namespace std;

int main()
  {
  //values from cfloat
  cout << "\n----- float values -----\n";
  cout << "# of decimal digits of precision " << FLT_DIG        << "\n";
  cout << "max value                        " << FLT_MAX        << "\n";
  cout << "max decimal exponent             " << FLT_MAX_10_EXP << "\n";
  cout << "min positive value               " << FLT_MIN        << "\n";
  cout << "min decimal exponent             " << FLT_MIN_10_EXP << "\n";

  cout << "\n----- double values -----\n";
  cout << "# of decimal digits of precision " << DBL_DIG        << "\n";
  cout << "max value                        " << DBL_MAX        << "\n";
  cout << "max decimal exponent             " << DBL_MAX_10_EXP << "\n";
  cout << "min positive value               " << DBL_MIN        << "\n";
  cout << "min decimal exponent             " << DBL_MIN_10_EXP << "\n";

  cout << "\n----- long double values -----\n";
  cout << "# of decimal digits of precision " << LDBL_DIG        << "\n";
  cout << "max value                        " << LDBL_MAX        << "\n";
  cout << "max decimal exponent             " << LDBL_MAX_10_EXP << "\n";
  cout << "min positive value               " << LDBL_MIN        << "\n";
  cout << "min decimal exponent             " << LDBL_MIN_10_EXP << "\n";
  return 0;
  }

Part 2 - Overflow Errors and other invalid input

Write a C++ program that interactively prompts the user for an integer and displays the input value to the screen. Name the source file containing the program YourLastNameLab3Part2.cpp. The dialogue should look exactly as below (when the user inputs 8 - user input appears in bold).

     Enter a number from 1 to 10: 8
     
     You entered 8

Make sure the variable used to store the input is defined as an int, not a double or float.

  1. Verify that your program works correctly for inputs in the range 1 through 10.

  2. Run your program and enter the number 10000000000. What value is displayed?
    You have just observed what is known as an overflow error. Because of the limited range
    of numeric objects, 10000000000 cannot be stored in an int.
    

    The number of bytes used to represent an int is system dependent. Note the number of bytes used to store an int from step 1a. Given this, figure out (mathematically) how large of a number an int can represent without an overflow error occurring. (Note: One bit is used for indicating sign (+/-)). Confirm your answer by running your program and inputting numbers slightly above and slightly below the number you have computed to be the maximum. What is the biggest integer on this system? The smallest?

  3. Run your program and enter the floating-point value 2.9. What value is displayed?

  4. Run your program and enter the characters HELP instead of a valid integer. Describe what happens.

Part 3 - Arithmetic operators and C++ arithmetic statements

Convert the following algebra formulas into C++ statements.

algebra

C++

y = rx + b y = r * x + b
 
 
 
 
 
 


a.) Write a C++ program (YourLastNameLab3Part3a.cpp)that incorporates each of the above C++ statements. Your program should print the formula to the screen, the value of the variables used in the equation and the result of the equation. (note you will need to declare integer type variables named: a, b, c, d and floating point type variables q, r, t, x, y). Before testing each statement, you will need to assign values to the variables used in the equation. Then the program should calculate the result and display a summary of the calculation to the screen. See the example program below for equation y = rx+b; Be sure to use comments to identify each step in your code.

//Header info goes here - 
#include <iostream>
using namespace std;
int main()
  {
  int a, b, c, d;
  float q, r, t, x, y;
  //test run for y = r * x + b
  //Assign test value to variables
  r = 3.5;
  x = 17.3;
  b = 3;
  //calculate results
  y = r * x + b;
  //print summary to screen
  cout << "For the equation y = r * x + b\n";
  cout << "With r = " << r << " x = " << x << " and b = " << b;
  cout << "\ny = " << y << endl;
  //second test run goes here
  //third test run goes here
  //fourth test run goes here
  //etc.......
  return 0;
  }
//program output
For the equation y = r * x + b
With r = 3.5 x = 17.3 and b = 3
y = 63.55

Use the following data to test your program:

a. r = 55	t = 2.3		d = 126
b. a = 10	b = 13	c = 19	d = 5	y = 1
c. r = 5	x = 4	q = 33.3333
d. a = 3	b = 6	c = 2	y = 12
e. a = 12	b = 13	c = 7	d = -20
f. a = 2 	x = 5 	y = 257

b.) Write an interactive C++ program (YourLastNameLab3Part3b.cpp) that computes gross pay for an employee given the hourly wage rate and the number of hours worked.

sample dialog (user input in bold)

Enter wage: 38.50
Enter Hours: 30.0
Gross Pay: $1155.00

c.) Modify the above program to allow for over time pay. SaveAs-> (YourLastNameLab3Part3c.cpp).

sample dialog

Enter wage: 38.50
Enter Hours: 30.0
Enter overtime rate(1.0-2.0): 1.5
Enter overtime Hours: 10;
Gross Pay: $1732.50

Part 4 - Type Compatibility

Investigate what happens when different data type are mixed in expressions.
Recall: ints can store a whole number, floats can store floating point numbers, and a char stores a single character, number, or symbol.

Consider the code segment below:

Given the following variable declarations, what is output by each cout statement?

  int a = 13;
  int b = 17;
  float x = 0.0;
  float y = 0.0;
  //assigning int values to floating point type
  x = a + b;	
  cout << "1.\tx = " << x << endl;

  //assigning float values to an int 
  x = 33.77;
  y = 3.25;
  a = x;
  b = x + y;
  cout << "2.\ta = " << a  << "\tb = " << b << endl;
  //what type of division ???
  x = 7 / 3;
  cout << "3.\tx = " << x << endl;
  x = 7.0 / 3;
  cout << "4.\tx = " << x << endl;
  x = 7 / 3.0;
  cout << "5.\tx = " << x << endl;
  //mix ints and floats
  y = 7.32;
  x = a + y;	
  cout << "6.\tx = " << x <<endl;
  //Test the relationship between chars, ints, and floats
  a = 36;
  char letter = 'A';   //declare a char type named letter
  b = letter;          //assign a char value to an int???
  x = a + letter;      //add a char value to an int???
  cout << "7.\tb = " << b << endl;
  cout << "8.\tx = " << x << endl;

Write a C++ program (YourLastNameLab3Part4.cpp)that incorporates each of the above statements. If your program output does not produce the result you predicted, make sure you discover why?


What to turn in:

Lab Folder: YourLastNameLab3 - With the following files