实验目的:学会二维数组的操作
实验内容:二维数组的操作
* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作
* 完成日期:
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:由程序产生随机数作为初始值
* 问题描述:……
* 程序输出:……
* 程序头部的注释结束
#include <iostream>
#include <time.h>
using namespace std;
void setdata(int a[8][8]); //设置随机数
void out(int a[8][8]); //输出数组
void outDiagonal(int a[8][8]); //输出对角线元素的值
void mine(int a[8][8],int x, int y); //按"扫雷"游戏的规则输出相邻格子
void change(int a[8][8]); //按要求改变数值
int main()
{
int a[8][8],x,y;
setdata(a);
out(a);
cout<<endl<<"输出对角线的数值"<<endl;
outDiagonal(a);
cout<<endl<<"输入一个位置:";
cin>>x>>y;
while(x < 0 || x > 7 || y < 0 || y > 7)
{
cout << "请重新输入"<<endl;
cin>>x>>y;
}
mine(a,x,y);
cout<<"改变后的数值"<<endl;
change(a);
out(a);
cout<<endl;
return 0;
}
void setdata(int a[8][8])
{
int i,j;
srand(time(NULL));//需要用当前时间作"种子",以便每次运行取得的序列不同
for(i=0;i<8;i++)
for(j=0;j<8;j++)
a[i][j]=rand()P+1;
return;
}
void out(int a[8][8])
{
int n = 0;
for(int i = 0; i <= 7; i++)
for(int j = 0; j <= 7; j++)
{
if(n % 8 == 0)
{
cout<<endl;
n = 0;
}
cout<<a[i][j]<<'\t';
n++;
}
}
void outDiagonal(int a[8][8])
{
cout << "左上右下"<<endl;
for(int i = 0; i <= 7; i++)
{
cout<<a[i][i]<<" ";
}
cout<<endl;
cout<<"右上左下"<<endl;
for(i = 0; i <= 7; i++)
{
cout<<a[i][7-i]<<" ";
}
}
void mine(int array[8][8],int x, int y)
{
int a,b,c,d,sum=0,m,n;
if(x == 0 && y == 0)
{
a = x,b = y,c = x + 1,d = y + 1;
}
else if(x == 0 && y != 0)
{
a = x,b = y - 1,c = x + 1,d = y + 1;
}
else if(x == 0 && y == 7)
{
a = x,b = y - 1,c = x + 1,d = y;
}
else if(x != 0 && y == 7)
{
a= x - 1,b = y -1,c = x + 1,d = y;
}
else if(x == 7 && y == 7)
{
a = x - 1,b = y - 1,c = x,d = y;
}
else if(x == 7 && y != 7)
{
a = x - 1,b = y - 1,c = x,d = y + 1;
}
else if(x == 7 && y == 0)
{
a = x - 1,b = y,c = x,d = y + 1;
}
else if(x != 7 && y == 0)
{
a = x - 1,b = y,c = x + 1,d = y + 1;
}
else
{
a = x - 1,b = y - 1,c = x + 1,d = y + 1;
}
for(m = a; m <= c; m++)
for(n = b; n <= d; n++)
{
if(m == x && n == y)
{
continue;
}
cout<<array[m][n]<<" ";
sum = sum +array[m][n];
}
cout<<endl<<"sum="<<sum<<endl;
}
void change(int a[8][8])
{
int i,j;
for(i = 1; i <= 7; i++)
for(j = 0; j <= 7; j++)
{
if(j == 7)
a[i][j] = a[i - 1][j] + a[i - 1][0];
else
a[i][j] = a[i - 1][j] + a[i - 1][j + 1];
}
}
经验积累:
三点失败版......还望阅读的朋友们不要当场凌乱,然后再很无奈的挤出一声:“你赢了...”........
其实刚输入中间的数成功以后我还是挺兴奋的....
上机感言:就像贺老师说的...这程序可以用更短的来处理...所以