compare

Reply

Join Date: Jul 2004
Posts: 10
Reputation: RITZY_G is an unknown quantity at this point 
Solved Threads: 0
RITZY_G's Avatar
RITZY_G RITZY_G is offline Offline
Newbie Poster

compare

 
0
  #1
Sep 1st, 2004
just need a little help wit comparing two int arrays and return the value as bool.

help:


#include<iostream>
#include<iomanip>

using namespace std;
using std::setw;

bool 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;
}

bool compare(int c[], int p[]){
bool resultarray=0;
for(int i=1;i<25;i++)
if( c[i]= p[i])
bool resultarray=true;
else
bool resultarray=false;
return resultarray ;
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 10
Reputation: RITZY_G is an unknown quantity at this point 
Solved Threads: 0
RITZY_G's Avatar
RITZY_G RITZY_G is offline Offline
Newbie Poster

Re: compare

 
0
  #2
Sep 1st, 2004
any way i can compare the two? ^shit aint workin'
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: compare

 
0
  #3
Sep 1st, 2004
Here is your problem. You are assigning p[i] to c[i]; basically copying the values from one array 'p' into array 'c'.
  1. if( c[i]= p[i])


What you really want to be doing is the '==' equals to operator. This would make the code be like this:
  1. if (c[i] == p[i])

Hope this helps!


Ed
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: compare

 
0
  #4
Sep 1st, 2004
Also I didn't notice this at first:

What you really want is so that if you check every item and it equals '==', then you return true. If you ever see a mismatch, you dump a false.

  1.  
  2. bool compare(int c[], int p[]){
  3. bool resultarray=true; // <-- start out with true
  4. for(int i=1;i<25;i++) {
  5. if( c[i] == p[i])
  6. // do nothing.. we still have condition resultarray == true
  7. else
  8. resultarray=false; // <-- if you notice I took out the 'bool' you should not declare this variable twice.
  9. }
  10. return resultarray ;
  11. }


A little cleaner, and minus another bug in your code:
  1. bool compare(int length, int c[], int p[]){
  2.  
  3. // NOTE: strings start at 0 not 1
  4. for(int i=0; i < length; i++) { // start at index 0
  5. if( c[i] != p[i])
  6. return false;
  7. }
  8.  
  9. return true;
  10. }
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 10
Reputation: RITZY_G is an unknown quantity at this point 
Solved Threads: 0
RITZY_G's Avatar
RITZY_G RITZY_G is offline Offline
Newbie Poster

Re: compare

 
0
  #5
Sep 1st, 2004
--------------------Configuration: HIT IT - Win32 Debug--------------------
Compiling...
HIT IT.CPP
C:\Documents and Settings\200402622\Desktop\HIT IT.CPP(43) : error C2181: illegal else without matching if
Error executing cl.exe.

HIT IT.OBJ - 1 error(s), 0 warning(s)


i get that
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 10
Reputation: RITZY_G is an unknown quantity at this point 
Solved Threads: 0
RITZY_G's Avatar
RITZY_G RITZY_G is offline Offline
Newbie Poster

Re: compare

 
0
  #6
Sep 1st, 2004
the other one returns no value to the resultarray
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 3869 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC