hello
our doctor give as in assignment , i already try to solve it but i got some errors...
==========
Write a function named compareArrays that takes 4 parameters, two arrays of any type
(arrA[] and arrB[]) , and two integers sizeA and sizeB, being the number of elements in the two arrays respectively. The function should return boolean value which is true if the arrays are of the same size and all the corresponding elements in both arrays arrA and arrB are equal. Otherwise, it will return false. (i.e. return false if sizeA is different from sizeB or if any element of arrA is different from the corresponding element of arrB). Test the functions by writing a C++ program.
==========
PHP Code:
#include <iostream>
template <class Type>
bool compareArrays(Type *arrA, Type *arrB);
using namespace std;
template <class Type>
bool compareArrays(Type arrA,Type arrB, int sizeA, int sizeB){
bool flag=true;
// arrayA
cout<<"Enter the number of element in arrayA:";
cin>>sizeA;
arrA= new Type[sizeA];
cout<<"Enter the elements of arrayA:";
for ( int i=0; i<sizeA; i++)
cin>>arrA[i];
// arrayB
cout<<"Enter the number of element in arrayB:";
cin>>sizeB;
arrB= new Type[sizeB];
cout<<"Enter the elements of arrayB:";
for ( int j=0; j<sizeB; j++)
cin>>arrB[j];
// compareArrays checks equivalent
while(flag){
if (sizeA!=sizeB)
{ flag= false;
break;
}
else{
for (int q=0; q<sizeA; q++)
if(arrA[q]!=arrB[q]){
flag=false;
break;}
return flag;
}
}
}
int main(){
int sA, sB;
compareArrays<int>(a ,b, sA, sB);
return 0;
}
the errors
Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib Assigment7
Your code is wrong on many many levels. As already mentioned, you have project configuration problems, but also algorithm problems, and finally, just downright logic problems.
Forget input from user, and do things 1 at a time. Start by writing your compare function only.
Here is a "fill in the gap" file:
Code:
#include <iostream>
template <typename T>
bool compareArrays(T arrA, int sizeA, T arrB, int sizeB)
{
//please fill me
}
int main()
{
//fancy output
std::cout.setf(std::ios_base::boolalpha);
//first test //false
{
int A[] = {1, 2, 3, 4};
int ASize = 4;
int B[] = {4, 3, 2};
int BSize = 3;
bool result = compareArrays(A, ASize, B, BSize);
std::cout << "Test1 result: " << result << std::endl;
}
//second test //false
{
int A[] = {1, 2, 3, 4};
int ASize = 4;
int B[] = {1, 2, 3, 5};
int BSize = 4;
bool result = compareArrays(A, ASize, B, BSize);
std::cout << "Test2 result: " << result << std::endl;
}
//third test //true
{
int A[] = {1, 2, 3, 4};
int ASize = 4;
int B[] = {1, 2, 3, 4};
int BSize = 4;
bool result = compareArrays(A, ASize, B, BSize);
std::cout << "Test3 result: " << result << std::endl;
}
//fourth test //false
{
int A[] = {1, 2, 3, 4};
int ASize = 4;
int B[] = {1, 2, 3, 4, 5};
int BSize = 5;
bool result = compareArrays(A, ASize, B, BSize);
std::cout << "Test4 result: " << result << std::endl;
}
return 0;
}
The correct answer should be "false, false, true, false". Get that to work, it is the first step.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Bookmarks