Question

In: Computer Science

3. Array Operations 3-1 Write a function, equalsArray that when passed two int arrays of the...

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

Solutions

Expert Solution

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 :


Related Solutions

In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function, hexDigits that when passed an int array of any length greater than 0...
Write a function, hexDigits that when passed an int array of any length greater than 0 will print the corresponding hex digit for each int, one per line. The corresponding hex digit should be determined using a switch statement. The hex digits must be printed in uppercase and in order starting with the first entry. Each line of output should start with the array index of the number being written out, followed by a space, then the number, then another...
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[],...
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[], int size); 2. Write a function to generate an array of random integers. void random_array (int arr[], int size, int range); The scale of numbers should be 1 to range. 3. Write a function to find the sum of a sequence of number. int sum (int arr[], int size); 4. Write a function rotate(arr[], d, n) that rotates arr[] of size n by d...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ with explanations
3. Write a program which divides a given array (int array []) into two parts: -...
3. Write a program which divides a given array (int array []) into two parts: - Left part with all elements with values <= (less than or equal) pivot (int pivot) and the - Right part with all elements with values > (larger than) pivot. You should ask the user to enter the values of: int pivot, int size and the array
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT