代码改变世界

使用程序调试输出窗口

2012-04-25 21:32  精诚所至 金石为开  阅读(540)  评论(0编辑  收藏  举报

程序输出窗口用来对程序运行过程进行监控。

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace a
{
	class Program
	{
		static void Main(string[] args)
		{
			int[] textArray={4,7,4,2,7,3,7,8,3,9,1,9};
			int[] maxValIndices;
			int maxVal= Maxima(textArray,out maxValIndices);
			Console.WriteLine("Maximum value {0} found at element indices:",maxVal);
			foreach(int index in maxValIndices)
			{
				Console.WriteLine(index);
			}
			Console.ReadKey();
		}
		static int Maxima(int[] integers,out int[] indices)
		{
			Debug.WriteLine("Maximum value search started.");
			indices=new int[1];
			int maxVal=integers[0];
			indices[0]=0;
			int count=1;
			Debug.WriteLine("Maximum value initialized to"+maxVal+",at element index 0.");
			for (int i=1;i<integers.Length;i++)
			{
				Debug.WriteLine("Now looking at element at index"+i+".");
				if(integers[i]>maxVal)
				{
					maxVal=integers[i];
					count=1;
					indices=new int[1];
					indices[0]=i;
					Debug.WriteLine("New maximum found. New value is"+maxVal+",at element index"+i+".");
				}
				else
				{
					if(integers[i]==maxVal)
					{
						count++;
						int[] oldIndices=indices;
						indices=new int[count];
						oldIndices.CopyTo(indices,0);
						Debug.WriteLine("Duplicate maximum found at element index"+i+".");
					}
				}
			}
			Trace.WriteLine("Maximum value "+maxVal+"found,with"+count+"occurences.");
			Debug.WriteLine("Maximum value search completed.");
			return maxVal;
		}
	}
}