/* (程序頭部注釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙台大學計算機學院學生
* All rights reserved.
* 文件名稱:輸入10個數存入數組,求最大值、最小值和平均值
* 作 者: 雷恆鑫
* 完成日期: 2012 年 09 月 09 日
* 版 本 號: V1.0
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:
* 程序輸出:
* 程序頭部的注釋結束
*/
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication_do_while
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("這是一個輸入10個數存入數組,求最大值、最小值和平均值的程序");
double[] c = new double[10];
for (int number = 0; number < 10; ++number)
{
Console.Write("請您輸入第{0}個數:", number + 1);
// string s = Console.ReadLine();
c[number] = double.Parse(Console.ReadLine());
}
//利用foreach語句循環輸出數組中每一個元素
Console.Write("您輸入10個數為:");
foreach (double var in c)
{
Console.Write(var); //依次讀取字符串中的元素
Console.Write(" ");
}
Console.WriteLine();
double min1 = min(c);
Console.WriteLine("您輸入10個數中最小值為:{0}",min1);
double max1 = max(c);
Console.WriteLine("您輸入10個數中最大值為:{0}",max1);
double average1 = average(c);
Console.WriteLine("您輸入10個數中平均值為:{0}",average1);
Console.ReadKey();
}
static double max(double[] c)
{
double max = c[0];
for (int i = 1; i < 10; ++i)
{
if (max < c[i])
max = c[i];
}
return max;
}
static double min(double[] c1)
{
double min = c1[0];
for (int i = 1; i < 10; ++i)
{
if (min > c1[i])
min = c1[i];
}
return min;
}
static double average(double[] c1)
{
double average;
double all=0;
/* foreach (char var in c1)
{
all = all + var;
}*/
for (int i = 0; i < 10; ++i)
{
all = all + c1[i];
}
average = all / c1.Length;
return average;
}
}
}
運行結果:
附件:
下面這個程序是錯誤的,他是計算的數字對應的ASCII的最大值。
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication_do_while
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("這是一個輸入10個數存入數組,求最大值、最小值和平均值的程序");
Console.Write("請您輸入10個數(數與數之間用空格斷開):");
string s = Console.ReadLine();
char[] c = s.ToCharArray();
int i = c.Length;
int j = 0;
int[] c1 = new int[c.Length];
//利用foreach語句循環輸出數組中每一個元素
foreach (char var in c)
{
//c1[j] = Convert.ToInt32(var);//轉化成了字符的ASCII碼
c1[j] = var;
++j; //字符串讀取
}
Console.Write("您輸入10個數為:");
foreach (char var in c1)
{
Console.Write(var); //依次讀取字符串中的元素
}
Console.WriteLine();
int min1 = min(c1);
Console.WriteLine("您輸入10個數中最小值為:{0}",min1);
int max1 = max(c1);
Console.WriteLine("您輸入10個數中最大值為:{0}",max1);
double average1 = average(c1);
Console.WriteLine("您輸入10個數中平均值為:{0}",average1);
Console.ReadKey();
}
static int max(int[] c1)
{
int n = c1.Length;
int max = c1[0];
for (int i = 2; i < n; i = i+2)
{
if (max < c1[i])
max = c1[i];
}
return max;
}
static int min(int[] c1)
{
int n = c1.Length;
int min = c1[0];
for (int i = 2; i < n; i = i + 2)
{
if (min > c1[i])
min = c1[i];
}
return min;
}
static double average(int[] c1)
{
double average;
int all=0;
/* foreach (char var in c1)
{
all = all + var;
}*/
for (int i = 0; i < c1.Length; i = i + 2)
{
all = all + c1[i];
}
average = all / c1.Length;
return average;
}
}
}
運行結果: