C++ Intro

Assignment 2: fundamental data types


Description

The purpose of this assignment is to experiment with the properties of C++ fundamental types. You should continue using the Microsoft VC++ project created in your first assignment. You have one week to complete your work. Solutions to the given problems will be available at the next lecture.

Instructions

Modify main.cpp program as follows:
  1. Think of as many different C++ fundamental types as you can. For example, int and unsigned int, short and unsigned short are all different types. Write code to print the sizes of various data types on the screen. Run your program and take a look at the results. Change your code to make sure that your program prints all sizes in ascending order.

  2. Add local two integer variables to your main( ) function.

  3. Next, input two integer values from the user. Lecture 2 handout, section Character Type, has an example of a statement std::cin >> c; to input a character. Take advantage of std::cin example and write similar code to get user inputs for two integer variables. (We will cover details of the stream input/output in the next lecture.) Display entered values on the screen.

  4. Write code to swap two integers. If you can, try to replace swap code by a single expression. Hint: you may use comma (sequence) operator to combine your statements into a single expression. Print results of the swap on the computer screen.

  5. Write an expression to determine the max of two numbers stored in your variables. Print max value on the screen.

  6. Write some code to determine whether your integers are odd or even, and print the results on the screen. Hint: use modulus operator. If the result of (value % 2) is equal to 0, the number is even. If the result of (value % 2) is equal to 1, the number is odd. To evaluate a condition and make a decision, use Arithmetic If expression, such as (condition) ? "odd" : "even" .

  7. Finally, find out what happens if you divide by zero on your system. You may give the user a hint to suggest that entering a particular number might result in program crash. (Normally programs behave more defensively to minimize dependence on the user input. Real-world programs pay a lot of attention to the input validation.)

To turn in your homework, please submit your program before the next lecture via an email attachment sent to your instructor,

Igor Kholodov Igor.Kholodov@bristolcc.edu

Good luck!