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= p)
bool resultarray=true;
else
bool    resultarray=false;
return resultarray ;
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Here is your problem. You are assigning p to c; basically copying the values from one array 'p' into array 'c'.

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:

if (c[i] == p[i])

Hope this helps!


Ed

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.

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:

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

--------------------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

This article has been dead for over six months. Start a new discussion instead.