



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1 // RollDie.cs 2 // Rolling 12 dice. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 using System.IO; 11 12 public class RollDie : System.Windows.Forms.Form 13 { 14 private System.Windows.Forms.Button rollButton; 15 16 private System.Windows.Forms.RichTextBox displayTextBox; 17 18 private System.Windows.Forms.Label dieLabel1; 19 private System.Windows.Forms.Label dieLabel2; 20 private System.Windows.Forms.Label dieLabel3; 21 private System.Windows.Forms.Label dieLabel4; 22 private System.Windows.Forms.Label dieLabel5; 23 private System.Windows.Forms.Label dieLabel6; 24 private System.Windows.Forms.Label dieLabel7; 25 private System.Windows.Forms.Label dieLabel8; 26 private System.Windows.Forms.Label dieLabel9; 27 private System.Windows.Forms.Label dieLabel10; 28 private System.Windows.Forms.Label dieLabel11; 29 private System.Windows.Forms.Label dieLabel12; 30 31 private System.ComponentModel.Container components = null; 32 33 Random randomNumber = new Random(); 34 int[] frequency = new int[ 7 ]; 35
36 public RollDie() 37 { 38 InitializeComponent(); 39 } 40 41 // Visual Studio .NET generated code 42 43 [STAThread] 44 static void Main() 45 { 46 Application.Run( new RollDie() ); 47 } 48 49 private void rollButton_Click( 50 object sender, System.EventArgs e ) 51 { 52 // pass the labels to a method that will 53 // randomly assign a face to each die 54 DisplayDie( dieLabel1 ); 55 DisplayDie( dieLabel2 ); 56 DisplayDie( dieLabel3 ); 57 DisplayDie( dieLabel4 ); 58 DisplayDie( dieLabel5 ); 59 DisplayDie( dieLabel6 ); 60 DisplayDie( dieLabel7 ); 61 DisplayDie( dieLabel8 ); 62 DisplayDie( dieLabel9 ); 63 DisplayDie( dieLabel10 ); 64 DisplayDie( dieLabel11 ); 65 DisplayDie( dieLabel12 ); 66 67 double total = 0; 68 69 for ( int i = 1; i < 7; i++ ) 70 total += frequency[ i ];
72 displayTextBox.Text = "Face\tFrequency\tPercent\n"; 73 74 // output frequency values 75 for ( int x = 1; x < frequency.Length; x++ ) 76 { 77 displayTextBox.Text += x + "\t" + 78 frequency[ x ] + "\t\t" + String.Format( "{0:N}", 79 frequency[ x ] / total * 100 ) + "%\n"; 80 } 81 82 } // end Main 83 84 // simulates roll, display proper 85 // image and increment frequency 86 public void DisplayDie( Label dieLabel ) 87 { 88 int face = randomNumber.Next( 1, 7 ); 89 90 dieLabel.Image = Image.FromFile( 91 Directory.GetCurrentDirectory() + 92 "\images\die" + face + ".gif" ); 93 94 frequency[ face ]++; 95 } 96 97 } // end class RollDie
1 // StudentPoll.cs 2 // A student poll program. 3 4 using System; 5 using System.Windows.Forms; 6 7 class StudentPoll 8 { 9 // main entry point for application 10 static void Main( string[] args ) 11 { 12 int[] responses = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 13 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 14 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 }; 15 16 int[] frequency = new int[ 11 ]; 17 string output = ""; 18 19 // increment the frequency for each response 20 for ( int answer = 0; answer < responses.Length; answer++ ) 21 ++frequency[ responses[ answer ] ]; 22 23 output += "Rating\tFrequency\n"; 24 25 // output results 26 for ( int rating = 1; rating < frequency.Length; rating++ ) 27 output += rating + "\t" + frequency[ rating ] + "\n"; 28 29 MessageBox.Show( output, "Student poll program", 30 MessageBoxButtons.OK, MessageBoxIcon.Information ); 31 32 } // end method Main 33 34 } // end class StudentPoll