注册 登录
编程论坛 C++教室

请教!如何分别读取二维数组的各个元素

allenfan 发布于 2007-05-29 22:32, 880 次点击

本人在完成一个模拟抽奖游戏,由用户输入6个数字的数组最为兑奖号码(list1[6]),然后程序提示用户买几组号码,用户输入数字后,比如3,程序提示用户分别输入3组号码中的各个元素(e.g.我用一个二维数组来储存list2[3][6]),然后分别比较每组数字的中奖个数,比如第一组中了3个数字,第二组中了4个数字,第五组中了2个数字,最后输出的每组中奖数字的个数是:4 3 2(由大到小的顺序)。小弟困惑于如何分别读取二维数组中的各组元素(e.g,比如先读入list2[0][6],比较之后,再读入list2[1][6].....)

这是我的原程序请高手指点:
#include <iostream>
using namespace std;

const int MAXSIZE=6;
struct arrayList
{
int value[MAXSIZE]; //struct value
int length; //struct length
};

void read(int list1[],int &size);
void read1(int list2[][MAXSIZE],int &num);
void analyze(int list1[],int list2[][MAXSIZE],arrayList list3,int &count,int &num);

int main()
{
arrayList list3;
int num,count;
int list1[MAXSIZE];

int size=MAXSIZE;


cout<<"Please enter the six numbers of the Draw: "<<endl;
read(list1,size);

cout<<"Please enter the number of panels you played:"<<endl;
cin>>num;
int list2[num][MAXSIZE];
read1(list2,num);


analyze(list1,list2,list3,count,num);


system("pause");
return 0;

}

void read(int list1[],int &size)
{
for(int i=0;i<MAXSIZE;i++)
{
cin>>list1[i];
}
}

void read1(int list2[][MAXSIZE],int &num)
{

for (int i=0;i<num;i++)
{
cout<<"Please enter the six numbers of panel["<<i+1<<"]:"<<endl;
for(int j=0;j<MAXSIZE;j++)
{
cin>>list2[i][j];
}
}
}

void analyze( int list1[],int list2[][MAXSIZE],arrayList list3,int &count,int &num)
{

count=0;
list3.length=0;
for(int k=0;k<MAXSIZE;k++)
for(int i=0;i<num;i++)
{
for(int j=0;j<MAXSIZE;j++)
{
if(list2[i][j]==list1[k])
{
count++;
}
}
}
cout<<"Your Lotto results are:"<<endl;
cout<<count<<endl;
}

2 回复
#2
allenfan2007-05-30 05:23
回复:(allenfan)请教!如何分别读取二维数组的各个...
plz help!! thanks !!!
#3
aipb20072007-05-30 11:45
数组,不管几维,在编译时确定每一维的size,即为常量。

所以你不能int list2[num][MAXSIZE];读取num作为数组的size。

要这样做,就用动态数组,或者其他容器!
1