Question

Task3: Given two int arrays, output true if the two arrays are identical and false if they are not. Input: task3 input Output

input:

10 ܕ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ : : 7 s a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

hey there.

please find the below code and output screenshot as you didn't mentioned the code language i've created the program that you asked in 2 main languages 1.Java 2. c++.

in the below code i've created 2 arrays whose values and number of elements value will be taken by user and then corresponding output will be shown in yes/no

if you need any other help regarding this please comment down below or try to connect with me.

C++ Code :


#include <bits/stdc++.h> 
using namespace std; 

bool areArrayEqual(int arr_1[], int arr_2[], int p, int q) 
{ 
        // If lengths of array are not equal means array are not equal 
        
        if (p != q) 
                return false; 

        // sort the arrays and compare
        sort(arr_1, arr_1 + p); 
        sort(arr_2, arr_2 + q); 

        //Compare elements
        for (int i = 0; i < p; i++) 
                if (arr_1[i] != arr_2[i]) 
                        return false; 

        // If all elements were same. 
        return true; 
} 

int main() 
{ 
    int total;
      cin >> total;
     int arr1[total];
     int arr2[total];


    //  store input from user to array
    for (int i = 0; i < total; ++i) {
        cin >> arr1[i];
    } 
      for (int i = 0; i < total; ++i) {
        cin >> arr2[i];
    }
        int n = sizeof(arr1) / sizeof(int); 
        int m = sizeof(arr2) / sizeof(int); 
cout << "Array 1: ";
     for (int i = 0; i < total; ++i) {
     cout   << arr1[i] << " ";
    }
    cout <<   endl<< "Array 2: ";
    for (int i = 0; i < total; ++i) {
       cout  << arr2[i] << " ";
    }
    cout <<   endl;
        if (areArrayEqual(arr1, arr2, n, m)) 
                cout << "True"; 
        else
                cout << "False"; 
        return 0; 
} 

Output :5 1 2 3 4 5 5 2 1 3 4 Run it (F8) Save it Show compiler warnings [ + ] Compiler args [ - ] Hide input Compilation time: 2.86

Java code :


import java.io.*; 
import java.util.*;

 public class Rextester { 
        public static boolean areArrayEqual(int arr_1[], int arr_2[]) 
        { 
                int p = arr_1.length; 
                int q = arr_2.length; 

                // If lengths of array are not equal means array are not equal
        
                if (p != q) 
                        return false; 

                // Sort both arrays 
                Arrays.sort(arr_1); 
                Arrays.sort(arr_2); 

                // compare elements 
                for (int i = 0; i < p; i++) 
                        if (arr_1[i] != arr_2[i]) 
                                return false; 

                // If elements are same. 
                return true; 
        } 

        public static void main(String[] args) 
        { 
    Scanner s = new Scanner(System.in);
     
            int total = s.nextInt();
               int arr1[] = new int[total];
               int arr2[] = new int[total];
     

    for (int i = 0; i < total; i++) {
    arr1[i] = s.nextInt();
    } 
    
      
      for (int i = 0; i < total; i++) {
       arr2[i] = s.nextInt();
    }

System.out.print("\nArray 1: "); 
      for (int i = 0; i < total; i++) {
      System.out.print(arr1[i]); 
    }
      System.out.print("\nArray 2: "); 
   for (int i = 0; i < total; i++) {
      System.out.print(arr2[i]); 
    } 
    
                if (areArrayEqual(arr1, arr2)) 
                        System.out.print("\nTrue"); 
                else
                        System.out.println("\nFalse"); 
        } 
} 

Output :

compiled and executed in 7.519 sec(s) 4 1 2 3 4 4 3 2 1 Array 1: 1234 Array 2: 4321 True

Add a comment
Know the answer?
Add Answer to:
input: Task3: Given two int arrays, output true if the two arrays are identical and false...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...

    public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the elements of the two arrays. The two arrays do not have to be the same length. For example, given a = [1, 2, 3] and b = [4, 5, 6, 7, 8], the method returns c = [1, 4, 2, 5, 3, 6, 7, 8] Parameters: a - given array b - given array Returns: the array which interleaves the elements of the two...

  • Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays...

    Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays using pointers. Input:     4     1 2 3 4     5     4 5 6 7 8     where: First line represents the number of elements in the first array. Second line represents the elements in the first array. Third line represents the number of elements in the second array. Fourth line represents the elements in the second array. Output:     4 5 6 7...

  • /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false...

    /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false duplicateInts({1, 2}) -> false duplicateInts({7, 7}) -> true duplicateInts({1, 2, 3, 4, 5}) -> false duplicateInts({1, 2, 3, 2, 4, 5}) -> true **/ public static boolean duplicateInts(int[] numbers) { //your code here return false; }//end duplicateInts /** Given a String array, return true if the array contains duplicate values. Note: Capital letters count duplicateStrings({"a"}) -> false duplicateStrings({"a", "b", "c", "d"}) -> false duplicateStrings({"a",...

  • C++ Arrays & Methods

     #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the same order. Otherwise, it...

  • Arrays: Middle item Given a set of data, output the middle item (if even number of items, output the two middle items). Ex: If the input is 5 7 9 11 13 -1 (a negative indicates end), the output is: 9...

    Arrays: Middle item Given a set of data, output the middle item (if even number of items, output the two middle items). Ex: If the input is 5 7 9 11 13 -1 (a negative indicates end), the output is: 9 Ex: If the input is 5 7 9 11 -1, the output is: 7 9 The maximum number of inputs for any test case will not exceed 5. If the input exceeds this maximum, output "Too many inputs". Hint:...

  • 1. Write a program that reads in two arrays (a1 and a2) of numbers and creates...

    1. Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array. Example input/output #1: Enter the length...

  • In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays...

    In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional arrays of integer and returns true (boolean value) if the arrays contain...

  • C++ Single Dimensional Arrays

    Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.  The program...

  • 6. From the following flow-chart, write the java code. false a<100 print Done true print Bad...

    6. From the following flow-chart, write the java code. false a<100 print Done true print Bad a++ 7. Given an array int[] myarray = {2, 4, 6, 8, 9, 7, 5, 3, 1, 0} Create a trace table for variables involved in the following code int s = 10; for (int i = 3; i <7; i++) S = S + myarray[i]; What is the final value of s? 8. Given an ArrayList object mywords of the content Avengers Endgame...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT