CSCI 201 Lab 3 -- 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 Lab03 in the directory C:\FILES. If you need help getting started, look back at Lab 2.

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

//  Lab 3  -- 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 eight 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
   2   3
-(y - z )
6
 y + z
-------
 y - z
7
   i
--------
 i + 1
8
   i
---------
 i + 1.0
9
         
 i  
--- * j
 j  
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.

Extra Credit

Write a program that computes the number of seconds, minutes and hours in a given number of seconds. The outline of the main function is given below. You need to fill in the missing expressions.


//  Lab 3  -- CSCI 201.0L?
//      Your Name
//
#include <iostream.h>
int main()
{
   int totalSeconds, minutes, hours, seconds;
   cout << "Enter the total number of seconds: " << flush;
   cin >> totalSeconds;
   // there are 3600 seconds in a hour
   hours = 
   // there are 60 seconds in a minute
   minutes = 
   // the remaining seconds
   seconds = 
   cout << totalSeconds << " written as hours:minutes:seconds = "
        << hours << ":" << minutes << ":"
	<< seconds << endl;
   return 0;
}

Extra Credit Checkoff

After you have tested your code, ask your instructor to test it.


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