CSCI 201 Lab 2 -- Binary numbers and C++ expressions

Binary numbers

One of your tasks in Assignment 1 was to convert a few decimal numbers to eight-bit twos-complement representation. We've written a Java applet to give you a little practice in decimal-to-binary conversation.

In the table below, you'll see some decimal numbers in the left hand side. These are your targets. In the middle of the table, you see a row of black and red dots. These represent LED's. Red means binary 1. Black means binary 0. Go ahead and mash one of the dots. It changes state -- 0 becomes 1, 1 becomes 0. Notice that the rightmost number also changes. It is the decimal number corresponding to the binary number.

Your job is to set the binary bits so that the right column matches the left column.
TargetYour guess
43
66
100
127
-1
-40
-86
-128

Now try out a couple of interesting 16-bit two-complement numbers.
TargetYour guess
1998
-1998

Don't forget there is some binary arithmetic in Assignment 1. The middle rows of circle in the following figure are two eight-bit numbers to add. You press these to set the number. The top row of squares is the carries and the bottom row is the sum. Now add 66 and -40.

Lab Checkoff 1

Ask your lab instructor to verify that you've pushed all the right buttons.

Expressions in C++

Start up Visual C++ and create a new Win 32 Console Application project Lab02 in the directory C:\FILES. If you need help getting started, look back at Lab 1.

Open a new C++ Source File in Lab02.cpp and type in the following outline of the main routine:

//  Lab 2  -- CSCI 201.0L?
//      Your Name
//
#include <iostream.h>
int main()
{


  return 0 ;
}

Even though this program does absolutely nothing, go ahead and Build it, just to make sure you've created the program, project, and workspace correctly.

At the beginning of your program, add declarations for two integer variables i and j and two float variables y and z.

Now add statements to read user-provided values for these variables. Be sure to prompt before reading each value. Here's some example code for setting i.

cout << "Enter integer value for i" << endl ;
cin >> i ;

Finally, write statements to print the six expressions listed in the following table:
Expression
Number
Expression
1Average of y and z
2Average of i and j
3The reminder when i is divided by j
4
 2
y  +  4 j z
5
 y + z
-------
 y - z
6
   i
--------
 i + 1
To help you out, we're going to show you how to compute and print the first expression

cout << "Expression 1 is " << (y+z)/2 << endl ;

Lab Checkoff 2

After you have thoroughly tested your code, ask your instructor to test it. If it passes the test, print your program and leave a copy with your lab instructor.


Return to CSCI 201 page
CSCI logo Return to the UNCA Computer Science home page