| | |
compare
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 ;
}
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 ;
}
Here is your problem. You are assigning p[i] to c[i]; basically copying the values from one array 'p' into array 'c'.
What you really want to be doing is the '==' equals to operator. This would make the code be like this:
Hope this helps!
Ed
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
if (c[i] == p[i])
Hope this helps!
Ed
•
•
•
•
In a world without walls or fences,
What use are Windows and Gates.
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.
A little cleaner, and minus another bug in your code:
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.
C++ Syntax (Toggle Plain Text)
bool compare(int c[], int p[]){ bool resultarray=true; // <-- start out with true for(int i=1;i<25;i++) { if( c[i] == p[i]) // do nothing.. we still have condition resultarray == true else resultarray=false; // <-- if you notice I took out the 'bool' you should not declare this variable twice. } return resultarray ; }
A little cleaner, and minus another bug in your code:
C++ Syntax (Toggle Plain Text)
bool compare(int length, int c[], int p[]){ // NOTE: strings start at 0 not 1 for(int i=0; i < length; i++) { // start at index 0 if( c[i] != p[i]) return false; } return true; }
•
•
•
•
In a world without walls or fences,
What use are Windows and Gates.
--------------------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
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
![]() |
Similar Threads
- string compare and fstream (C)
- File Compare (C++)
- Please help me compare strings (Java)
- String search in a file compare? (Computer Science)
- Compare strings... (C++)
Other Threads in the C++ Forum
- Previous Thread: probelm with <fstream>; saving characters
- Next Thread: Socket
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class code coding compile console conversion count data database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





