下面是引用和煦微风于2005-12-8 21:16发表的 :
同感+1....
程式就是要自己写过才会有经验...
刚开始一定都会很辛苦的....
像我们每次拿到作业...
就开始要构思到底要怎么写...
.......
第一题,未完善
//Question1
#include <iostream.h>
int countSize(const float scores[]){ //to count how many soures what he/she input function
for (int i = 0; i < 10; i ++){
if (scores
== 0)
break;
}
return i;
}
void getExtremes(float& min, float& max, const float scores[], int actualSize){ //get the maximum and the minimum scores
min = scores[0];
max = scores[0]; //set the maximum and the minimum scoures to the frist input
for (int i = 0; i < actualSize; i ++){
if (scores > max) //get the maximum scoure
max = scores;
else {
if (scores < min) //get the minimum scoure
min = scores; }
}
}
float firstMin(const float scores[], int n){ //find the first minimum score function
float min = scores[0];
for (int i = 0; i < n; i ++){
if (scores < min) {
min = scores; }
}
return min;
}
float secondMax(const float scores[], int actualSize){ //find the secound maximum score function
float max, SecondMax;
max = SecondMax;
SecondMax = scores[0];
for (int i = 0; i < actualSize; i ++){
if (scores > max){
SecondMax = max;
max = scores; }
}
return SecondMax;
}
bool find(const float scores[], int n, float particular){ //find the terget score function
for (int i = 0; i < n; i ++){
if (scores == particular)
return 1;
}
return 0;
}
void shift(float scores[], int n){ //shift the score to cover the space function
for (int i = n; i <= 9; i ++){
scores = scores[i+1];
}
scores[9] = 0;
}
bool remove(float scores[], int n, float target){ //remove the terget score function
for (int i = 0; i < n; i ++){
if (scores == target)
shift(scores, i);
}
return 0;
}
void display(const float scores[]){ //display the array of scores fuction
for (int i = 0; i < 10; i ++)
cout << " {" << scores << "}";
cout << endl;
}
int main(){
float a[10];
int i=0;
int n;
float min, max, particular;
int choice;
do {
cout <<"Please Enter The Students' Scores : "; //input the students' scores
cin >> a; //get the value
}
while (a[i++]!=0 && i < 10) ;
cout <<"1. Count the number of non-zero elements (which means the actual size of the array); "<<endl; //the menu
cout <<"2. Find the maximum and minimum scores in the array through its reference parameters;"<<endl;
cout <<"3. Find the minimum element among the first n elements (you should ask the user to specify the value for n first, and n should be less than or equal to the actual size of the array);"<<endl;
cout <<"4. Find the second largest score in the array;"<<endl;
cout <<"5. Check whether a particular score is found within the first n element in the array;"<<endl;
cout <<"6. Remove a targeted element from the first n elements in the array;"<<endl;
cout <<"7. Print the array of scores."<<endl;
cin >> choice;
switch (choice){
case 1:
cout <<"The number of non-zero elements: "<< countSize(a)<<endl; //count the elements
cout <<"Please Enter The Students' Scores : "; //input the students' scores
cin >> a; //get the value
break;
case 2:
getExtremes(min,max,a,countSize(a)); //link the function
cout <<"The maximum score in the array is: "<< max<<endl; //output the maximum
cout <<"The minimum score in the array is: "<< min<<endl; //output the minimum
break;
case 3:
cout <<"Please enter the n what n elements that you want to be counted: "<<endl; //set the n elements to be counted
cin >> n; //get the value
if ((n<=10)&&(n>0)) { //make sure 0<n<=10
cout << "The minimum among the first "<<n<<" elemnts is: "<<firstMin(a, n)<<endl;} //output the answer
else
cout <<"Please Enter an Possible Number!!"<<endl; //not allow input incorrect value
break;
case 4:
cout <<"The second largest score in the array is: "<<secondMax(a, countSize(a))<<endl; //output the answer
break;
case 5:
cout <<"Please enter the n what n elements that you want to be counted: "<<endl; //set the n elements to be counted
cin >> n;
if ((n<=10)&&(n>0)) { //make sure 0<n<=10
cout << "Which particular score do you want to find? "; //ask to set the particular score
cin >>particular; //get the value
if (find(a, n, particular)) //if the function is true
{
cout <<"Yes, I can find it. It is "<<particular<<endl; //to answer user we can find the value
}
else {
cout <<"No, I couldn't find the particular scorce."<<endl; //to answer user we can't find it
}
}
else
cout <<"Please Enter an Possible Number!!"<<endl; //not allow to input a incorrect value
break;
case 6:
break;
case 7:
cout<<"The array of scores is : ";
break;
default:
cout <<"Please Enter an Possible Number!!"<<endl;
display(a);
break;
}
return 0;
}