Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Leda > Re: comparing 2...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 156 of 212
Post > Topic >>

Re: comparing 2 arrys.

by iandemps@[EMAIL PROTECTED] (demps) Sep 3, 2004 at 08:28 AM

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
 




 2 Posts in Topic:
comparing 2 arrys.
rzza@[EMAIL PROTECTED] (  2004-09-02 01:29:45 
Re: comparing 2 arrys.
iandemps@[EMAIL PROTECTED  2004-09-03 08:28:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:52:35 CDT 2008.