Question

In: Computer Science

C++ Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a...

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.

Solutions

Expert Solution

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!


Related Solutions

a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. [2.5 marks] b. Write a c++ function, called GCD that receives two parameters n, d, and prints their greatest common divisor [2.5 marks] Now after you created the two functions, you have to write a main function that can call the above functions according to user choice. Write a menu that allows the user to select from the following options...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
In R: write a function that inputs a vector x and a number n and returns...
In R: write a function that inputs a vector x and a number n and returns the first n elements of x. When n is greater than length(x), your function should just return x. We are not allowed to use any control flow statements
Write a boolean function that is given a binary tree and returns true if and only...
Write a boolean function that is given a binary tree and returns true if and only if the tree has an odd number of nodes. An empty tree is considered to have an even number of nodes. Notes: The function should have just one argument, a pointer to the root. No global variables may be used. No additional functions may be defined. You may not count the number of nodes.
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns a string containing the number word for the whole numbers 0 - 999. For example:  numberWords(234) would return 'two hundred thirty-four' If the input value is not a whole number between 0 - 999 then the function should return a string equivalent to 'ERROR'.
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT