Below is my crazy program.What i want it to do is that it should take two arrays from the user;the 1st array should be the memo & the 2nd should be answers.These two arrays should be passed to function compare of the type bool.In compare the 2 arrays should be compared and the results be stored in another array using bool.This means that the new array in function compare should consist of zeros and ones:zero-wrong & one-correct.How do i copy the results int the ResultArray?

#include<iostream>

using namespace std;

bool Compare(int[],int[],int);


int main()
{
	const int arraySize = 5;
	int CorrectArray[arraySize];
	int PupilArray[arraySize];

	
	cout<<"Enter the memorandum : ";

	for(int i = 0;i<arraySize; i++)
	{
		cin>>CorrectArray[i];

	}
	
	cout<<endl;

	cout<<"Pupil's answers : ";

	for(int j = 0; j<arraySize; j++)
	{
		cin>>PupilArray[j];
	}

	cout<<Compare(CorrectArray,PupilArray,arraySize);

	return 0;

}

bool Compare(int correct[],int pupil[],int size)
{
	
	bool ResultArray[5] = 0;
	for ( int i = 0; i < size; i++ ) {
		for(int j = 0; j<size;j++)
			if ( correct[i] != pupil[i] ){
      
	  i=false;
	ResultArray[j]=ResultArray[i];
			}

	else
	{
		i=true;

		ResultArray[i]=ResultArray[i];
	}
  }

  return ResultArray[];

}

Dani AI

Generated

A few practical options that avoid the pitfalls in the original post and the early replies.

and have the right procedural idea (fill a results array), and correctly flagged the missing size in acceptInput. If the goal is simplicity and safety, prefer standard containers: std::vector or std::array let you return a results container by value and avoid raw pointer/index mistakes. For a concise, idiomatic one-liner you can use std::transform to produce the per-question booleans:

#include <vector>
#include <algorithm>
#include <functional>

std::vector<bool> compare(const std::vector<int>& correct,
                          const std::vector<int>& pupil)
{
    if (correct.size() != pupil.size()) throw std::invalid_argument("size mismatch");
    std::vector<bool> result(correct.size());
    std::transform(correct.begin(), correct.end(), pupil.begin(),
                   result.begin(), std::equal_to<int>());
    return result;
}

Notes and small gotchas:

These small changes make the code safer, easier to test, and simpler to extend (for example, to compute score totals or generate per-item feedback).

Recommended Answers

All 4 Replies

1) You can not return arrays
2) Way you initiliase array was wrong
3) Pass all three arrays
4) You need only single for loop inside compare function.
5) Compare don't need to return anything. Address of array is passed so changes will be seen back.

You should something like this

#include<iostream>
using namespace std;

void Compare(int[],int[],bool [],int);

int main()
{
    const int arraySize = 5;
    int CorrectArray[arraySize];
    int PupilArray[arraySize];
    bool ResultArray[arraySize] = {false};
    
    cout<<"Enter the memorandum : ";

    for(int i = 0;i<arraySize; i++)
    {
        cin>>CorrectArray[i];
    }
    
    cout<<endl;
    cout<<"Pupil's answers : ";

    for(int j = 0; j<arraySize; j++)
    {
        cin>>PupilArray[j];
    }

    Compare(CorrectArray,PupilArray, ResultArray, arraySize);
    
    for(int j = 0; j<arraySize; j++)
    {
        cout<<ResultArray[j];
    }
    return 0;
}


void Compare(int correct[],int pupil[],bool Result[],int size)
{
    
    for ( int i = 0; i < size; i++ )
        if ( correct[i]== pupil[i] ){
                Result[i]=true;
            }
  
}
#include<iostream>
using namespace std;

void Compare(int[],int[],bool [],int);
void acceptInput (int [], int []);
void displayResult (bool []);

int main()
{
    const int arraySize = 5;
    int CorrectArray[arraySize];
    int PupilArray[arraySize];
    bool ResultArray[arraySize] = {false};
    acceptInput (CorrectArray, PupilArray);
    Compare(CorrectArray,PupilArray, ResultArray, arraySize);
    displayResult (ResultArray);
    return 0;
}

void Compare(int correct[],int pupil[],bool Result[],int size)
{
    
    for ( int i = 0; i < size; i++ )
        if ( correct[i]== pupil[i] ){
                Result[i]=true;
            }
  
}

void acceptInput (int CorrectArray[], int PupilArray[])
{
    cout<<"Enter the memorandum : ";
    for(int i = 0;i<arraySize; i++)
    {
        cin>>CorrectArray[i];
    }
    cout<<endl;
    cout<<"Pupil's answers : ";
    for(int j = 0; j<arraySize; j++)
    {
        cin>>PupilArray[j];
    }
}

void displayResult (bool ResultArray[])
{ 
  for (int i = 0; i < arraySize; ++i)
  {
     if (ResultArray [i] == true)
     { 
        cout <<"\nThe answer " << i + 1 << "is correct.";
     }
     else
     {
        cout <<"\nThe answer " << i + 1 << "is incorrect.";
     }
  }
}

Or in a more fuction oriented way.

Your acceptInput() code doesn't have access to arraySize. :)

Hi all,
There is a function in string.h library called

strcmp(str1, str2);

which can be used to compare two strings against each other.
It returns 0 if two strings are equal and return non-zero if they're different.

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.