| | |
compare
![]() |
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
Views: 3869 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes client code command compile compiler console constructor conversion convert count delete dll dynamic encryption error file files filestream fstream function functions game givemetehcodez graph gui homework http iamthwee ifstream input int lazy linker list loop loops math matrix member memory multidimensional newbie number object objects opengl operator output parameter pointer pointers problem program programming project qt random read recursion recursive reference server simple sort spoonfeeding string strings struct student studio system template templates text time tree variable vc++ vector video visual visualstudio void win32 window windows winsock wordfrequency





