I generate the array for you --- you need to do the homework yourself.
[CODE]/**
I did not implement the following requirement 1.
What I did is to generate the scores' array.
1、首先将每个分数段内的最高分组织在另外一个数组中,
在该数组中用二分法(对半查找)找到待查数据所在的分数段,
然后再到响应的段中去顺序查找。注意此处的二分法和通常二分法实现细节上的差别。
*/
#include <iostream>
#include <ctime> // for srand()
using namespace std;
// generate random number between a and b, both inclusive.
int rd(int a, int b) // this is a very crappy random number generator
{
return a + ( rand() | (rand() << 16) ) % (b-a+1);
}
class StuScores
{
public:
// generating the array
StuScores() : size(0)
{
int i;
int j;
int pos=0;
srand(time(0)); // seed random number generator
// generate sizes for each part and calculate the total size
for(i=0; i<5; ++i)
{
partSizes[i] = rd(5, 10);
size += partSizes[i];
}
a= new int[size];
i=0;
// first part has scores 0..59
for(j=0; j<partSizes[i]; ++j)
a[j] = rd(0, 59);
pos = partSizes[0];
// parts #2..#5
for(i=1; i<5; ++i)
{
for(j=0; j<partSizes[i]; ++j)
{
if(i==4) // needs to include the score 100
a[pos+j] = (5+i)*10 + rd(0, 10);
else
a[pos+j] = (5+i)*10 + rd(0, 9);
}
pos+=partSizes[i];
}
}
virtual ~StuScores()
{
delete [] a;
}
// print the scores' array
void display() const
{
int i=0;
int b[4];
int j;
b[0] = partSizes[0]-1;
for(j=1; j<4; ++j)
b[j] = b[j-1]+partSizes[j];
j=0;
while(i < size)
{
cout<<a[i]<< " ";
if (i==b[j] && j<4)
{
cout<<endl;
++j;
}
++i;
}
cout<<endl;
}
private:
int *a; // the scores' array
int size; // total number of students
int partSizes[5]; // stores the size of each part
};
int main(int argc, char** argv)
{
StuScores ss;
ss.display();
return 0;
}[/CODE]