Question

3. How Will You Compare? Write a Comparator class with the following 3 overloaded compare methods: 1. boolean compare(int a,Input Format for Custom Testing Input from stdin will be processed as follows and passed to the function. The first line contSample Output 0 Same Different Same Explanation o There are 3 test cases: conditie #t ь Output Explanation Test Case compariا ابا ب 11 با لیا | ایا11ابا ابا ااا ااااااااT1T1الباي بالا با {1,2,3} {1,2,3) Same element a[i] = b[i] Sample Case 1 Sampl

in C++ and comments please for better understanding

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

Here is the answer for your question in C++ Programming Language.

CODE :

#include <iostream>

#include<string>

using namespace std;

//Writing method declarations

bool compare(int, int);

bool compare(string, string);

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

int main()

{

    //Creating variable to read number of test case

    //i is taken to loop from 0 to T

    //choice is to read choice, a and b are integers

    // m and n are length of arrays, arr1 and arr2 are arrays

    int i = 0,T,choice,a,b,m,n;

    int arr1[10],arr2[10];

    string str1,str2;

    bool result1,result2,result3;

    cin >> T; //Reading T

    while(i<T) //loop to iterate through the no.of test cases

    {

        cin >> choice; //reading choice

        //switching to the case

        switch(choice)

        {

            //To read strings and call compare method and display result

            case 1 :cin >> str1;

                    cin >> str2;

                    result1 = compare(str1,str2);

                    if(result1 == 1)

                    {

                        cout << "Same" << endl;

                    }

                    else

                    {

                         cout << "Different" << endl;

                    }

                    break;

            //To read integers and compare them and display result

            case 2 :cin >> a ;

                    cin >> b ;

                    result2 = compare(a,b);

                    if(result2 == 1)

                    {

                        cout << "Same" << endl;

                    }

                    else

                    {

                         cout << "Different" << endl;

                    }

                    break;

            //To read length and value of array and display the result after comparision

            case 3 :cin >> m ;

                    cin >> n ;

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

                  for(int k =0;k<n;k++){ cin >> arr2[k];}

                    result3 = compare(arr1,arr2,m,n);

                    if(result3 == 1)

                    {

                        cout << "Same" << endl;

                    }

                    else

                    {

                         cout << "Different" << endl;

                    }

                    break;

            default : cout << "Invalid Option " << endl;

                    break;

        }

        i++;

    }

       

    return 0;

}

//Comparing integer numbers

bool compare(int a,int b)

{

    if(a == b)

    return true;

    else

    return false;

}

//Comparing strings using compare()

bool compare(string str1,string str2)

{

    if(str1.compare(str2) == 0)

    return true;

    else

    return false;

}

//Comparing two arrays

bool compare(int arr1[],int arr2[],int m,int n)

{

    int count = 0;

    if( m == n ) // if their length is same checks each value in array

    {

        for(int i =0;i<m;i++)

        {

            if(arr1[i] == arr2[i])

            count++;   

        }

        if(count == m)

        return true;

        else

        return false;

    }

    else //if the length is not same returns false

    {

        return false;

    }

}

SCREENSHOTS:

Please see the screenshots of the code below for indentations of the code.

10 1 #include <iostream> 2 #include<string> 3 using namespace std; 4 //writing method declarations 5 bool compare(int, int);//To read integers and compare them and display result case 2 :cin>> a ; cin >> b; result2 = compare(a,b); if(result2 == 1) c67 else 68 return false; 69 } 70 //Comparing strings using compare() 71 bool compare(string str1, string str2) 72 - { 73 if(s

OUTPUT:

3 helloWorld helloworld $g++ -o main *.cpp $main Same Different Same wwwN 1 2 3 1 2 3mm $g++ -o main *.cpp $main Different Same 1 2 3 1 2 3 4 HackerRank HackerRank$g++ -o main *.cpp $main Different Different 34 1 2 3 1 2 3 4 HackerRank hackerRank

Any doubts regarding this can be explained with pleasure:)

Add a comment
Know the answer?
Add Answer to:
in C++ and comments please for better understanding 3. How Will You Compare? Write a Comparator...
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
  • Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two...

    Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two strings while ignoring the case of alphabetical characters. Your program will take two strings as input from the user and use your function to compare them. Assume a maximum C-string size of 1000 characters. Make sure your code works for any input number, not just the test cases. Your code will be tested on other test cases not listed here. Please properly comment your...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • In C please : The tasks in this exam review exercise are structured in levels. You...

    In C please : The tasks in this exam review exercise are structured in levels. You can see the point distribution for the levels at the end. It is strongly recommended that you ensure you have passed all test cases of lower levels before moving up to a higher level. For example, ensure you have completed all Level 0 and 1 tasks before even looking at Level 2 Tasks. MESSAGE ENCODER LEVEL 0: (test cases - 50 points) Start by...

  • Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize...

    Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, create a random integer array of size arraySize (15 OR LESS) with elements from the the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} (integers can appear multiple times in this array, has two accessor methods: public int getArraySize() that will...

  • 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...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #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...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #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...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

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