In: Computer Science
3. Array Operations
3-1
Write a function, equalsArray that when passed two int arrays of the same length that is greater than 0 will return true if every number in the first array is equal to the number at the same index in the second array. If the length of the arrays is less than 1 the function must return false. For example, comparing the two arrays, {1,2,3,4,5} and {1,2,3,4,5} would return true but the two arrays {3,7} and {3,6} would return false.
You should start by copying the function-1-1.cpp file and name it function-3-1.cpp. Then add the function equalsArray to the new file.
The main function for this problem must call your readNumbers function twice, then pass both new arrays to your equalsArray function, display the result as true or false and finally delete the array. The main function in the file main-3-1.cpp.
The signature for your new function is:
bool equalsArray(int *numbers1,int *numbers2,int length) ;
3-2
Write a function, reverseArray that when passed an int array of length greater than 0 will return a dynamically allocated array of the same length but with the elements in the reverse order. For example, if passed the array, {1,2,3,4,5,6,7,8,9,0} the function would return the array {0,9,8,7,6,5,4,3,2,1}.
You should start by copying the function-3-1.cpp file and name it function-3-2.cpp. Then add the function reverseArray to the new file.
The main function for this problem must call your readNumbers function, then pass the new array to your reverseArray function, then pass the first array and the array returned by reverseArray to equalsArray, display the result as true or false and finally delete both arrays. The main function in the file main-3-2.cpp.
The signature for your new function is:
int *reverseArray(int *numbers1,int length) ;
Solution 3.1 :
C++ Code :
#include<iostream>
using namespace std;
bool equalsArray(int *numbers1,int *numbers2,int length)
// Function for checking whether
both arrays are equal or not
{
bool res = true; //
this will store the result after checking the equality of both the
arrays
if(length<1)
// if length is less than 1
res = false;
// then storing false in res as given
else
// if length is greater than or equals to 1
{
for(int
i=0;i<length;i++)
// iterating for all the
elements both the arrays
{
if(numbers1[i]!=numbers2[i])
// if elements are not equal
{
res = false;
// then res = false
break;
// and break , means there is
no need for further checking
}
}
}
return res;
// returning the result value after checking the
equality of both the arrays
}
void readNumbers(int *arr,int length)
// this function is for
reading the array elements
{
for(int i=0;i<length;i++)
// reading length elements
are size is given
{
int x;
cin>>x;
// reading each element for
array
arr[i] = x;
// and storing the element
value in the array
}
}
int main()
{
int length;
// it will store the length value of both the
arrays
cout<<"Enter the length value : ";
// asking for the input of
length value of both the arrays
cin>>length;
// taking input of length of both the
arrays
if(length != 0)
// if length is greater than
0 , then we'll ask for input of the elements of the array1
cout<<"\nEnter "<<length<<" values
for array 1 : ";
int *arr1 = new int[length];
// creating an array1 of size
length dynamically
readNumbers(arr1,length);
// calling readNumbers() for
reading the elements of array1
if(length != 0)
// if length is greater than 0 , then we'll ask
for input of the elements of the array2
cout<<"\nEnter "<<length<<" values
for array 2 : ";
int *arr2 = new int[length];
// creating an array2 of size
length dynamically
readNumbers(arr2,length);
// calling readNumbers() for
reading the elements of array1
bool result =
equalsArray(arr1,arr2,length);
// calling equalsArray() for checking the
equality of both the arrays and storing the returned value as our
required result
delete(arr1);
// deleting the dynamically created array1
delete(arr2);
// deleting the dynamically created array2
cout<<boolalpha<<"\nResult =
"<<result<<"\n";
// printing the result value ( boolalpha is used
for printing result as true/false instead of 0/1 )
}
Output :