CPP Week 3

From IAMMediaWiki Create Change

Jump to: navigation, search

Contents

Lecture Notes

Homework

  • Read Chapter 4 - 6 Pages 81-161 Covers functions, arrays and pointers

Programming Assignment

A number is said to be perfect if the sum of its factors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6= 1 + 2 + 3).

Write a function called perfect that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect.

Challenge: Implement your solution as a windows form applications using appropriate graphics, lines, etc.

In Class Assignment

Problem 1

Write a recursive function int count_digit(int n, int digit); to count the number of digits in a number n (n > 0) that are equal to a specified digit.

For example, if the digit we're searching for were 2 and the number we're searching were 220, the answer would be 2.

 
#include <iostream>
using namespace std;
int count_digit(int n, int digit);  //declare the function
 
int main () {
	
        int number, find;
	cout << "enter the number and the digit to test for " << endl;
	cin >>number;
	cin >>find;
/*
        call the function
*/
	cout << "the total times number appears is = " << count_digit(number, find) << endl; 
	return (0);
 
}
 
	int count_digit(int n, int digit)  //define the function
{
	if (n == 0) return 0;
 
	if (n % 10 == digit) return 1 + count_digit(n / 10, digit);
	else return count_digit(n / 10, digit);
}
 

Problem 2

Write a program that simulates coin tossing. For each toss of the coin, the program should print HEADS or TAILS. Let the program toss the coin 100 times and count the number of times each side of the coin appears. Print the results. The program should call a separate function FLIP that takes no arguments and returns 0 for tails and 1 for heads.

[Note: If the program realistically simulates the coin tossing, then each side of the coin should appear approximately half the time]

 
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
 
 
int flip(void);  //Function prototype
 
int main ()
{
	int headsCount = 0;
	int tailsCount = 0;
 
	srand ( time(0) );  //seed random number generator
 
	//simulate coin toss 100 times
 
	for (int loop =1; loop <=100; loop++)
	{
		if  (flip () == 0 ) // 0 return refers to tails
		{
			tailsCount++;
			cout << "Tails ";
		} //end if
		else   // 1 refers to heads
		{
			headsCount++;
			cout << "Heads ";
		} // end else
		if (loop % 10 == 0) cout << endl;   // print ten per line
 
	} // end for
 
	// display totals
 
	cout << "\nThe total number of Heads was " <<headsCount;
	cout << "\nThe total number of Tails was " <<tailsCount << endl;
 
	return 0;
 
} // End Main
 
int flip (void)
{
	return rand() % 2;   //scale by 2
} // end flip
 

Problem 3

Convert your Coin Toss Program to run as a windows form application. Print the following text/graphics on your form. Image:CoinToss.jpg

 
#pragma once
namespace CoinTossGraph
{
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
//Function Prototypes  
 
int flip(void);  //Function prototype
 
	/// <summary> 
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the 
	///          'Resource File Name' property for the managed resource compiler tool 
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public __gc class Form1 : public System::Windows::Forms::Form
	{	
	public:
		Form1(void)
		{
			InitializeComponent();
		}
  
	protected:
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
	private: System::Windows::Forms::PictureBox *  pictureBox1;
 
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container * components;
 
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->pictureBox1 = new System::Windows::Forms::PictureBox();
			this->SuspendLayout();
			// 
			// pictureBox1
			// 
			this->pictureBox1->Location = System::Drawing::Point(32, 24);
			this->pictureBox1->Name = S"pictureBox1";
			this->pictureBox1->Size = System::Drawing::Size(544, 440);
			this->pictureBox1->TabIndex = 0;
			this->pictureBox1->TabStop = false;
		
			// 
			// Form1
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
			this->ClientSize = System::Drawing::Size(600, 509);
			this->Controls->Add(this->pictureBox1);
			this->Name = S"Form1";
			this->Text = S"Form1";
			this->Load += new System::EventHandler(this, Form1_Load);
			this->ResumeLayout(false);
 
	}	
	
 
	 private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
				 			
	 pictureBox1->Dock = DockStyle::Fill;
	 pictureBox1->BackColor = Color::White;
//
// Connect PaintEvent to PictureBox1
//
	 pictureBox1->Paint += new System::Windows::Forms::PaintEventHandler
	(this, &Form1::pictureBox1_Paint);
					
         }
 
   void pictureBox1_Paint(Object *  sender, System::Windows::Forms::PaintEventArgs* e)
{
	Graphics *g = e->Graphics;
        int headsCount = 0;
	int tailsCount = 0;
	int pointx = 30, pointy = 30;
	srand ( time (0));  //seed random number generator
 
 
	//simulate coin toss 100 times
 
	for (int loop =1; loop <=100; loop++)
	{
	pointx+=50;
	if  (flip () == 0 ) // 0 return refers to tails
        {
	tailsCount++;
        g->DrawString(S"tt ",new System::Drawing::Font(S"Wingdings 2",20),  
        System::Drawing::Brushes::Blue, Point(pointx,pointy));
	} //end if
	else   // 1 refers to heads
        {
	headsCount++;
	g->DrawString(S"uu ",new System::Drawing::Font(S"Wingdings 2",20),  
        System::Drawing::Brushes::Red, Point(pointx,pointy));
	} // end else
	if (loop % 10 == 0) {
	pointx = 30;
	pointy+=30;
	}   // print ten per line
 
	} // end for
 
	// display totals
 
	g->DrawString(S"Total Heads = ",new System::Drawing::Font
           (S"Arial",20,FontStyle::Bold),
            System::Drawing::Brushes::Red, Point(pointx + 50,pointy + 30));
//
	g->DrawString(System::Convert::ToString(headsCount),new 
            System::Drawing::Font(S"Arial",20,FontStyle::Bold), 
            System::Drawing::Brushes::Red, Point(pointx + 250,pointy + 30));
//
	g->DrawString(S"Total Tails = ",new System::Drawing::Font
          (S"Arial",20,FontStyle::Bold), System::Drawing::Brushes::Blue, 
          Point(pointx + 50,pointy + 60));
//
	g->DrawString(System::Convert::ToString(tailsCount),new 
           System::Drawing::Font(S"Arial",20,FontStyle::Bold),
           System::Drawing::Brushes::Blue, Point(pointx + 250,pointy + 60));
					 }
int flip (void)
{
	return rand() % 2;   //scale by 2
} // end flip
	};
 
}
Personal tools