Program Prototype - Programming and Problem Solving I - Project 3 | CECS 174, Lab Reports of Computer Science

Material Type: Lab; Class: Programming & Problem Solving I; Subject: Computer Engr & Computer Sci; University: California State University - Long Beach; Term: Fall 2003;

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-sm3
koofers-user-sm3 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CECS 174 Project 3 - Due in lab on Tuesday, 10/21/03
Write a C++ program to generate and count all the bit strings of length 8 that contain 4 consecutive ones,
starting with the prototype program given below.
Program prototype
//////////////////////////////////////////////////////////////
//
//
// Add prolog comments as per Documentation Guidelines
//
//
///////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
using namespace std;
bool has_4ConsOnes( int i ); // finds out if i, 0<=i<255, has 3 consecutive 0s
void printbits(int i);
int main()
{
int count = 0; // count number with 4 consecutive 1s
// Find and output all bit strings of length 8 that contain four
// consecutive ones, using functions has_4ConsOnes and printbits
cout << "\nNumber of bit strings of length 8 with 4 consecutive 1s = "
<< count << endl;
cout << endl;
return 0;
}// end main
bool has_4ConsOnes( int i )
// Describe function
{
// Implement your function
}
void printbits ( int i )
// This function takes an integer i, where 0 <= i < 256, and
// outputs it in the form of an 8-bit string.
// Precondition: Parameter i is an integer with the range of [0, 255]
// Postcondition: The binary representation of I is output
{
string bits[16] = {"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"};
cout << bits[i/16] << bits[i%16] << " ";
}
…/Continued on the other side.
pf2

Partial preview of the text

Download Program Prototype - Programming and Problem Solving I - Project 3 | CECS 174 and more Lab Reports Computer Science in PDF only on Docsity!

CECS 174 Project 3 - Due in lab on Tuesday, 10/21/

Write a C++ program to generate and count all the bit strings of length 8 that contain 4 consecutive ones, starting with the prototype program given below. Program prototype ////////////////////////////////////////////////////////////// // // // Add prolog comments as per Documentation Guidelines // // /////////////////////////////////////////////////////////////// #include #include using namespace std; bool has_4ConsOnes( int i ); // finds out if i, 0<=i<255, has 3 consecutive 0s void printbits(int i); int main() { int count = 0; // count number with 4 consecutive 1s // Find and output all bit strings of length 8 that contain four // consecutive ones, using functions has_4ConsOnes and printbits cout << "\nNumber of bit strings of length 8 with 4 consecutive 1s = " << count << endl; cout << endl; return 0; }// end main bool has_4ConsOnes( int i ) // Describe function { // Implement your function } void printbits ( int i ) // This function takes an integer i, where 0 <= i < 256, and // outputs it in the form of an 8-bit string. // Precondition: Parameter i is an integer with the range of [0, 255] // Postcondition: The binary representation of I is output { string bits[16] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; cout << bits[i/16] << bits[i%16] << " "; } …/Continued on the other side.

When your program executes, it should produce output as in the following sample dialogue: All bit strings of length 8 that contain 4 consecutive 1s: 00001111 00011110 00011111 00101111 00111100 00111101 00111110 00111111 01001111 01011110 01011111 01101111 01111000 01111001 01111010 01111011 01111100 01111101 01111110 01111111 10001111 10011110 10011111 10101111 10111100 10111101 10111110 10111111 11001111 11011110 11011111 11101111 11110000 11110001 11110010 11110011 11110100 11110101 11110110 11110111 11111000 11111001 11111010 11111011 11111100 11111101 11111110 11111111 Number of bit strings of length 8 with 4 consecutive 1s = 48


C++ Statements Needed :  Declaration for int and string variables ( int and string declarations).  Output integers and character strings ( cout << ).  Integer division ( / ) and the remainder ( % ) operations.  for loop.  if-else statements.


Do the following:  Name your program file lab3.cpp and place it in your 174 subdirectory.  Document your program following the Program Documentation Guide which may be accessed through http://www.cecs.csulb.edu/~lam/cecs174/course-notes.html  Make sure your program’s output matches the sample dialogue given above.  Use script to get a typescript file that captures: a) cat lab3.cpp b) g++ lab3.cpp c) a.out.  Print and hand in a hardcopy of the typescript file.  Demo your program on the due date.