rzza@[EMAIL PROTECTED]
(Lemmy) wrote in message
news:<56636c35.0409020029.ca5cea@[EMAIL PROTECTED]
>...
> this is my code and i want to compare pupilarray[] and
> correctarray[].Please help me.
>
> #include<iostream>
> #include<iomanip>
>
> using namespace std;
> using std::setw;
>
> void compare(int[], int[]);
>
>
>
> int main(){
>
> const int correctarraysize=5;
> const int pupilarraysize=5;
> int resultarraysize=5;
>
> int pupilarray[pupilarraysize ];
> int correctarray[correctarraysize]={1,2,3,4,1};
> bool resultarray=0;
>
> cout<<"\n\nEnter pupil's results:"<<endl;
> for(int k=0;k<pupilarraysize;k++)
> cin>>pupilarray[k];
>
>
cout<<"*************************\a**************************\a**********"<<endl;
>
> cout<<"\nPupil's choice:"<<setw(26)<<"Answare:"
> <<setw(20)<<"Results are"<<endl;
>
> for(int j=0;j<pupilarraysize;j++)
>
> cout<<setw(14)<<pupilarray[j]<<setw(26)<<correctarray[j]
> <<setw(20)<<resultarray<<endl;
>
cout<<"*************************\a**************************\a***********"<<endl;
> return 0;
> }
>
> void compare(int c[], int p[]){
> bool resultarray; // <-- start out with true
> for(int i=1;i<25;i++) {
> if( c[i] == p[i])
> resultarray = true;
> else
> resultarray = false; // <-- if you notice I took out the
> 'bool' you should not declare this variable twice.
> }
>
> }
What is it you are trying to do? As it is you are going outside the
bounds of the arrays as their size is 5 and you are loopping up to 25.
If you are trying to compare each element in one array to all the
elements in the second you will need a nested for loop, for example:
for (int i = 0; i < 5; i++) //use i = 0 because arrays start with a
zero base
for (int j = 0; j < 5; j++)
if (c[i] == p[j]) //rest of code...
now in your code:
> if( c[i] == p[i])
> resultarray = true;
> else
> resultarray = false;
you may also have a problem as result array will only be set to the
value of the last comparison. If you want you could use an int for
resultarray and increment it each time a comparison is true, if
resultarray is less than 5 (the size of the array) then you know one
or more of the comparisons is false. As it is compare() doesn't
actually return anything or print anything to the screen so I'm not
exactly sure what you are seeking it to do...
Hope this helps,
Ian


|