By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Issue with Breakpoint giving different results in application that is no breakpoint a

P: 1
Not sure if others have come acrossed this bit I have a program for a C# class I am taking and during my troubleshooting I have found that if I turn on a breakpoint and then press F5 to continue until the program finishes I am receiving different results than if I didn't insert a breakpoint. (Put breakpoint at 1st curly bracket after AddToSumCount(rollSum); line in RollDice() method):

Here is a copy of my code if you want to see it:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Lab5_Ex2
  6. {
  7.     class TwoDiceStats
  8.     {
  9.         //declare class variables
  10.         private int dice1Roll, dice2Roll, rollSum;
  11.  
  12.         //declare & create class arrays
  13.         int[] sumArray = new int[13]; 
  14.         int[] dice1Array = new int[37];
  15.         int[] dice2Array = new int[37];
  16.         int[] calcSumArray = new int[37];
  17.  
  18.         public TwoDiceStats()
  19.         {
  20.         }
  21.  
  22.         public void RollDice()
  23.         {
  24.  
  25.             for (int roll = 1; roll < dice1Array.Length; roll++)
  26.             {
  27.                 //Roll dice# 1 using Random method
  28.                 Random randomNumber = new Random();
  29.                 dice1Roll = randomNumber.Next(1, 7);
  30.                 dice1Array[roll] = dice1Roll;
  31.                 //Roll dice# 2 using Random method
  32.                 dice2Roll = randomNumber.Next(1, 7);
  33.                 dice2Array[roll] = dice2Roll;
  34.  
  35.                 //Calculate sum of values rolled
  36.                 rollSum = dice1Roll + dice2Roll;
  37.                 calcSumArray[roll] = rollSum;
  38.  
  39.                 //Add to Sum count
  40.                 AddToSumCount(rollSum);
  41.             } 
  42.  
  43.         }
  44.  
  45.         //Add to sum count Method
  46.         public void AddToSumCount(int sum)
  47.         {
  48.             sumArray[sum]++;
  49.         }
  50.  
  51.         //Display Results Method
  52.         public void DisplayResults()
  53.         {
  54.             Console.WriteLine("Roll #\tDice 1\tDice 2\tSum\t\t\tRoll #\tDice 1\tDice 2\tSum");
  55.             Console.WriteLine("------\t------\t------\t---\t\t\t------\t------\t------\t---");
  56.             for (int i = 1; i < dice1Array.Length; i = i + 2)
  57.             {
  58.                 Console.Write("{0,6}\t{1,6}\t{2,6}\t{3,3}", i, dice1Array[i], dice2Array[i], calcSumArray[i]);
  59.                 Console.Write("\t\t\t{0,6}\t{1,7}\t{2,7}\t{3,3}\n", i + 1, dice1Array[i + 1], dice2Array[i + 1], calcSumArray[i + 1]);
  60.             }
  61.  
  62.             Console.WriteLine("\n{0} {1,10}","Sum","Frequency");
  63.             Console.WriteLine("{0} {1,10}", "---", "----------");
  64.  
  65.             for (int sum = 2; sum < sumArray.Length; sum++)
  66.                 Console.WriteLine("{0,3} {1,10}", sum, sumArray[sum]);
  67.         }
  68.     }
  69.  
  70.     class TwoDiceStatsTest
  71.     {
  72.         static void Main(string[] args)
  73.         {
  74.             //create class object
  75.             TwoDiceStats myDice = new TwoDiceStats();
  76.  
  77.             //Call Roll Dice Method
  78.             myDice.RollDice();
  79.  
  80.             //Call Display Results Method
  81.             myDice.DisplayResults();
  82.  
  83.             //keep console open until user presses Enter
  84.             Console.Read();
  85.         }
  86.     }
  87. }
  88.  
Oct 19 '07 #1
Share this Question
Share on Google+
1 Reply


debasisdas
Expert 5K+
P: 8,127
Please check again , it might happen some part of the code is not executing because of the break point.
Oct 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.