C#实验三数组.docx
- 文档编号:15756199
- 上传时间:2023-07-07
- 格式:DOCX
- 页数:14
- 大小:62.69KB
C#实验三数组.docx
《C#实验三数组.docx》由会员分享,可在线阅读,更多相关《C#实验三数组.docx(14页珍藏版)》请在冰点文库上搜索。
C#实验三数组
实验4数组
一、实验学时:
2课时
二、实验目标
1.了解数组的概念,掌握数组的声明、初始化方式;
2.熟练掌握foreach语句的用法;
3.了解多维数组的定义和声明,理解规则数组和不规则数组的使用区别;
4.掌握数组转换和数组排序方法的使用;
5.掌握array类静态方法运用;
三、实验要求
1.在上实验课之前,每一个同学必须将实验的题目、程序编写完毕,做好充分的准备。
2.所有实验环节均由每位同学独立完成,严禁抄袭他人实验结果,若发现有结果雷同者,按实验课考核办法处理。
3.写出程序,并调试程序,要给出测试数据和实验结果。
4.整理上机步骤,总结经验和体会。
5.实验环境要求:
VisualStuudio2010。
6.实验知识点要求:
数组。
四、实验内容
本实验共有三个实验任务。
、实验任务1:
数组的声明和使用。
在本实验任务中,将声明并使用数组。
声明一个数组,将一年中的12个月的英文存入其中。
当用户输入月份的数字时,打印出月份的英文。
若输入0则退出,并提供输入信息不合法提示,如下:
请输入月份数,若输入0则退出:
(输入2,显示February)
2
February
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceShuZu
{
classProgram
{
staticvoidMain(string[]args)
{
string[]Month={"January","February","March","April","May","June","July","August","September","October","November","December"};
stringmon;
intmonth;
Console.WriteLine("请?
输º?
入¨?
月?
份¤Y数ºy,ê?
若¨?
输º?
入¨?
0则¨°退ª?
出?
");
mon=Console.ReadLine();
month=Convert.ToInt16(mon);
if(month==0)
{
Console.WriteLine("程¨¬序¨°即¡ä将?
退ª?
出?
!
ê?
");
}
else
{
Console.WriteLine("{0}",Month[month-1]);
}
System.Console.WriteLine();
}
}
}
、实验任务2:
数组和数组类静态方法结合应用。
产生10个1到50之间的随机整数放在数组中,然后对这个数组的进行排序(降序),利用已做好程序(昨天布置课堂作业),利用Array类Sort()和Reverse()来进行完成这道程序。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Linq;
usingSystem.Text;
namespaceConsoleApplication1
{
classshuzhu
{
staticvoidMain(string[]args)
{
intmax;
intmin;
int[]shuzu=newint[10];
Console.WriteLine("随?
机¨²产¨²生¦¨²50以°?
内¨²的Ì?
10个?
随?
机¨²数ºy");
Randomrandom=newRandom();
for(inti=0;i<10;i++)
{
shuzu[i]=random.Next(1,50);
Console.WriteLine("第̨²{0}个?
数ºy是º?
:
êo{1}",i+1,shuzu[i]);
}
Console.WriteLine("Sort排?
序¨°结¨¢果?
:
êo");
Console.WriteLine();
Array.Reverse(shuzu);
//Array.Reverse(shuzhu);
for(inti=0;i<10;i++)
{
//shuzu[i]=random.Next(1,50);
Console.WriteLine("第̨²{0}个?
数ºy是º?
:
êo{1}",i+1,shuzu[i]);
}
max=min=shuzu[0];
for(inti=0;i<9;i++)
{
if(shuzu[i]>max)
{max=shuzu[i];}
elseif(i min=shuzu[i]; } Console.WriteLine("最Á? 大䨮值¦Ì是º? {0},最Á? 小? 值¦Ì是º? {1}",max,min); Console.ReadLine(); } } } 、实验任务3: 交错数组应用。 利用交错数组编写程序实现下列图形。 模仿书上例子4-6 1 24 3927 41664256 5251256253125 usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceJiaoCuoShuZu { classProgram { staticvoidMain(string[]args) { constintk=5; int[][]arr=newint[k][]; for(inti=0;i for(intj=1;j { arr[i]=newint[j]; } for(inti=0;i { for(intj=0;j { if(i==0&&j==0) { arr[i][j]=(int)Math.Pow(i,j); } intm,n; m=i; n=j; arr[i][j]=(int)Math.Pow(m+1,n+1); } } for(inti=0;i { for(intj=0;j { System.Console.Write("{0}",arr[i][j]); } System.Console.WriteLine(); } } } } 五、思考题 1.交错数组和二维数组区别,举例来说明 二维数组实际上是一个一维数组,这个一维数组的每一个成员又是一个一维数组。 交错数组是数组的数组,因此其元素是引用类型并初始化为 null.交错数组元素的维度和大小可以不同。 int[2,3]那就是说一个两行三列,每一个元素都是一个整型数的数组,但是交错数组int[2][],意思是这个数组有两个元素,每一个元素都是一个整型的数组,但是长度可以不一样,例如: int[][]arr=newint[2][]; int[0][]=newint[10]; int[1][]=newint[8]; 2.用C#编写,直接插入排序,冒泡排序,简单选择排序 插入排序 usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceInsertionSorter { publicclassInsertionSorter { publicvoidSort(int[]xuanze) { for(inti=1;i { intt=xuanze[i]; intj=i; while((j>0)&&(xuanze[j-1]>t)) { xuanze[j]=xuanze[j-1]; --j; }xuanze[j]=t; } } } publicclassMainClass { publicstaticvoidMain() { intm; int[]iArrary=newint[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47}; InsertionSorterii=newInsertionSorter(); ii.Sort(iArrary); for(m=0;m Console.WriteLine("{0}",iArrary[m]); Console.WriteLine(); } } } 冒泡排序 本人用了C#开发出冒泡排序算法。 希望能为C#语言的学习者带来一些益处。 不要忘了,学语言要花大力气学数据结构和算法。 usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceBubbleSorter { publicclassBubbleSorter { publicvoidSort(int[]list) { inti,j,temp; booldone=false; j=1; while((j done)) { done=true; for(i=0;i { if(list[i]>list[i+1]) { done=false; temp=list[i]; list[i]=list[i+1]; list[i+1]=temp; } } j++; } } } publicclassMainClass { publicstaticvoidMain() { int[]iArrary=newint[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47}; BubbleSortersh=newBubbleSorter(); sh.Sort(iArrary); for(intm=0;m Console.Write("{0}",iArrary[m]); Console.WriteLine(); } } } 选择排序 usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceSelectionSorter { publicclassSelectionSorter { privateintmin; publicvoidSort(int[]list) { for(inti=0;i { min=i; for(intj=i+1;j { if(list[j] min=j; } intt=list[min]; list[min]=list[i]; list[i]=t; } } } publicclassMainClass { publicstaticvoidMain() { int[]iArrary=newint[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47}; SelectionSorterss=newSelectionSorter(); ss.Sort(iArrary); for(intm=0;m Console.Write("{0)",iArrary[m]); Console.WriteLine(); } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 实验 数组