In: Computer Science
C++
Suppose getAcctNum() returns the account number of a given Account variable. Write a function that returns a boolean value to find if a given account number appears in an array of Account objects.
Explanation:
Here is the function is_present which takes the array of objects named arr, size of the array and also the account number to be searched inside the array.
using a for loop, the account number is searched, it returns true if it is found.
Else false if it is not found till the last element.
Function required:
bool is_present(Account arr[], int size, string
acc_num_to_find)
{
for (int i = 0; i < size; i++) {
if(arr[i].getAcctNum()== acc_num_to_find)
return true;
}
return false;
}
Sample code to check the function:
#include <iostream>
using namespace std;
class Account
{
private:
string accNum;
string name;
double balance;
double interestRate;
public:
Account(string a, string n, double b, double i)
{
accNum = a;
name = a;
balance = b;
interestRate = i;
}
string getAcctNum()
{
return accNum;
}
};
bool is_present(Account arr[], int size, string
acc_num_to_find)
{
for (int i = 0; i < size; i++) {
if(arr[i].getAcctNum()== acc_num_to_find)
return true;
}
return false;
}
int main()
{
Account ob("123", "John", 0, 0);
Account ob2("234", "Smith", 1, 1);
Account arr[] = {ob, ob2};
cout<<is_present(arr, 2, "234");
return 0;
}
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!