w3resource

C++ Exercises: Check two given arrays of integers of length 1 or more and return true if they have the same first element or they have the same last element

C++ Basic Algorithm: Exercise-83 with Solution

Write a C++ program to check two given arrays of integers of length 1 or more and return true if they have the same first element or they have the same last element.

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

bool test(int a1[], int arr_length1, int a2[], int arr_length2)
          {
           return a1[0] == a2[0] || a1[arr_length1 - 1] == a2[arr_length2 - 1];
          } 
          
int main() 
 {  
  int arr_length1, arr_length2;	
  int nums11[] = {10, 20, 40, 50};	
  int nums12[] = {10, 20, 40, 50};	
  arr_length1 = sizeof(nums11) / sizeof(nums11[0]);
  arr_length2 = sizeof(nums12) / sizeof(nums12[0]);
  cout << test(nums11, arr_length1, nums12, arr_length2) << endl; 
  int nums21[] = {10, 20, 40, 50};	
  int nums22[] = {10, 20, 40, 5};	
  arr_length1 = sizeof(nums21) / sizeof(nums21[0]);
  arr_length2 = sizeof(nums22) / sizeof(nums22[0]);
  cout << test(nums11, arr_length1, nums12, arr_length2) << endl; 
  int nums31[] = {10, 20, 40, 50};	
  int nums32[] = {1, 20, 40, 5};	
  arr_length1 = sizeof(nums31) / sizeof(nums21[0]);
  arr_length2 = sizeof(nums32) / sizeof(nums22[0]);
  cout << test(nums31, arr_length1, nums32, arr_length2) << endl;
  return 0;    
}

Sample Output:

1
1
0

Pictorial Presentation:

C++ Basic Algorithm Exercises: Check two given arrays of integers of length 1 or more and return true if they have the same first element or they have the same last element.

Flowchart:

Flowchart: Check two given arrays of integers of length 1 or more and return true if they have the same first element or they have the same last element.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to check a given array of integers of length 1 or more and return true if the first element and the last element are equal in the given array.
Next: Write a C++ program to compute the sum of the elements of a given array of integers.

What is the difficulty level of this exercise?



Share this Tutorial / Exercise on : Facebook and Twitter

C++ Programming: Tips of the Day

How do I execute a command and get the output of the command within C++ using POSIX?

#include<cstdio>
#include<iostream>
#include<memory>
#include<stdexcept>
#include<string>
#include<array>

std::string exec(constchar* cmd){
    std::array>char, 128> buffer;
    std::string result;
std::unique_ptr<FILE, decltype(&pclose)>pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
    }
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
return result;
}
Pre-C++11 version:
#include<iostream>
#include<stdexcept>
#include<stdio.h>
#include<string>

std::string exec(constchar* cmd){
char buffer[128];
    std::string result = "";
    FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
            result += buffer;
        }
    } catch (...) {
pclose(pipe);
throw;
    }
pclose(pipe);
return result;
} 

Replace popen and pclose with _popen and _pclose for Windows.

Ref : https://bit.ly/3HODowH